thttp_parser_header_Dummy.rl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2010-2015 Mamadou Diop.
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. *
  19. */
  20. /**@file thttp_header_Dummy.c
  21. * @brief HTTP 'Dummy' header.
  22. *
  23. */
  24. #include "tinyhttp/headers/thttp_header_Dummy.h"
  25. #include "tinyhttp/parsers/thttp_parser_url.h"
  26. #include "tsk_debug.h"
  27. #include "tsk_memory.h"
  28. #include <string.h>
  29. /***********************************
  30. * Ragel state machine.
  31. */
  32. %%{
  33. machine thttp_machine_parser_header_Dummy;
  34. # Includes
  35. include thttp_machine_utils "./ragel/thttp_machine_utils.rl";
  36. action tag{
  37. tag_start = p;
  38. }
  39. action parse_name{
  40. TSK_PARSER_SET_STRING(hdr_Dummy->name);
  41. }
  42. action parse_value{
  43. TSK_PARSER_SET_STRING(hdr_Dummy->value);
  44. }
  45. action eob{
  46. }
  47. Dummy = token>tag %parse_name SP* HCOLON SP*<: any*>tag %parse_value;
  48. # Entry point
  49. main := Dummy :>CRLF @eob;
  50. }%%
  51. thttp_header_Dummy_t* thttp_header_dummy_create(const char* name, const char* value)
  52. {
  53. return tsk_object_new(THTTP_HEADER_DUMMY_VA_ARGS(name, value));
  54. }
  55. thttp_header_Dummy_t* thttp_header_dummy_create_null()
  56. {
  57. return thttp_header_dummy_create(tsk_null, tsk_null);
  58. }
  59. int thttp_header_Dummy_tostring(const thttp_header_t* header, tsk_buffer_t* output)
  60. {
  61. if(header){
  62. const thttp_header_Dummy_t *Dummy = (const thttp_header_Dummy_t*)header;
  63. if(Dummy->value){
  64. return tsk_buffer_append(output, Dummy->value, tsk_strlen(Dummy->value));
  65. }
  66. return 0;
  67. }
  68. return -1;
  69. }
  70. /**@ingroup thttp_header_group
  71. */
  72. thttp_header_Dummy_t *thttp_header_Dummy_parse(const char *data, tsk_size_t size)
  73. {
  74. int cs = 0;
  75. const char *p = data;
  76. const char *pe = p + size;
  77. const char *eof = pe;
  78. thttp_header_Dummy_t *hdr_Dummy = thttp_header_dummy_create_null();
  79. const char *tag_start = tsk_null;
  80. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  81. %%write data;
  82. (void)(eof);
  83. (void)(thttp_machine_parser_header_Dummy_first_final);
  84. (void)(thttp_machine_parser_header_Dummy_error);
  85. (void)(thttp_machine_parser_header_Dummy_en_main);
  86. %%write init;
  87. %%write exec;
  88. TSK_RAGEL_DISABLE_WARNINGS_END()
  89. if( cs < %%{ write first_final; }%% ){
  90. TSK_OBJECT_SAFE_FREE(hdr_Dummy);
  91. }
  92. return hdr_Dummy;
  93. }
  94. //========================================================
  95. // Dummy header object definition
  96. //
  97. static tsk_object_t* thttp_header_Dummy_ctor(tsk_object_t *self, va_list * app)
  98. {
  99. thttp_header_Dummy_t *Dummy = self;
  100. if(Dummy){
  101. THTTP_HEADER(Dummy)->type = thttp_htype_Dummy;
  102. THTTP_HEADER(Dummy)->tostring = thttp_header_Dummy_tostring;
  103. Dummy->name = tsk_strdup(va_arg(*app, const char*));
  104. Dummy->value = tsk_strdup(va_arg(*app, const char*));
  105. }
  106. else{
  107. TSK_DEBUG_ERROR("Failed to create new Dummy header.");
  108. }
  109. return self;
  110. }
  111. static tsk_object_t* thttp_header_Dummy_dtor(tsk_object_t *self)
  112. {
  113. thttp_header_Dummy_t *Dummy = self;
  114. if(Dummy){
  115. TSK_FREE(Dummy->name);
  116. TSK_FREE(Dummy->value);
  117. TSK_OBJECT_SAFE_FREE(THTTP_HEADER_PARAMS(Dummy));
  118. }
  119. else{
  120. TSK_DEBUG_ERROR("Null Dummy header.");
  121. }
  122. return self;
  123. }
  124. static const tsk_object_def_t thttp_header_Dummy_def_s =
  125. {
  126. sizeof(thttp_header_Dummy_t),
  127. thttp_header_Dummy_ctor,
  128. thttp_header_Dummy_dtor,
  129. tsk_null
  130. };
  131. const tsk_object_def_t *thttp_header_Dummy_def_t = &thttp_header_Dummy_def_s;