tsip_parser_header_Session_Expires.rl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_Session_Expires.c
  23. * @brief SIP Min-SE header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Session_Expires.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_Session_Expires;
  37. # Includes
  38. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  39. action tag{
  40. tag_start = p;
  41. }
  42. action parse_delta_seconds{
  43. TSK_PARSER_SET_INTEGER(hdr_session_expires->delta_seconds);
  44. }
  45. action is_uas{
  46. hdr_session_expires->refresher_uas = tsk_true;
  47. }
  48. action is_uac{
  49. hdr_session_expires->refresher_uas = tsk_false;
  50. }
  51. action parse_param{
  52. TSK_PARSER_ADD_PARAM(TSIP_HEADER_PARAMS(hdr_session_expires));
  53. }
  54. action eob{
  55. }
  56. refresher_param = "refresher"i EQUAL ( "uas"i>tag %is_uas | "uac"i>tag %is_uac );
  57. se_params = (refresher_param)@1 | (generic_param)@0>tag %parse_param;
  58. Session_Expires = ( "Session-Expires"i | "x"i ) HCOLON delta_seconds>tag %parse_delta_seconds ( SEMI se_params )*;
  59. # Entry point
  60. main := Session_Expires :>CRLF @eob;
  61. }%%
  62. tsip_header_Session_Expires_t* tsip_header_Session_Expires_create(int64_t delta_seconds, tsk_bool_t refresher_uas)
  63. {
  64. return tsk_object_new(TSIP_HEADER_SESSION_EXPIRES_VA_ARGS(delta_seconds, refresher_uas));
  65. }
  66. int tsip_header_Session_Expires_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  67. {
  68. if(header){
  69. const tsip_header_Session_Expires_t *Session_Expires = (const tsip_header_Session_Expires_t *)header;
  70. if(Session_Expires->delta_seconds >=0){
  71. return tsk_buffer_append_2(output, "%lld;refresher=%s",
  72. Session_Expires->delta_seconds, Session_Expires->refresher_uas ? "uas" : "uac");
  73. }
  74. return 0;
  75. }
  76. return -1;
  77. }
  78. tsip_header_Session_Expires_t *tsip_header_Session_Expires_parse(const char *data, tsk_size_t size)
  79. {
  80. int cs = 0;
  81. const char *p = data;
  82. const char *pe = p + size;
  83. const char *eof = pe;
  84. tsip_header_Session_Expires_t *hdr_session_expires = tsip_header_Session_Expires_create(TSIP_SESSION_EXPIRES_DEFAULT_VALUE, tsk_false);
  85. const char *tag_start = tsk_null;
  86. %%write data;
  87. (void)(eof);
  88. (void)(tsip_machine_parser_header_Session_Expires_first_final);
  89. (void)(tsip_machine_parser_header_Session_Expires_error);
  90. (void)(tsip_machine_parser_header_Session_Expires_en_main);
  91. %%write init;
  92. %%write exec;
  93. if( cs < %%{ write first_final; }%% ){
  94. TSK_DEBUG_ERROR("Failed to parse 'Session-Expires' header.");
  95. TSK_OBJECT_SAFE_FREE(hdr_session_expires);
  96. }
  97. return hdr_session_expires;
  98. }
  99. //========================================================
  100. // Session_Expires header object definition
  101. //
  102. static tsk_object_t* tsip_header_Session_Expires_ctor(tsk_object_t *self, va_list * app)
  103. {
  104. tsip_header_Session_Expires_t *Session_Expires = self;
  105. if(Session_Expires){
  106. TSIP_HEADER(Session_Expires)->type = tsip_htype_Session_Expires;
  107. TSIP_HEADER(Session_Expires)->serialize = tsip_header_Session_Expires_serialize;
  108. Session_Expires->delta_seconds = va_arg(*app, int64_t);
  109. Session_Expires->refresher_uas = va_arg(*app, tsk_bool_t);
  110. }
  111. else{
  112. TSK_DEBUG_ERROR("Failed to create new Session-Expires header.");
  113. }
  114. return self;
  115. }
  116. static tsk_object_t* tsip_header_Session_Expires_dtor(tsk_object_t *self)
  117. {
  118. tsip_header_Session_Expires_t *Session_Expires = self;
  119. if(Session_Expires){
  120. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Session_Expires));
  121. }
  122. else{
  123. TSK_DEBUG_ERROR("Null Session-Expires header.");
  124. }
  125. return self;
  126. }
  127. static const tsk_object_def_t tsip_header_Session_Expires_def_s =
  128. {
  129. sizeof(tsip_header_Session_Expires_t),
  130. tsip_header_Session_Expires_ctor,
  131. tsip_header_Session_Expires_dtor,
  132. tsk_null
  133. };
  134. const tsk_object_def_t *tsip_header_Session_Expires_def_t = &tsip_header_Session_Expires_def_s;