tsip_parser_header_Require.rl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_Require.c
  23. * @brief SIP Require header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Require.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. #include <string.h>
  33. /***********************************
  34. * Ragel state machine.
  35. */
  36. %%{
  37. machine tsip_machine_parser_header_Require;
  38. # Includes
  39. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  40. action tag{
  41. tag_start = p;
  42. }
  43. action parse_option{
  44. TSK_PARSER_ADD_STRING(hdr_require->options);
  45. }
  46. action eob{
  47. }
  48. Require = "Require"i HCOLON option_tag>tag %parse_option ( COMMA option_tag>tag %parse_option )*;
  49. # Entry point
  50. main := Require :>CRLF @eob;
  51. }%%
  52. tsip_header_Require_t* tsip_header_Require_create(const char* option)
  53. {
  54. return tsk_object_new(TSIP_HEADER_REQUIRE_VA_ARGS(option));
  55. }
  56. tsip_header_Require_t* tsip_header_Require_create_null()
  57. {
  58. return tsip_header_Require_create(tsk_null);
  59. }
  60. int tsip_header_Require_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  61. {
  62. if(header){
  63. const tsip_header_Require_t *Require = (const tsip_header_Require_t *)header;
  64. tsk_list_item_t *item;
  65. tsk_string_t *str;
  66. int ret = 0;
  67. tsk_list_foreach(item, Require->options){
  68. str = item->data;
  69. if(item == Require->options->head){
  70. ret = tsk_buffer_append(output, str->value, tsk_strlen(str->value));
  71. }
  72. else{
  73. ret = tsk_buffer_append_2(output, ",%s", str->value);
  74. }
  75. }
  76. return ret;
  77. }
  78. return -1;
  79. }
  80. tsip_header_Require_t *tsip_header_Require_parse(const char *data, tsk_size_t size)
  81. {
  82. int cs = 0;
  83. const char *p = data;
  84. const char *pe = p + size;
  85. const char *eof = pe;
  86. tsip_header_Require_t *hdr_require = tsip_header_Require_create_null();
  87. const char *tag_start = tsk_null;
  88. %%write data;
  89. (void)(eof);
  90. (void)(tsip_machine_parser_header_Require_first_final);
  91. (void)(tsip_machine_parser_header_Require_error);
  92. (void)(tsip_machine_parser_header_Require_en_main);
  93. %%write init;
  94. %%write exec;
  95. if( cs < %%{ write first_final; }%% ){
  96. TSK_DEBUG_ERROR("Failed to parse 'Require' header.");
  97. TSK_OBJECT_SAFE_FREE(hdr_require);
  98. }
  99. return hdr_require;
  100. }
  101. //========================================================
  102. // Require header object definition
  103. //
  104. static tsk_object_t* tsip_header_Require_ctor(tsk_object_t *self, va_list * app)
  105. {
  106. tsip_header_Require_t *Require = self;
  107. if(Require){
  108. const char* option;
  109. TSIP_HEADER(Require)->type = tsip_htype_Require;
  110. TSIP_HEADER(Require)->serialize = tsip_header_Require_serialize;
  111. if((option = va_arg(*app, const char*))){
  112. tsk_string_t* string = tsk_string_create(option);
  113. Require->options = tsk_list_create();
  114. tsk_list_push_back_data(Require->options, ((void**) &string));
  115. }
  116. }
  117. else{
  118. TSK_DEBUG_ERROR("Failed to create new Require header.");
  119. }
  120. return self;
  121. }
  122. static tsk_object_t* tsip_header_Require_dtor(tsk_object_t *self)
  123. {
  124. tsip_header_Require_t *Require = self;
  125. if(Require){
  126. TSK_OBJECT_SAFE_FREE(Require->options);
  127. }
  128. else{
  129. TSK_DEBUG_ERROR("Null Require header.");
  130. }
  131. return self;
  132. }
  133. static const tsk_object_def_t tsip_header_Require_def_s =
  134. {
  135. sizeof(tsip_header_Require_t),
  136. tsip_header_Require_ctor,
  137. tsip_header_Require_dtor,
  138. tsk_null
  139. };
  140. const tsk_object_def_t *tsip_header_Require_def_t = &tsip_header_Require_def_s;