tmedia_content_cpim.rl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2010-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 tmedia_content_cpim.c
  21. * @brief Common Presence and Instant Messaging (CPIM): Message Format (RFC 3862)
  22. */
  23. #include "tinymedia/content/tmedia_content_cpim.h"
  24. #include "tsk_debug.h"
  25. #include "tsk_memory.h"
  26. #include "tsk_string.h"
  27. #include "tsk_ragel_state.h"
  28. #include <string.h>
  29. /* RFC 3862 - 2. Overall Message Structure
  30. A complete message looks something like this:
  31. m: Content-type: Message/CPIM
  32. s:
  33. h: (message-metadata-headers)
  34. s:
  35. e: (encapsulated MIME message-body)
  36. The end of the message body is defined by the framing mechanism of
  37. the protocol used. The tags 'm:', 's:', 'h:', 'e:', and 'x:' are not
  38. part of the message format and are used here to indicate the
  39. different parts of the message, thus:
  40. m: MIME headers for the overall message
  41. s: a blank separator line
  42. h: message headers
  43. e: encapsulated MIME object containing the message content
  44. x: MIME security multipart message wrapper
  45. */
  46. /***********************************
  47. * Ragel state machine.
  48. */
  49. %%{
  50. machine tmedia_machine_content_cpim;
  51. # Includes
  52. include tmedia_machine_utils "./ragel/tmedia_machine_utils.rl";
  53. action tag{
  54. tag_start = p;
  55. }
  56. action is_parsing_mime_headers{
  57. parsing_mime_headers = tsk_true;
  58. }
  59. action is_parsing_message_headers{
  60. parsing_mime_headers = tsk_false;
  61. }
  62. action parse_hname{
  63. TSK_PARSER_SET_STRING(hname);
  64. }
  65. action parse_hvalue{
  66. tmedia_content_header_t* header;
  67. TSK_PARSER_SET_STRING(hvalue);
  68. header = tmedia_content_header_create(hname, hvalue);
  69. TSK_FREE(hname); TSK_FREE(hvalue);
  70. if(parsing_mime_headers){
  71. if(!TMEDIA_CONTENT_CPIM(self)->m_headers){
  72. TMEDIA_CONTENT_CPIM(self)->m_headers = tsk_list_create();
  73. }
  74. tsk_list_push_back_data(TMEDIA_CONTENT_CPIM(self)->m_headers, (void**)&header);
  75. }
  76. else{
  77. if(!TMEDIA_CONTENT_CPIM(self)->h_headers){
  78. TMEDIA_CONTENT_CPIM(self)->h_headers = tsk_list_create();
  79. }
  80. tsk_list_push_back_data(TMEDIA_CONTENT_CPIM(self)->h_headers, (void**)&header);
  81. }
  82. }
  83. action parse_e{
  84. int len = (int)(p - tag_start);
  85. if(len && tag_start){
  86. if(TMEDIA_CONTENT_CPIM(self)->e){
  87. TSK_OBJECT_SAFE_FREE(TMEDIA_CONTENT_CPIM(self)->e); \
  88. }
  89. TMEDIA_CONTENT_CPIM(self)->e = tsk_buffer_create(tag_start, len);
  90. }
  91. }
  92. hname = token>tag %parse_hname;
  93. hvalue = any*>tag %parse_hvalue;
  94. Header = hname :>SP*:> ":" SP*<: hvalue :> CRLF;
  95. m = Header+ >is_parsing_mime_headers;
  96. s = CRLF;
  97. h = Header+ >is_parsing_message_headers;
  98. e = any*>tag %parse_e;
  99. # Entry point
  100. main := m s h s e;
  101. }%%
  102. static int tmedia_content_cpim_parse(tmedia_content_t* self, const void* in_data, tsk_size_t in_size)
  103. {
  104. int cs = 0;
  105. const char *p = in_data;
  106. const char *pe = p + in_size;
  107. const char *eof = pe;
  108. const char *tag_start = tsk_null;
  109. char* hname = tsk_null;
  110. char* hvalue = tsk_null;
  111. tsk_bool_t parsing_mime_headers = tsk_true;
  112. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  113. %%write data;
  114. (void)(eof);
  115. (void)(tmedia_machine_content_cpim_first_final);
  116. (void)(tmedia_machine_content_cpim_error);
  117. (void)(tmedia_machine_content_cpim_en_main);
  118. %%write init;
  119. %%write exec;
  120. TSK_RAGEL_DISABLE_WARNINGS_END()
  121. TSK_FREE(hname);
  122. TSK_FREE(hvalue);
  123. if( cs < %%{ write first_final; }%% ){
  124. TSK_DEBUG_ERROR("Failed to parse CPIM content");
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. static tsk_buffer_t* tmedia_content_cpim_get_data(tmedia_content_t* self)
  130. {
  131. tsk_buffer_t* data = tsk_buffer_create_null();
  132. tmedia_content_cpim_t *cpim = TMEDIA_CONTENT_CPIM(self);
  133. const tsk_list_item_t* item;
  134. /*
  135. m: Content-type: Message/CPIM
  136. s:
  137. h: (message-metadata-headers)
  138. s:
  139. e: (encapsulated MIME message-body)
  140. x: MIME security multipart message wrapper
  141. */
  142. if(cpim->m_headers){
  143. tsk_list_foreach(item, cpim->m_headers){
  144. char* hstring = tmedia_content_header_tostring(TMEDIA_CONTENT_HEADER(item->data));
  145. tsk_buffer_append_2(data, TSK_LIST_IS_LAST(cpim->m_headers, item) ? "%s\r\n\r\n" : "%s\r\n", hstring);
  146. TSK_FREE(hstring);
  147. }
  148. }
  149. if(cpim->h_headers){
  150. tsk_list_foreach(item, cpim->h_headers){
  151. char* hstring = tmedia_content_header_tostring(TMEDIA_CONTENT_HEADER(item->data));
  152. tsk_buffer_append_2(data, TSK_LIST_IS_LAST(cpim->h_headers, item) ? "%s\r\n\r\n" : "%s\r\n", hstring);
  153. TSK_FREE(hstring);
  154. }
  155. }
  156. if(cpim->e){
  157. tsk_buffer_append(data, TSK_BUFFER_DATA(cpim->e), TSK_BUFFER_SIZE(cpim->e));
  158. }
  159. if(cpim->x){
  160. tsk_buffer_append(data, TSK_BUFFER_DATA(cpim->x), TSK_BUFFER_SIZE(cpim->x));
  161. }
  162. return data;
  163. }
  164. //=================================================================================================
  165. // object/plugin definitions
  166. //
  167. /* constructor */
  168. static tsk_object_t* tmedia_content_cpim_ctor(tsk_object_t * self, va_list * app)
  169. {
  170. tmedia_content_cpim_t *cpim = self;
  171. if(cpim){
  172. /* init base: called by tmedia_content_create() */
  173. /* init self */
  174. }
  175. return self;
  176. }
  177. /* destructor */
  178. static tsk_object_t* tmedia_content_cpim_dtor(tsk_object_t * self)
  179. {
  180. tmedia_content_cpim_t *cpim = self;
  181. if(cpim){
  182. /* deinit base */
  183. tmedia_content_deinit(TMEDIA_CONTENT(cpim));
  184. /* deinit self */
  185. TSK_OBJECT_SAFE_FREE(cpim->m_headers);
  186. TSK_OBJECT_SAFE_FREE(cpim->h_headers);
  187. TSK_OBJECT_SAFE_FREE(cpim->e);
  188. TSK_OBJECT_SAFE_FREE(cpim->x);
  189. }
  190. return self;
  191. }
  192. /* object definition */
  193. static const tsk_object_def_t tmedia_content_cpim_def_s =
  194. {
  195. sizeof(tmedia_content_cpim_t),
  196. tmedia_content_cpim_ctor,
  197. tmedia_content_cpim_dtor,
  198. tsk_null,
  199. };
  200. /* plugin definition*/
  201. static const tmedia_content_plugin_def_t tmedia_content_cpim_plugin_def_s =
  202. {
  203. &tmedia_content_cpim_def_s,
  204. TMEDIA_CONTENT_CPIM_TYPE,
  205. tmedia_content_cpim_parse,
  206. tmedia_content_cpim_get_data
  207. };
  208. const tmedia_content_plugin_def_t *tmedia_content_cpim_plugin_def_t = &tmedia_content_cpim_plugin_def_s;