tsip_parser_header_SIP_If_Match.rl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_SIP_If_Match.c
  23. * @brief SIP SIP-If-Match header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_SIP_If_Match.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_SIP_If_Match;
  38. # Includes
  39. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  40. action tag{
  41. tag_start = p;
  42. }
  43. action parse_ifmatch{
  44. TSK_PARSER_SET_STRING(hdr_ifmatch->value);
  45. }
  46. action eob{
  47. }
  48. SIP_If_Match = "SIP-If-Match"i HCOLON token>tag %parse_ifmatch;
  49. # Entry point
  50. main := SIP_If_Match :>CRLF @eob;
  51. }%%
  52. tsip_header_SIP_If_Match_t* tsip_header_SIP_If_Match_create(const char* etag)
  53. {
  54. return tsk_object_new(TSIP_HEADER_SIP_IF_MATCH_VA_ARGS(etag));
  55. }
  56. tsip_header_SIP_If_Match_t* tsip_header_SIP_If_Match_create_null()
  57. {
  58. return tsip_header_SIP_If_Match_create(tsk_null);
  59. }
  60. int tsip_header_SIP_If_Match_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  61. {
  62. if(header){
  63. const tsip_header_SIP_If_Match_t *SIP_If_Match = (const tsip_header_SIP_If_Match_t *)header;
  64. if(SIP_If_Match->value){
  65. return tsk_buffer_append(output, SIP_If_Match->value, tsk_strlen(SIP_If_Match->value));
  66. }
  67. return 0;
  68. }
  69. return -1;
  70. }
  71. tsip_header_SIP_If_Match_t *tsip_header_SIP_If_Match_parse(const char *data, tsk_size_t size)
  72. {
  73. int cs = 0;
  74. const char *p = data;
  75. const char *pe = p + size;
  76. const char *eof = pe;
  77. tsip_header_SIP_If_Match_t *hdr_ifmatch = tsip_header_SIP_If_Match_create_null();
  78. const char *tag_start = tsk_null;
  79. %%write data;
  80. (void)(eof);
  81. (void)(tsip_machine_parser_header_SIP_If_Match_first_final);
  82. (void)(tsip_machine_parser_header_SIP_If_Match_error);
  83. (void)(tsip_machine_parser_header_SIP_If_Match_en_main);
  84. %%write init;
  85. %%write exec;
  86. if( cs < %%{ write first_final; }%% ){
  87. TSK_DEBUG_ERROR("Failed to parse 'SIP-If-Match' header.");
  88. TSK_OBJECT_SAFE_FREE(hdr_ifmatch);
  89. }
  90. return hdr_ifmatch;
  91. }
  92. //========================================================
  93. // SIP_If_Match header object definition
  94. //
  95. static tsk_object_t* tsip_header_SIP_If_Match_ctor(tsk_object_t *self, va_list * app)
  96. {
  97. tsip_header_SIP_If_Match_t *SIP_If_Match = self;
  98. if(SIP_If_Match){
  99. TSIP_HEADER(SIP_If_Match)->type = tsip_htype_SIP_If_Match;
  100. TSIP_HEADER(SIP_If_Match)->serialize = tsip_header_SIP_If_Match_serialize;
  101. SIP_If_Match->value = tsk_strdup(va_arg(*app, const char*));
  102. }
  103. else{
  104. TSK_DEBUG_ERROR("Failed to create new SIP_If_Match header.");
  105. }
  106. return self;
  107. }
  108. static tsk_object_t* tsip_header_SIP_If_Match_dtor(tsk_object_t *self)
  109. {
  110. tsip_header_SIP_If_Match_t *SIP_If_Match = self;
  111. if(SIP_If_Match){
  112. TSK_FREE(SIP_If_Match->value);
  113. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(SIP_If_Match));
  114. }
  115. else{
  116. TSK_DEBUG_ERROR("Null SIP_If_Match header.");
  117. }
  118. return self;
  119. }
  120. static const tsk_object_def_t tsip_header_SIP_If_Match_def_s =
  121. {
  122. sizeof(tsip_header_SIP_If_Match_t),
  123. tsip_header_SIP_If_Match_ctor,
  124. tsip_header_SIP_If_Match_dtor,
  125. tsk_null
  126. };
  127. const tsk_object_def_t *tsip_header_SIP_If_Match_def_t = &tsip_header_SIP_If_Match_def_s;