tsip_parser_header_Allow.rl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_Allow.c
  23. * @brief SIP Allow header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Allow.h"
  29. #include "tinysip/parsers/tsip_parser_uri.h"
  30. #include "tsk_debug.h"
  31. #include "tsk_memory.h"
  32. #include <string.h>
  33. /***********************************
  34. * Ragel state machine.
  35. */
  36. %%{
  37. machine tsip_machine_parser_header_Allow;
  38. # Includes
  39. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  40. action tag
  41. {
  42. tag_start = p;
  43. }
  44. action parse_method
  45. {
  46. TSK_PARSER_ADD_STRING(hdr_allow->methods);
  47. }
  48. action eob
  49. {
  50. }
  51. Allow = "Allow"i HCOLON ( Method>tag %parse_method ( COMMA Method>tag %parse_method )* )?;
  52. # Entry point
  53. main := Allow :>CRLF @eob;
  54. }%%
  55. tsip_header_Allow_t* tsip_header_Allow_create()
  56. {
  57. return tsk_object_new(tsip_header_Allow_def_t);
  58. }
  59. int tsip_header_Allow_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  60. {
  61. if(header){
  62. const tsip_header_Allow_t *Allow = (const tsip_header_Allow_t *)header;
  63. tsk_list_item_t *item;
  64. tsk_string_t *str;
  65. int ret = 0;
  66. tsk_list_foreach(item, Allow->methods){
  67. str = item->data;
  68. if(item == Allow->methods->head){
  69. tsk_buffer_append(output, str->value, tsk_strlen(str->value));
  70. }
  71. else{
  72. tsk_buffer_append_2(output, ",%s", str->value);
  73. }
  74. }
  75. return ret;
  76. }
  77. return -1;
  78. }
  79. tsip_header_Allow_t *tsip_header_Allow_parse(const char *data, tsk_size_t size)
  80. {
  81. int cs = 0;
  82. const char *p = data;
  83. const char *pe = p + size;
  84. const char *eof = pe;
  85. tsip_header_Allow_t *hdr_allow = tsip_header_Allow_create();
  86. const char *tag_start = tsk_null;
  87. %%write data;
  88. (void)(eof);
  89. (void)(void)(tsip_machine_parser_header_Allow_first_final);
  90. (void)(void)(tsip_machine_parser_header_Allow_error);
  91. (void)(void)(tsip_machine_parser_header_Allow_en_main);
  92. %%write init;
  93. %%write exec;
  94. if( cs < %%{ write first_final; }%% ){
  95. TSK_DEBUG_ERROR("Failed to parse SIP 'Allow' header.");
  96. TSK_OBJECT_SAFE_FREE(hdr_allow);
  97. }
  98. return hdr_allow;
  99. }
  100. tsk_bool_t tsip_header_Allow_allows(const tsip_header_Allow_t* self, const char* method)
  101. {
  102. const tsk_list_item_t* item;
  103. const tsk_string_t* string;
  104. if(!self || !method){
  105. TSK_DEBUG_ERROR("Invalid parameter");
  106. return -1;
  107. }
  108. tsk_list_foreach(item, self->methods){
  109. if(!(string = item->data)){
  110. continue;
  111. }
  112. if(tsk_striequals(string->value, method)){
  113. return tsk_true;
  114. }
  115. }
  116. return tsk_false;
  117. }
  118. //========================================================
  119. // Allow header object definition
  120. //
  121. static tsk_object_t* tsip_header_Allow_ctor(tsk_object_t *self, va_list * app)
  122. {
  123. tsip_header_Allow_t *Allow = self;
  124. if(Allow){
  125. /*const char* methods = va_arg(*app, const char *);
  126. if(methods && !tsk_strempty(methods))
  127. {
  128. Allow->methods = tsip_header_Allow_parse(methods, tsk_strlen(methods));
  129. }*/
  130. TSIP_HEADER(Allow)->type = tsip_htype_Allow;
  131. TSIP_HEADER(Allow)->serialize = tsip_header_Allow_serialize;
  132. }
  133. else{
  134. TSK_DEBUG_ERROR("Failed to create new Allow header.");
  135. }
  136. return self;
  137. }
  138. static tsk_object_t* tsip_header_Allow_dtor(tsk_object_t *self)
  139. {
  140. tsip_header_Allow_t *Allow = self;
  141. if(Allow){
  142. TSK_OBJECT_SAFE_FREE(Allow->methods);
  143. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Allow));
  144. }
  145. else TSK_DEBUG_ERROR("Null Allow header.");
  146. return self;
  147. }
  148. static const tsk_object_def_t tsip_header_Allow_def_s =
  149. {
  150. sizeof(tsip_header_Allow_t),
  151. tsip_header_Allow_ctor,
  152. tsip_header_Allow_dtor,
  153. tsk_null
  154. };
  155. const tsk_object_def_t *tsip_header_Allow_def_t = &tsip_header_Allow_def_s;