tsip_parser_header_To.rl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_To.c
  23. * @brief SIP To/t header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_To.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. /***********************************
  33. * Ragel state machine.
  34. */
  35. %%{
  36. machine tsip_machine_parser_header_To;
  37. # Includes
  38. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  39. action tag{
  40. tag_start = p;
  41. }
  42. action parse_uri{
  43. int len = (int)(p - tag_start);
  44. if(hdr_to && !hdr_to->uri){
  45. if((hdr_to->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && hdr_to->display_name){
  46. hdr_to->uri->display_name = tsk_strdup(hdr_to->display_name);
  47. }
  48. }
  49. }
  50. action parse_display_name{
  51. TSK_PARSER_SET_STRING(hdr_to->display_name);
  52. tsk_strunquote(&hdr_to->display_name);
  53. }
  54. action parse_tag{
  55. TSK_PARSER_SET_STRING(hdr_to->tag);
  56. }
  57. action parse_param{
  58. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(hdr_to));
  59. }
  60. action eob{
  61. }
  62. my_tag_param = "tag"i EQUAL token>tag %parse_tag;
  63. to_param = my_tag_param@1 | (generic_param)@0 >tag %parse_param;
  64. URI = (scheme HCOLON any+)>tag %parse_uri;
  65. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  66. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  67. To = ( "To"i | "t"i ) HCOLON ( my_name_addr | URI ) ( SEMI to_param )*;
  68. # Entry point
  69. main := To :>CRLF @eob;
  70. }%%
  71. tsip_header_To_t* tsip_header_To_create(const char* display_name, const tsip_uri_t* uri, const char* tag)
  72. {
  73. return tsk_object_new(TSIP_HEADER_TO_VA_ARGS(display_name, uri, tag));
  74. }
  75. tsip_header_To_t* tsip_header_To_create_null()
  76. {
  77. return tsip_header_To_create(tsk_null, tsk_null, tsk_null);
  78. }
  79. int tsip_header_To_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  80. {
  81. if(header){
  82. int ret = 0;
  83. const tsip_header_To_t *To = (const tsip_header_To_t *)header;
  84. /* Uri with hacked display-name*/
  85. if((ret = tsip_uri_serialize(To->uri, tsk_true, tsk_true, output))){
  86. return ret;
  87. }
  88. if(To->tag && (ret = tsk_buffer_append_2(output, ";tag=%s", To->tag))){
  89. return ret;
  90. }
  91. return ret;
  92. }
  93. return -1;
  94. }
  95. tsip_header_To_t *tsip_header_To_parse(const char *data, tsk_size_t size)
  96. {
  97. int cs = 0;
  98. const char *p = data;
  99. const char *pe = p + size;
  100. const char *eof = pe;
  101. tsip_header_To_t *hdr_to = tsip_header_To_create_null();
  102. const char *tag_start = tsk_null;
  103. %%write data;
  104. (void)(eof);
  105. (void)(tsip_machine_parser_header_To_first_final);
  106. (void)(tsip_machine_parser_header_To_error);
  107. (void)(tsip_machine_parser_header_To_en_main);
  108. %%write init;
  109. %%write exec;
  110. if( cs < %%{ write first_final; }%% ){
  111. TSK_DEBUG_ERROR("Failed to parse 'To' header.");
  112. TSK_OBJECT_SAFE_FREE(hdr_to);
  113. }
  114. return hdr_to;
  115. }
  116. //========================================================
  117. // To header object definition
  118. //
  119. static tsk_object_t* tsip_header_To_ctor(tsk_object_t *self, va_list * app)
  120. {
  121. tsip_header_To_t *To = self;
  122. if(To){
  123. const char* display_name = va_arg(*app, const char *);
  124. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t *);
  125. const char* tag = va_arg(*app, const char *);
  126. To->display_name = tsk_strdup(display_name);
  127. if(uri) To->uri = tsk_object_ref((void *)uri);
  128. To->tag = tsk_strdup(tag);
  129. TSIP_HEADER(To)->type = tsip_htype_To;
  130. TSIP_HEADER(To)->serialize = tsip_header_To_serialize;
  131. }
  132. else{
  133. TSK_DEBUG_ERROR("Failed to create new To header.");
  134. }
  135. return self;
  136. }
  137. static tsk_object_t* tsip_header_To_dtor(tsk_object_t *self)
  138. {
  139. tsip_header_To_t *To = self;
  140. if(To){
  141. TSK_FREE(To->display_name);
  142. TSK_FREE(To->tag);
  143. TSK_OBJECT_SAFE_FREE(To->uri);
  144. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(To));
  145. }
  146. else{
  147. TSK_DEBUG_ERROR("Null To header.");
  148. }
  149. return self;
  150. }
  151. static const tsk_object_def_t tsip_header_To_def_s =
  152. {
  153. sizeof(tsip_header_To_t),
  154. tsip_header_To_ctor,
  155. tsip_header_To_dtor,
  156. tsk_null
  157. };
  158. const tsk_object_def_t *tsip_header_To_def_t = &tsip_header_To_def_s;