thttp_parser_header_Content_Length.rl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_Content_Length.c
  21. * @brief HTTP Content-Length header.
  22. *
  23. */
  24. #include "tinyhttp/headers/thttp_header_Content_Length.h"
  25. #include "tsk_debug.h"
  26. #include "tsk_memory.h"
  27. /***********************************
  28. * Ragel state machine.
  29. */
  30. %%{
  31. machine thttp_machine_parser_header_Content_Length;
  32. # Includes
  33. include thttp_machine_utils "./ragel/thttp_machine_utils.rl";
  34. action tag{
  35. tag_start = p;
  36. }
  37. action parse_content_length{
  38. TSK_PARSER_SET_INTEGER(hdr_clength->length);
  39. }
  40. action eob{
  41. }
  42. Content_Length = "Content-Length"i HCOLON (DIGIT+)>tag %parse_content_length;
  43. # Entry point
  44. main := Content_Length :>CRLF @eob;
  45. }%%
  46. thttp_header_Content_Length_t* thttp_header_content_length_create(uint32_t length)
  47. {
  48. return tsk_object_new(THTTP_HEADER_CONTENT_LENGTH_VA_ARGS(length));
  49. }
  50. int thttp_header_Content_Length_tostring(const thttp_header_t* header, tsk_buffer_t* output)
  51. {
  52. if(header){
  53. const thttp_header_Content_Length_t *Content_Length = (const thttp_header_Content_Length_t*)header;
  54. return tsk_buffer_append_2(output, "%d", Content_Length->length);
  55. }
  56. return -1;
  57. }
  58. /**@ingroup thttp_header_group
  59. */
  60. thttp_header_Content_Length_t *thttp_header_Content_Length_parse(const char *data, tsk_size_t size)
  61. {
  62. int cs = 0;
  63. const char *p = data;
  64. const char *pe = p + size;
  65. const char *eof = pe;
  66. thttp_header_Content_Length_t *hdr_clength = thttp_header_content_length_create(0);
  67. const char *tag_start = tsk_null;
  68. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  69. %%write data;
  70. (void)(eof);
  71. (void)(thttp_machine_parser_header_Content_Length_first_final);
  72. (void)(thttp_machine_parser_header_Content_Length_error);
  73. (void)(thttp_machine_parser_header_Content_Length_en_main);
  74. %%write init;
  75. %%write exec;
  76. TSK_RAGEL_DISABLE_WARNINGS_END()
  77. if( cs < %%{ write first_final; }%% ){
  78. TSK_OBJECT_SAFE_FREE(hdr_clength);
  79. }
  80. return hdr_clength;
  81. }
  82. //========================================================
  83. // Content_Length header object definition
  84. //
  85. static tsk_object_t* thttp_header_Content_Length_ctor(tsk_object_t *self, va_list * app)
  86. {
  87. thttp_header_Content_Length_t *Content_Length = self;
  88. if(Content_Length){
  89. Content_Length->length = va_arg(*app, uint32_t);
  90. THTTP_HEADER(Content_Length)->type = thttp_htype_Content_Length;
  91. THTTP_HEADER(Content_Length)->tostring = thttp_header_Content_Length_tostring;
  92. }
  93. else{
  94. TSK_DEBUG_ERROR("Failed to create new Content_Length header.");
  95. }
  96. return self;
  97. }
  98. static tsk_object_t* thttp_header_Content_Length_dtor(tsk_object_t *self)
  99. {
  100. thttp_header_Content_Length_t *Content_Length = self;
  101. if(Content_Length){
  102. TSK_OBJECT_SAFE_FREE(THTTP_HEADER_PARAMS(Content_Length));
  103. }
  104. else{
  105. TSK_DEBUG_ERROR("Null Content_Length header.");
  106. }
  107. return self;
  108. }
  109. static const tsk_object_def_t thttp_header_Content_Length_def_s =
  110. {
  111. sizeof(thttp_header_Content_Length_t),
  112. thttp_header_Content_Length_ctor,
  113. thttp_header_Content_Length_dtor,
  114. tsk_null
  115. };
  116. const tsk_object_def_t *thttp_header_Content_Length_def_t = &thttp_header_Content_Length_def_s;