tsip_parser_header_P_Associated_URI.rl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_P_Associated_URI.c
  23. * @brief SIP P-Associated-URI header as per RFC 3455.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_P_Associated_URI.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. #include "tsk_time.h"
  33. #include <string.h>
  34. /***********************************
  35. * Ragel state machine.
  36. */
  37. %%{
  38. machine tsip_machine_parser_header_P_Associated_URI;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_p_associated_uri{
  45. if(!curr_p_associated_uri){
  46. curr_p_associated_uri = tsip_header_P_Associated_URI_create_null();
  47. }
  48. }
  49. action parse_display_name{
  50. if(curr_p_associated_uri){
  51. TSK_PARSER_SET_STRING(curr_p_associated_uri->display_name);
  52. tsk_strunquote(&curr_p_associated_uri->display_name);
  53. }
  54. }
  55. action parse_uri{
  56. if(curr_p_associated_uri && !curr_p_associated_uri->uri){
  57. int len = (int)(p - tag_start);
  58. if(curr_p_associated_uri && !curr_p_associated_uri->uri){
  59. if((curr_p_associated_uri->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && curr_p_associated_uri->display_name){
  60. curr_p_associated_uri->uri->display_name = tsk_strdup(curr_p_associated_uri->display_name);
  61. }
  62. }
  63. }
  64. }
  65. action parse_param{
  66. if(curr_p_associated_uri){
  67. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_p_associated_uri));
  68. }
  69. }
  70. action add_p_associated_uri{
  71. if(curr_p_associated_uri){
  72. tsk_list_push_back_data(hdr_p_associated_uris, ((void**) &curr_p_associated_uri));
  73. }
  74. }
  75. action eob{
  76. }
  77. URI = (scheme HCOLON any+)>tag %parse_uri;
  78. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  79. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  80. ai_param = (generic_param)>tag %parse_param;
  81. p_aso_uri_spec = (my_name_addr ( SEMI ai_param )*) >create_p_associated_uri %add_p_associated_uri;
  82. P_Associated_URI = "P-Associated-URI"i HCOLON p_aso_uri_spec ( COMMA p_aso_uri_spec )*;
  83. # Entry point
  84. main := P_Associated_URI :>CRLF @eob;
  85. }%%
  86. tsip_header_P_Associated_URI_t* tsip_header_P_Associated_URI_create(const tsip_uri_t* uri)
  87. {
  88. return tsk_object_new(TSIP_HEADER_P_ASSOCIATED_URI_VA_ARGS(uri));
  89. }
  90. tsip_header_P_Associated_URI_t* tsip_header_P_Associated_URI_create_null()
  91. {
  92. return tsip_header_P_Associated_URI_create(tsk_null);
  93. }
  94. int tsip_header_P_Associated_URI_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  95. {
  96. if(header){
  97. const tsip_header_P_Associated_URI_t *P_Associated_URI = (const tsip_header_P_Associated_URI_t *)header;
  98. int ret = 0;
  99. /* Uri with hacked display-name*/
  100. if((ret = tsip_uri_serialize(P_Associated_URI->uri, tsk_true, tsk_true, output))){
  101. return ret;
  102. }
  103. return ret;
  104. }
  105. return -1;
  106. }
  107. tsip_header_P_Associated_URIs_L_t *tsip_header_P_Associated_URI_parse(const char *data, tsk_size_t size)
  108. {
  109. int cs = 0;
  110. const char *p = data;
  111. const char *pe = p + size;
  112. const char *eof = pe;
  113. tsip_header_P_Associated_URIs_L_t *hdr_p_associated_uris = tsk_list_create();
  114. const char *tag_start = tsk_null;
  115. tsip_header_P_Associated_URI_t *curr_p_associated_uri = tsk_null;
  116. %%write data;
  117. (void)(eof);
  118. (void)(tsip_machine_parser_header_P_Associated_URI_first_final);
  119. (void)(tsip_machine_parser_header_P_Associated_URI_error);
  120. (void)(tsip_machine_parser_header_P_Associated_URI_en_main);
  121. %%write init;
  122. %%write exec;
  123. if( cs < %%{ write first_final; }%% ){
  124. TSK_DEBUG_ERROR("Failed to parse 'P-Associated-URI' header.");
  125. TSK_OBJECT_SAFE_FREE(curr_p_associated_uri);
  126. TSK_OBJECT_SAFE_FREE(hdr_p_associated_uris);
  127. }
  128. return hdr_p_associated_uris;
  129. }
  130. //========================================================
  131. // P_Associated_URI header object definition
  132. //
  133. static tsk_object_t* tsip_header_P_Associated_URI_ctor(tsk_object_t *self, va_list * app)
  134. {
  135. tsip_header_P_Associated_URI_t *P_Associated_URI = self;
  136. if(P_Associated_URI){
  137. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t*);
  138. TSIP_HEADER(P_Associated_URI)->type = tsip_htype_P_Associated_URI;
  139. TSIP_HEADER(P_Associated_URI)->serialize = tsip_header_P_Associated_URI_serialize;
  140. if(uri){
  141. P_Associated_URI->uri = tsk_object_ref((void*)uri);
  142. }
  143. }
  144. else{
  145. TSK_DEBUG_ERROR("Failed to create new P_Associated_URI header.");
  146. }
  147. return self;
  148. }
  149. static tsk_object_t* tsip_header_P_Associated_URI_dtor(tsk_object_t *self)
  150. {
  151. tsip_header_P_Associated_URI_t *P_Associated_URI = self;
  152. if(P_Associated_URI){
  153. TSK_FREE(P_Associated_URI->display_name);
  154. TSK_OBJECT_SAFE_FREE(P_Associated_URI->uri);
  155. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(P_Associated_URI));
  156. }
  157. else{
  158. TSK_DEBUG_ERROR("Null P_Associated_URI header.");
  159. }
  160. return self;
  161. }
  162. static const tsk_object_def_t tsip_header_P_Associated_URI_def_s =
  163. {
  164. sizeof(tsip_header_P_Associated_URI_t),
  165. tsip_header_P_Associated_URI_ctor,
  166. tsip_header_P_Associated_URI_dtor,
  167. tsk_null
  168. };
  169. const tsk_object_def_t *tsip_header_P_Associated_URI_def_t = &tsip_header_P_Associated_URI_def_s;