tsip_parser_header_Route.rl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_Route.c
  23. * @brief SIP Route header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Route.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_Route;
  39. # Includes
  40. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  41. action tag{
  42. tag_start = p;
  43. }
  44. action create_route{
  45. if(!curr_route){
  46. curr_route = tsip_header_Route_create_null();
  47. }
  48. }
  49. action parse_display_name{
  50. if(curr_route){
  51. TSK_PARSER_SET_STRING(curr_route->display_name);
  52. tsk_strunquote(&curr_route->display_name);
  53. }
  54. }
  55. action parse_uri{
  56. if(curr_route && !curr_route->uri){
  57. int len = (int)(p - tag_start);
  58. if(curr_route && !curr_route->uri){
  59. if((curr_route->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && curr_route->display_name){
  60. curr_route->uri->display_name = tsk_strdup(curr_route->display_name);
  61. }
  62. }
  63. }
  64. }
  65. action parse_param{
  66. if(curr_route){
  67. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(curr_route));
  68. }
  69. }
  70. action add_route{
  71. if(curr_route){
  72. tsk_list_push_back_data(hdr_routes, ((void**) &curr_route));
  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. rr_param = (generic_param)>tag %parse_param;
  81. #route_param = (my_name_addr ( SEMI rr_param )*)>create_route %add_route;
  82. #Route = "Route"i HCOLON route_param (COMMA route_param)*;
  83. route_value = (my_name_addr ( SEMI rr_param )*) >create_route %add_route;
  84. Route = "Route"i HCOLON route_value (COMMA route_value)*;
  85. # Entry point
  86. main := Route :>CRLF @eob;
  87. }%%
  88. tsip_header_Route_t* tsip_header_Route_create(const tsip_uri_t* uri)
  89. {
  90. return tsk_object_new(TSIP_HEADER_ROUTE_VA_ARGS(uri));
  91. }
  92. tsip_header_Route_t* tsip_header_Route_create_null()
  93. {
  94. return tsip_header_Route_create(tsk_null);
  95. }
  96. int tsip_header_Route_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  97. {
  98. if(header){
  99. const tsip_header_Route_t *Route = (const tsip_header_Route_t *)header;
  100. int ret = 0;
  101. /* Uri with hacked display-name*/
  102. if((ret = tsip_uri_serialize(Route->uri, tsk_true, tsk_true, output))){
  103. return ret;
  104. }
  105. return ret;
  106. }
  107. return -1;
  108. }
  109. tsip_header_Routes_L_t *tsip_header_Route_parse(const char *data, tsk_size_t size)
  110. {
  111. int cs = 0;
  112. const char *p = data;
  113. const char *pe = p + size;
  114. const char *eof = pe;
  115. tsip_header_Routes_L_t *hdr_routes = tsk_list_create();
  116. const char *tag_start = tsk_null;
  117. tsip_header_Route_t *curr_route = tsk_null;
  118. %%write data;
  119. (void)(eof);
  120. (void)(tsip_machine_parser_header_Route_first_final);
  121. (void)(tsip_machine_parser_header_Route_error);
  122. (void)(tsip_machine_parser_header_Route_en_main);
  123. %%write init;
  124. %%write exec;
  125. if( cs < %%{ write first_final; }%% ){
  126. TSK_DEBUG_ERROR("Failed to parse 'Route' header.");
  127. TSK_OBJECT_SAFE_FREE(curr_route);
  128. TSK_OBJECT_SAFE_FREE(hdr_routes);
  129. }
  130. return hdr_routes;
  131. }
  132. //========================================================
  133. // Route header object definition
  134. //
  135. static tsk_object_t* tsip_header_Route_ctor(tsk_object_t *self, va_list * app)
  136. {
  137. tsip_header_Route_t *Route = self;
  138. if(Route){
  139. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t*);
  140. TSIP_HEADER(Route)->type = tsip_htype_Route;
  141. TSIP_HEADER(Route)->serialize = tsip_header_Route_serialize;
  142. if(uri){
  143. Route->uri = tsk_object_ref((void*)uri);
  144. }
  145. }
  146. else{
  147. TSK_DEBUG_ERROR("Failed to create new Route header.");
  148. }
  149. return self;
  150. }
  151. static tsk_object_t* tsip_header_Route_dtor(tsk_object_t *self)
  152. {
  153. tsip_header_Route_t *Route = self;
  154. if(Route){
  155. TSK_FREE(Route->display_name);
  156. TSK_OBJECT_SAFE_FREE(Route->uri);
  157. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Route));
  158. }
  159. else{
  160. TSK_DEBUG_ERROR("Null Route header.");
  161. }
  162. return self;
  163. }
  164. static const tsk_object_def_t tsip_header_Route_def_s =
  165. {
  166. sizeof(tsip_header_Route_t),
  167. tsip_header_Route_ctor,
  168. tsip_header_Route_dtor,
  169. tsk_null
  170. };
  171. const tsk_object_def_t *tsip_header_Route_def_t = &tsip_header_Route_def_s;