tmsrp_parser_header_Use-Path.rl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2009-2015 Mamadou DIOP.
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. *
  19. */
  20. /**@file tmsrp_header_Use_Path.c
  21. * @brief MSRP "Use-Path" header.
  22. */
  23. #include "tinymsrp/headers/tmsrp_header_Use-Path.h"
  24. #include "tsk_debug.h"
  25. #include "tsk_memory.h"
  26. #include "tsk_string.h"
  27. #include <string.h>
  28. /***********************************
  29. * Ragel state machine.
  30. */
  31. %%{
  32. machine tmsrp_machine_parser_header_Use_Path;
  33. # Includes
  34. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_uri{
  39. int len = (int)(p - tag_start);
  40. tmsrp_uri_t* uri;
  41. if((uri = tmsrp_uri_parse(tag_start, (tsk_size_t)len))){
  42. if(!header->uri){
  43. header->uri = uri;
  44. }
  45. else{
  46. if(!header->otherURIs){
  47. header->otherURIs = tsk_list_create();
  48. }
  49. tsk_list_push_back_data(header->otherURIs, ((void**) &uri));
  50. }
  51. }
  52. }
  53. MSRP_URI = (any -- SP)* >tag %parse_uri;
  54. #//"Use-Path:" SP MSRP-URI *( SP MSRP-URI )
  55. Use_Path = "Use-Path:"i SP MSRP_URI (SP <:MSRP_URI)*;
  56. # Entry point
  57. main := Use_Path :>CRLF?;
  58. }%%
  59. tmsrp_header_Use_Path_t* tmsrp_header_Use_Path_create(const tmsrp_uri_t* uri)
  60. {
  61. return tsk_object_new(TMSRP_HEADER_USE_PATH_VA_ARGS(uri));
  62. }
  63. tmsrp_header_Use_Path_t* tmsrp_header_Use_Path_create_null()
  64. {
  65. return tmsrp_header_Use_Path_create(tsk_null);
  66. }
  67. int tmsrp_header_Use_Path_tostring(const tmsrp_header_t* header, tsk_buffer_t* output)
  68. {
  69. if(header){
  70. const tmsrp_header_Use_Path_t *Use_Path = (const tmsrp_header_Use_Path_t *)header;
  71. const tsk_list_item_t *item;
  72. if(Use_Path->uri){
  73. tmsrp_uri_serialize(Use_Path->uri, output);
  74. }
  75. tsk_list_foreach(item, Use_Path->otherURIs){
  76. tsk_buffer_append(output, " ", 1);
  77. tmsrp_uri_serialize(TMSRP_URI(item->data), output);
  78. }
  79. }
  80. return -1;
  81. }
  82. tmsrp_header_Use_Path_t *tmsrp_header_Use_Path_parse(const char *data, tsk_size_t size)
  83. {
  84. int cs = 0;
  85. const char *p = data;
  86. const char *pe = p + size;
  87. const char *eof = pe;
  88. tmsrp_header_Use_Path_t *header = tmsrp_header_Use_Path_create_null();
  89. const char *tag_start = tsk_null;
  90. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  91. %%write data;
  92. (void)(eof);
  93. (void)(tmsrp_machine_parser_header_Use_Path_first_final);
  94. (void)(tmsrp_machine_parser_header_Use_Path_error);
  95. (void)(tmsrp_machine_parser_header_Use_Path_en_main);
  96. %%write init;
  97. %%write exec;
  98. TSK_RAGEL_DISABLE_WARNINGS_END()
  99. if( cs < %%{ write first_final; }%% ){
  100. TSK_DEBUG_ERROR("Failed to parse 'Use-Path' header.");
  101. TSK_OBJECT_SAFE_FREE(header);
  102. }
  103. return header;
  104. }
  105. //========================================================
  106. // Use_Path header object definition
  107. //
  108. static tsk_object_t* tmsrp_header_Use_Path_ctor(tsk_object_t *self, va_list * app)
  109. {
  110. tmsrp_header_Use_Path_t *Use_Path = self;
  111. if(Use_Path){
  112. const tmsrp_uri_t* uri = va_arg(*app, const tmsrp_uri_t*);
  113. TMSRP_HEADER(Use_Path)->type = tmsrp_htype_Use_Path;
  114. TMSRP_HEADER(Use_Path)->tostring = tmsrp_header_Use_Path_tostring;
  115. if(uri){
  116. Use_Path->uri = tsk_object_ref((void*)uri);
  117. }
  118. }
  119. else{
  120. TSK_DEBUG_ERROR("Failed to create new Use-Path header.");
  121. }
  122. return self;
  123. }
  124. static tsk_object_t* tmsrp_header_Use_Path_dtor(tsk_object_t *self)
  125. {
  126. tmsrp_header_Use_Path_t *Use_Path = self;
  127. if(Use_Path){
  128. TSK_OBJECT_SAFE_FREE(Use_Path->uri);
  129. TSK_OBJECT_SAFE_FREE(Use_Path->otherURIs);
  130. }
  131. else{
  132. TSK_DEBUG_ERROR("Null Use-Path header.");
  133. }
  134. return self;
  135. }
  136. static const tsk_object_def_t tmsrp_header_Use_Path_def_s =
  137. {
  138. sizeof(tmsrp_header_Use_Path_t),
  139. tmsrp_header_Use_Path_ctor,
  140. tmsrp_header_Use_Path_dtor,
  141. tsk_null
  142. };
  143. const tsk_object_def_t *tmsrp_header_Use_Path_def_t = &tmsrp_header_Use_Path_def_s;