tsip_parser_header_Date.rl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_Date.c
  23. * @brief SIP DUmmy header.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinysip/headers/tsip_header_Date.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_Date;
  38. # Includes
  39. include tsip_machine_utils "./ragel/tsip_machine_utils.rl";
  40. action tag{
  41. tag_start = p;
  42. }
  43. action parse_wkday{
  44. TSK_PARSER_SET_STRING(hdr_Date->wkday);
  45. }
  46. action parse_day{
  47. TSK_PARSER_SET_INTEGER(hdr_Date->day);
  48. }
  49. action parse_month{
  50. TSK_PARSER_SET_STRING(hdr_Date->month);
  51. }
  52. action parse_year{
  53. TSK_PARSER_SET_INTEGER(hdr_Date->year);
  54. }
  55. action parse_h{
  56. TSK_PARSER_SET_INTEGER(hdr_Date->time.h);
  57. }
  58. action parse_m{
  59. TSK_PARSER_SET_INTEGER(hdr_Date->time.m);
  60. }
  61. action parse_s{
  62. TSK_PARSER_SET_INTEGER(hdr_Date->time.s);
  63. }
  64. action eob{
  65. }
  66. wkday = "Mon"i | "Tue"i | "Wed"i | "Thu"i | "Fri"i | "Sat"i | "Sun"i;
  67. month = "Jan"i | "Feb"i | "Mar"i | "Apr"i | "May"i | "Jun"i | "Jul"i | "Aug"i | "Sep"i | "Oct"i | "Nov"i | "Dec"i;
  68. date1 = DIGIT{2}>tag %parse_day SP month>tag %parse_month SP DIGIT{4}>tag %parse_year;
  69. time = DIGIT{2}>tag %parse_h ":" DIGIT{2}>tag %parse_m ":" DIGIT{2}>tag %parse_s;
  70. rfc1123_date = wkday>tag %parse_wkday "," SP date1 SP time SP "GMT"i;
  71. SIP_date = rfc1123_date;
  72. Date = "Date"i HCOLON SIP_date;
  73. # Entry point
  74. main := Date :>CRLF @eob;
  75. }%%
  76. tsip_header_Date_t* tsip_header_Date_create(const char* wkday, const char* month, int8_t day, int16_t year, int8_t h, int8_t m, int8_t s)
  77. {
  78. return tsk_object_new(TSIP_HEADER_DATE_VA_ARGS(wkday, month, day, year, h, m, s));
  79. }
  80. tsip_header_Date_t* tsip_header_Date_create_null()
  81. {
  82. return tsip_header_Date_create(tsk_null, tsk_null, -1, -1, -1, -1, -1);
  83. }
  84. int tsip_header_Date_serialize(const tsip_header_t* header, tsk_buffer_t* output)
  85. {
  86. /* Date: Wed, 28 Apr 2010 23:42:50 GMT */
  87. if(header){
  88. const tsip_header_Date_t *Date = (const tsip_header_Date_t *)header;
  89. if(Date->month){
  90. tsk_buffer_append_2(output, "%s, %d %s %d %d:%d:%d GMT",
  91. Date->wkday, Date->day, Date->month, Date->year, Date->time.h, Date->time.m, Date->time.s);
  92. }
  93. return 0;
  94. }
  95. return -1;
  96. }
  97. tsip_header_Date_t *tsip_header_Date_parse(const char *data, tsk_size_t size)
  98. {
  99. int cs = 0;
  100. const char *p = data;
  101. const char *pe = p + size;
  102. const char *eof = pe;
  103. tsip_header_Date_t *hdr_Date = tsip_header_Date_create_null();
  104. const char *tag_start = tsk_null;
  105. %%write data;
  106. (void)(eof);
  107. (void)(tsip_machine_parser_header_Date_first_final);
  108. (void)(tsip_machine_parser_header_Date_error);
  109. (void)(tsip_machine_parser_header_Date_en_main);
  110. %%write init;
  111. %%write exec;
  112. if( cs < %%{ write first_final; }%% ){
  113. TSK_DEBUG_ERROR("Failed to parse 'Date' header.");
  114. TSK_OBJECT_SAFE_FREE(hdr_Date);
  115. }
  116. return hdr_Date;
  117. }
  118. //========================================================
  119. // Date header object definition
  120. //
  121. static tsk_object_t* tsip_header_Date_ctor(tsk_object_t *self, va_list * app)
  122. {
  123. tsip_header_Date_t *Date = self;
  124. if(Date){
  125. const char* wkday;
  126. const char* month;
  127. TSIP_HEADER(Date)->type = tsip_htype_Date;
  128. TSIP_HEADER(Date)->serialize = tsip_header_Date_serialize;
  129. Date->day = Date->time.h = Date->time.m = Date->time.s = -1;
  130. Date->year = -1;
  131. if((wkday = va_arg(*app, const char*))){
  132. month = va_arg(*app, const char*);
  133. Date->wkday = tsk_strdup(wkday);
  134. Date->month = tsk_strdup(month);
  135. #if defined __GNUC__
  136. Date->day = (int8_t)va_arg(*app, int);
  137. Date->year = (int16_t)va_arg(*app, int);
  138. Date->time.h = (int8_t)va_arg(*app, int);
  139. Date->time.m = (int8_t)va_arg(*app, int);
  140. Date->time.s = (int8_t)va_arg(*app, int);
  141. #else
  142. Date->day = va_arg(*app, int8_t);
  143. Date->year = va_arg(*app, int16_t);
  144. Date->time.h = va_arg(*app, int8_t);
  145. Date->time.m = va_arg(*app, int8_t);
  146. Date->time.s = va_arg(*app, int8_t);
  147. #endif
  148. }
  149. }
  150. else{
  151. TSK_DEBUG_ERROR("Failed to create new Date header.");
  152. }
  153. return self;
  154. }
  155. static tsk_object_t* tsip_header_Date_dtor(tsk_object_t *self)
  156. {
  157. tsip_header_Date_t *Date = self;
  158. if(Date){
  159. TSK_FREE(Date->wkday);
  160. TSK_FREE(Date->month);
  161. TSK_OBJECT_SAFE_FREE(TSIP_HEADER_PARAMS(Date));
  162. }
  163. else{
  164. TSK_DEBUG_ERROR("Null Date header.");
  165. }
  166. return self;
  167. }
  168. static const tsk_object_def_t tsip_header_Date_def_s =
  169. {
  170. sizeof(tsip_header_Date_t),
  171. tsip_header_Date_ctor,
  172. tsip_header_Date_dtor,
  173. tsk_null
  174. };
  175. const tsk_object_def_t *tsip_header_Date_def_t = &tsip_header_Date_def_s;