tsip_parser_header_Content_Length.rl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file tsip_header_Content_Length.c
  23. * @brief SIP Content-Length/l header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Content_Length.h"
  29. #include "tsk_debug.h"
  30. #include "tsk_memory.h"
  31. /***********************************
  32. * Ragel state machine.
  33. */
  34. %%{
  35. machine tsip_machine_parser_header_Content_Length;
  36. # Includes
  37. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  38. action tag{
  39. tag_start = p;
  40. }
  41. action parse_content_length{
  42. TSK_PARSER_SET_INTEGER(hdr_clength->length);
  43. }
  44. action eob{
  45. }
  46. Content_Length = ( "Content-Length"i | "l"i ) HCOLON (DIGIT+)>tag %parse_content_length;
  47. # Entry point
  48. main := Content_Length :>CRLF @eob;
  49. }%%
  50. tsip_header_Content_Length_t* tsip_header_Content_Length_create(uint32_t length)
  51. {
  52. return tsk_object_new(TSIP_HEADER_CONTENT_LENGTH_VA_ARGS(length));
  53. }
  54. tsip_header_Content_Length_t* tsip_header_Content_Length_create_null()
  55. {
  56. return tsip_header_Content_Length_create(0);
  57. }
  58. int tsip_header_Content_Length_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  59. {
  60. if(header){
  61. const tsip_header_Content_Length_t *Content_Length = (const tsip_header_Content_Length_t *)header;
  62. return tsk_buffer_append_2(output, "%u", Content_Length->length);
  63. }
  64. return -1;
  65. }
  66. tsip_header_Content_Length_t *tsip_header_Content_Length_parse(const char *data, tsk_size_t size)
  67. {
  68. int cs = 0;
  69. const char *p = data;
  70. const char *pe = p + size;
  71. const char *eof = pe;
  72. tsip_header_Content_Length_t *hdr_clength = tsip_header_Content_Length_create(0);
  73. const char *tag_start = tsk_null;
  74. %%write data;
  75. (void)(eof);
  76. (void)(tsip_machine_parser_header_Content_Length_first_final);
  77. (void)(tsip_machine_parser_header_Content_Length_error);
  78. (void)(tsip_machine_parser_header_Content_Length_en_main);
  79. %%write init;
  80. %%write exec;
  81. if( cs < %%{ write first_final; }%% ){
  82. TSK_DEBUG_ERROR("Failed to parse SIP 'Content-Length' header.");
  83. TSK_OBJECT_SAFE_FREE(hdr_clength);
  84. }
  85. return hdr_clength;
  86. }
  87. //========================================================
  88. // Content_Length header object definition
  89. //
  90. static tsk_object_t* tsip_header_Content_Length_ctor(tsk_object_t *self, va_list * app)
  91. {
  92. tsip_header_Content_Length_t *Content_Length = self;
  93. if(Content_Length){
  94. Content_Length->length = va_arg(*app, uint32_t);
  95. TSIP_HEADER(Content_Length)->type = tsip_htype_Content_Length;
  96. TSIP_HEADER(Content_Length)->serialize = tsip_header_Content_Length_serialize;
  97. }
  98. else{
  99. TSK_DEBUG_ERROR("Failed to create new Content_Length header.");
  100. }
  101. return self;
  102. }
  103. static tsk_object_t* tsip_header_Content_Length_dtor(tsk_object_t *self)
  104. {
  105. tsip_header_Content_Length_t *Content_Length = self;
  106. if(Content_Length){
  107. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Content_Length));
  108. }
  109. else{
  110. TSK_DEBUG_ERROR("Null Content_Length header.");
  111. }
  112. return self;
  113. }
  114. static const tsk_object_def_t tsip_header_Content_Length_def_s =
  115. {
  116. sizeof(tsip_header_Content_Length_t),
  117. tsip_header_Content_Length_ctor,
  118. tsip_header_Content_Length_dtor,
  119. tsk_null
  120. };
  121. const tsk_object_def_t *tsip_header_Content_Length_def_t = &tsip_header_Content_Length_def_s;