tmsrp_parser_header_Status.rl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_Status.c
  21. * @brief MSRP 'Status' header.
  22. */
  23. #include "tinymsrp/headers/tmsrp_header_Status.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_Status;
  33. # Includes
  34. include tmsrp_machine_utils "./ragel/tmsrp_machine_utils.rl";
  35. action tag{
  36. tag_start = p;
  37. }
  38. action parse_namespace{
  39. TSK_PARSER_SET_INT(hdr_Status->_namespace);
  40. }
  41. action parse_code{
  42. TSK_PARSER_SET_INT(hdr_Status->code);
  43. }
  44. action parse_reason{
  45. TSK_PARSER_SET_STRING(hdr_Status->reason);
  46. }
  47. #/// @par ABNF : Status = "Status:" SP namespace SP status-code [SP text-reason]
  48. #/// namespace = 3(DIGIT) ; "000" for all codes defined in RFC 4975
  49. #/// text-reason = utf8text
  50. Status = "Status:"i SP DIGIT{3}>tag %parse_namespace SP DIGIT{3}>tag %parse_code (SP utf8text>tag %parse_reason)?;
  51. # Entry point
  52. main := Status :>CRLF?;
  53. }%%
  54. tmsrp_header_Status_t* tmsrp_header_Status_create(short _namespace, short code, const char* reason)
  55. {
  56. return tsk_object_new(TMSRP_HEADER_STATUS_VA_ARGS(_namespace, code, reason));
  57. }
  58. tmsrp_header_Status_t* tmsrp_header_Status_create_null()
  59. {
  60. return tmsrp_header_Status_create(0, 200, tsk_null);
  61. }
  62. int tmsrp_header_Status_tostring(const tmsrp_header_t* header, tsk_buffer_t* output)
  63. {
  64. if(header){
  65. const tmsrp_header_Status_t *Status = (const tmsrp_header_Status_t *)header;
  66. // Status: 000 200 OK
  67. return tsk_buffer_append_2(output, "%.3hi %.3hi%s%s",
  68. Status->_namespace,
  69. Status->code,
  70. Status->reason ? " " : "",
  71. Status->reason ? Status->reason : ""
  72. );
  73. }
  74. return -1;
  75. }
  76. tmsrp_header_Status_t *tmsrp_header_Status_parse(const char *data, tsk_size_t size)
  77. {
  78. int cs = 0;
  79. const char *p = data;
  80. const char *pe = p + size;
  81. const char *eof = pe;
  82. tmsrp_header_Status_t *hdr_Status = tmsrp_header_Status_create_null();
  83. const char *tag_start = tsk_null;
  84. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  85. %%write data;
  86. (void)(eof);
  87. (void)(tmsrp_machine_parser_header_Status_first_final);
  88. (void)(tmsrp_machine_parser_header_Status_error);
  89. (void)(tmsrp_machine_parser_header_Status_en_main);
  90. %%write init;
  91. %%write exec;
  92. TSK_RAGEL_DISABLE_WARNINGS_END()
  93. if( cs < %%{ write first_final; }%% ){
  94. TSK_DEBUG_ERROR("Failed to parse 'Status' header.");
  95. TSK_OBJECT_SAFE_FREE(hdr_Status);
  96. }
  97. return hdr_Status;
  98. }
  99. //========================================================
  100. // Status header object definition
  101. //
  102. static tsk_object_t* tmsrp_header_Status_ctor(tsk_object_t *self, va_list * app)
  103. {
  104. tmsrp_header_Status_t *Status = self;
  105. if(Status){
  106. TMSRP_HEADER(Status)->type = tmsrp_htype_Status;
  107. TMSRP_HEADER(Status)->tostring = tmsrp_header_Status_tostring;
  108. #if defined(__GNUC__)
  109. Status->_namespace = (short)va_arg(*app, int);
  110. Status->code = (short)va_arg(*app, int);
  111. #else
  112. Status->_namespace = va_arg(*app, short);
  113. Status->code = va_arg(*app, short);
  114. #endif
  115. Status->reason = tsk_strdup( va_arg(*app, const char*) );
  116. }
  117. else{
  118. TSK_DEBUG_ERROR("Failed to create new Status header.");
  119. }
  120. return self;
  121. }
  122. static tsk_object_t* tmsrp_header_Status_dtor(tsk_object_t *self)
  123. {
  124. tmsrp_header_Status_t *Status = self;
  125. if(Status){
  126. TSK_FREE(Status->reason);
  127. }
  128. else{
  129. TSK_DEBUG_ERROR("Null Status header.");
  130. }
  131. return self;
  132. }
  133. static const tsk_object_def_t tmsrp_header_Status_def_s =
  134. {
  135. sizeof(tmsrp_header_Status_t),
  136. tmsrp_header_Status_ctor,
  137. tmsrp_header_Status_dtor,
  138. tsk_null
  139. };
  140. const tsk_object_def_t *tmsrp_header_Status_def_t = &tmsrp_header_Status_def_s;