tsip_parser_header_Refer_To.rl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_Refer_To.c
  23. * @brief SIP Refer-To header as per RFC 3515.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Refer_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_Refer_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. if(!r_to->uri) /* Only one URI */{
  44. int len = (int)(p - tag_start);
  45. if(r_to && !r_to->uri){
  46. if((r_to->uri = tsip_uri_parse(tag_start, (tsk_size_t)len)) && r_to->display_name){
  47. r_to->uri->display_name = tsk_strdup(r_to->display_name);
  48. }
  49. }
  50. }
  51. }
  52. action parse_display_name{
  53. if(!r_to->display_name){
  54. TSK_PARSER_SET_STRING(r_to->display_name);
  55. tsk_strunquote(&r_to->display_name);
  56. }
  57. }
  58. action parse_param{
  59. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(r_to));
  60. }
  61. action eob{
  62. }
  63. URI = (scheme HCOLON any+)>tag %parse_uri;
  64. display_name = (( token LWS )+ | quoted_string)>tag %parse_display_name;
  65. my_name_addr = display_name? :>LAQUOT<: URI :>RAQUOT;
  66. feature_param = generic_param; # HACK
  67. refer_param = generic_param>tag %parse_param | feature_param>tag %parse_param;
  68. Refer_To = ( "Refer-To"i | "r"i ) HCOLON ( my_name_addr>0 | URI>1 ) ( SEMI refer_param )*;
  69. # Entry point
  70. main := Refer_To :>CRLF @eob;
  71. }%%
  72. tsip_header_Refer_To_t* tsip_header_Refer_To_create(const tsip_uri_t* uri)
  73. {
  74. return tsk_object_new(TSIP_HEADER_REFER_TO_VA_ARGS(uri));
  75. }
  76. tsip_header_Refer_To_t* tsip_header_Refer_To_create_null()
  77. {
  78. return tsip_header_Refer_To_create(tsk_null);
  79. }
  80. int tsip_header_Refer_To_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  81. {
  82. if(header){
  83. int ret;
  84. const tsip_header_Refer_To_t *Refer_To = (const tsip_header_Refer_To_t *)header;
  85. /* Uri with hacked display-name*/
  86. if((ret = tsip_uri_serialize(Refer_To->uri, tsk_true, tsk_true, output))){
  87. return ret;
  88. }
  89. return ret;
  90. }
  91. return -1;
  92. }
  93. tsip_header_Refer_To_t *tsip_header_Refer_To_parse(const char *data, tsk_size_t size)
  94. {
  95. int cs = 0;
  96. const char *p = data;
  97. const char *pe = p + size;
  98. const char *eof = pe;
  99. tsip_header_Refer_To_t *r_to = tsip_header_Refer_To_create_null();
  100. const char *tag_start = tsk_null;
  101. %%write data;
  102. (void)(eof);
  103. (void)(tsip_machine_parser_header_Refer_To_first_final);
  104. (void)(tsip_machine_parser_header_Refer_To_error);
  105. (void)(tsip_machine_parser_header_Refer_To_en_main);
  106. %%write init;
  107. %%write exec;
  108. if( cs < %%{ write first_final; }%% ){
  109. TSK_DEBUG_ERROR("Failed to parse 'Refer-To' header.");
  110. TSK_OBJECT_SAFE_FREE(r_to);
  111. }
  112. return r_to;
  113. }
  114. //========================================================
  115. // Refer_To header object definition
  116. //
  117. static tsk_object_t* tsip_header_Refer_To_ctor(tsk_object_t *self, va_list * app)
  118. {
  119. tsip_header_Refer_To_t *Refer_To = self;
  120. if(Refer_To){
  121. const tsip_uri_t* uri = va_arg(*app, const tsip_uri_t*);
  122. TSIP_HEADER(Refer_To)->type = tsip_htype_Refer_To;
  123. TSIP_HEADER(Refer_To)->serialize = tsip_header_Refer_To_serialize;
  124. if(uri){
  125. Refer_To->uri = tsk_object_ref((void*)uri);
  126. }
  127. }
  128. else{
  129. TSK_DEBUG_ERROR("Failed to create new Refer_To header.");
  130. }
  131. return self;
  132. }
  133. static tsk_object_t* tsip_header_Refer_To_dtor(tsk_object_t *self)
  134. {
  135. tsip_header_Refer_To_t *Refer_To = self;
  136. if(Refer_To){
  137. TSK_FREE(Refer_To->display_name);
  138. TSK_OBJECT_SAFE_FREE(Refer_To->uri);
  139. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Refer_To));
  140. }
  141. else{
  142. TSK_DEBUG_ERROR("Null Refer_To header.");
  143. }
  144. return self;
  145. }
  146. static const tsk_object_def_t tsip_header_Refer_To_def_s =
  147. {
  148. sizeof(tsip_header_Refer_To_t),
  149. tsip_header_Refer_To_ctor,
  150. tsip_header_Refer_To_dtor,
  151. tsk_null
  152. };
  153. const tsk_object_def_t *tsip_header_Refer_To_def_t = &tsip_header_Refer_To_def_s;