thttp_parser_header_Transfer_Encoding.rl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_Transfer_Encoding.c
  21. * @brief HTTP Transfer-Encoding header.
  22. */
  23. #include "tinyhttp/headers/thttp_header_Transfer_Encoding.h"
  24. #include "tsk_debug.h"
  25. #include "tsk_memory.h"
  26. #include <string.h>
  27. /***********************************
  28. * Ragel state machine.
  29. */
  30. %%{
  31. machine thttp_machine_parser_header_Transfer_Encoding;
  32. # Includes
  33. include thttp_machine_utils "./ragel/thttp_machine_utils.rl";
  34. action tag{
  35. tag_start = p;
  36. }
  37. action parse_encoding{
  38. TSK_PARSER_SET_STRING(hdr_tencoding->encoding);
  39. }
  40. action parse_param{
  41. TSK_PARSER_ADD_PARAM(THTTP_HEADER_PARAMS(hdr_tencoding));
  42. }
  43. action eob{
  44. }
  45. Transfer_Encoding = ( "Transfer-Encoding"i ) HCOLON token>tag %parse_encoding :>( ";" (pname("=" pvalue )?)>tag %parse_param )*;
  46. # Entry point
  47. main := Transfer_Encoding :>CRLF @eob;
  48. }%%
  49. thttp_header_Transfer_Encoding_t* thttp_header_transfer_encoding_create(const char* encoding)
  50. {
  51. return tsk_object_new(THTTP_HEADER_TRANSFER_ENCODING_VA_ARGS(encoding));
  52. }
  53. thttp_header_Transfer_Encoding_t* thttp_header_transfer_encoding_create_null()
  54. {
  55. return thttp_header_transfer_encoding_create(tsk_null);
  56. }
  57. int thttp_header_Transfer_Encoding_tostring(const thttp_header_t* header, tsk_buffer_t* output)
  58. {
  59. if(header){
  60. const thttp_header_Transfer_Encoding_t *Transfer_Encoding = (const thttp_header_Transfer_Encoding_t*)header;
  61. if(Transfer_Encoding->encoding){
  62. return tsk_buffer_append(output, Transfer_Encoding->encoding, tsk_strlen(Transfer_Encoding->encoding));
  63. }
  64. return 0;
  65. }
  66. return -1;
  67. }
  68. /**@ingroup thttp_header_group
  69. */
  70. thttp_header_Transfer_Encoding_t *thttp_header_Transfer_Encoding_parse(const char *data, tsk_size_t size)
  71. {
  72. int cs = 0;
  73. const char *p = data;
  74. const char *pe = p + size;
  75. const char *eof = pe;
  76. thttp_header_Transfer_Encoding_t *hdr_tencoding = thttp_header_transfer_encoding_create_null();
  77. const char *tag_start = tsk_null;
  78. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  79. %%write data;
  80. (void)(eof);
  81. (void)(thttp_machine_parser_header_Transfer_Encoding_first_final);
  82. (void)(thttp_machine_parser_header_Transfer_Encoding_error);
  83. (void)(thttp_machine_parser_header_Transfer_Encoding_en_main);
  84. %%write init;
  85. %%write exec;
  86. TSK_RAGEL_DISABLE_WARNINGS_END()
  87. if( cs < %%{ write first_final; }%% ){
  88. TSK_DEBUG_ERROR("Failed to parse Tansfer-Encoding header.");
  89. TSK_OBJECT_SAFE_FREE(hdr_tencoding);
  90. }
  91. return hdr_tencoding;
  92. }
  93. //========================================================
  94. // Transfer_Encoding header object definition
  95. //
  96. static tsk_object_t* thttp_header_Transfer_Encoding_ctor(tsk_object_t *self, va_list * app)
  97. {
  98. thttp_header_Transfer_Encoding_t *Transfer_Encoding = self;
  99. if(Transfer_Encoding){
  100. THTTP_HEADER(Transfer_Encoding)->type = thttp_htype_Transfer_Encoding;
  101. THTTP_HEADER(Transfer_Encoding)->tostring = thttp_header_Transfer_Encoding_tostring;
  102. Transfer_Encoding->encoding = tsk_strdup( va_arg(*app, const char*) );
  103. }
  104. else{
  105. TSK_DEBUG_ERROR("Failed to create new Transfer_Encoding header.");
  106. }
  107. return self;
  108. }
  109. static tsk_object_t* thttp_header_Transfer_Encoding_dtor(tsk_object_t* self)
  110. {
  111. thttp_header_Transfer_Encoding_t *Transfer_Encoding = self;
  112. if(Transfer_Encoding){
  113. TSK_FREE(Transfer_Encoding->encoding);
  114. TSK_OBJECT_SAFE_FREE(THTTP_HEADER_PARAMS(Transfer_Encoding));
  115. }
  116. else{
  117. TSK_DEBUG_ERROR("Null Transfer_Encoding header.");
  118. }
  119. return self;
  120. }
  121. static const tsk_object_def_t thttp_header_Transfer_Encoding_def_s =
  122. {
  123. sizeof(thttp_header_Transfer_Encoding_t),
  124. thttp_header_Transfer_Encoding_ctor,
  125. thttp_header_Transfer_Encoding_dtor,
  126. tsk_null
  127. };
  128. const tsk_object_def_t *thttp_header_Transfer_Encoding_def_t = &thttp_header_Transfer_Encoding_def_s;