thttp_parser_header.rl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 thttp_parser_header.c
  21. * @brief HTTP headers parser.
  22. */
  23. #include "tinyhttp/parsers/thttp_parser_header.h"
  24. //#include "tinyhttp/headers/thttp_header_Allow.h"
  25. //#include "tinyhttp/headers/thttp_header_Allow_Events.h"
  26. #include "tinyhttp/headers/thttp_header_Authorization.h"
  27. //#include "tinyhttp/headers/thttp_header_Call_ID.h"
  28. //#include "tinyhttp/headers/thttp_header_Contact.h"
  29. //#include "tinyhttp/headers/thttp_header_CSeq.h"
  30. #include "tinyhttp/headers/thttp_header_Dummy.h"
  31. #include "tinyhttp/headers/thttp_header_ETag.h"
  32. //#include "tinyhttp/headers/thttp_header_Expires.h"
  33. //#include "tinyhttp/headers/thttp_header_From.h"
  34. //#include "tinyhttp/headers/thttp_header_Max_Forwards.h"
  35. //#include "tinyhttp/headers/thttp_header_Min_Expires.h"
  36. //#include "tinyhttp/headers/thttp_header_Path.h"
  37. //#include "tinyhttp/headers/thttp_header_P_Access_Network_Info.h"
  38. //#include "tinyhttp/headers/thttp_header_P_Preferred_Identity.h"
  39. //#include "tinyhttp/headers/thttp_header_Privacy.h"
  40. //#include "tinyhttp/headers/thttp_header_Proxy_Authenticate.h"
  41. //#include "tinyhttp/headers/thttp_header_Proxy_Authorization.h"
  42. //#include "tinyhttp/headers/thttp_header_Record_Route.h"
  43. //#include "tinyhttp/headers/thttp_header_Require.h"
  44. //#include "tinyhttp/headers/thttp_header_Route.h"
  45. #include "tinyhttp/headers/thttp_header_Sec_WebSocket_Accept.h"
  46. #include "tinyhttp/headers/thttp_header_Sec_WebSocket_Key.h"
  47. #include "tinyhttp/headers/thttp_header_Sec_WebSocket_Protocol.h"
  48. #include "tinyhttp/headers/thttp_header_Sec_WebSocket_Version.h"
  49. //#include "tinyhttp/headers/thttp_header_Service_Route.h"
  50. //#include "tinyhttp/headers/thttp_header_Supported.h"
  51. #include "tinyhttp/headers/thttp_header_Transfer_Encoding.h"
  52. //#include "tinyhttp/headers/thttp_header_User_Agent.h"
  53. //#include "tinyhttp/headers/thttp_header_Via.h"
  54. #include "tinyhttp/headers/thttp_header_WWW_Authenticate.h"
  55. #include "tsk_debug.h"
  56. #undef ADD_HEADERS
  57. #undef ADD_HEADER
  58. #define ADD_HEADERS(headers)\
  59. if(headers)\
  60. {\
  61. tsk_list_item_t *item;\
  62. tsk_list_foreach(item, headers)\
  63. {\
  64. thttp_header_t *hdr = tsk_object_ref(item->data);\
  65. tsk_list_push_back_data(message->headers, ((void**) &hdr));\
  66. }\
  67. \
  68. TSK_OBJECT_SAFE_FREE(headers);\
  69. }
  70. #define ADD_HEADER(header)\
  71. if(header)\
  72. {\
  73. tsk_list_push_back_data(message->headers, ((void**) &header));\
  74. }
  75. /***********************************
  76. * Ragel state machine.
  77. */
  78. %%{
  79. machine thttp_machine_parser_headers;
  80. # /*== Accept: ==*/
  81. action parse_header_Accept
  82. {
  83. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  84. ADD_HEADER(header);
  85. //TSK_DEBUG_WARN("parse_header_Accept NOT IMPLEMENTED. Will be added as Dummy header.");
  86. }
  87. # /*== Accept_Charset: ==*/
  88. action parse_header_Accept_Charset
  89. {
  90. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  91. ADD_HEADER(header);
  92. //TSK_DEBUG_WARN("parse_header_Accept_Charset NOT IMPLEMENTED. Will be added as Dummy header.");
  93. }
  94. # /*== Accept_Encoding: ==*/
  95. action parse_header_Accept_Encoding
  96. {
  97. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  98. ADD_HEADER(header);
  99. //TSK_DEBUG_WARN("parse_header_Accept_Encoding NOT IMPLEMENTED. Will be added as Dummy header.");
  100. }
  101. # /*== Accept_Language: ==*/
  102. action parse_header_Accept_Language
  103. {
  104. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  105. ADD_HEADER(header);
  106. //TSK_DEBUG_WARN("parse_header_Accept_Language NOT IMPLEMENTED. Will be added as Dummy header.");
  107. }
  108. # /*== Allow: ==*/
  109. action parse_header_Allow
  110. {
  111. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  112. ADD_HEADER(header);
  113. //TSK_DEBUG_WARN("parse_header_Allow NOT IMPLEMENTED. Will be added as Dummy header.");
  114. }
  115. # /*== Authorization: ==*/
  116. action parse_header_Authorization
  117. {
  118. thttp_header_Authorization_t *header = thttp_header_Authorization_parse(state->tag_start, (state->tag_end-state->tag_start));
  119. ADD_HEADER(header);
  120. }
  121. # /*== Cache_Control: ==*/
  122. action parse_header_Cache_Control
  123. {
  124. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  125. ADD_HEADER(header);
  126. //TSK_DEBUG_WARN("parse_header_Cache_Control NOT IMPLEMENTED. Will be added as Dummy header.");
  127. }
  128. # /*== Connection: ==*/
  129. action parse_header_Connection
  130. {
  131. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  132. ADD_HEADER(header);
  133. //TSK_DEBUG_WARN("parse_header_Connection NOT IMPLEMENTED. Will be added as Dummy header.");
  134. }
  135. # /*== Content_Encoding: ==*/
  136. action parse_header_Content_Encoding
  137. {
  138. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  139. ADD_HEADER(header);
  140. //TSK_DEBUG_WARN("parse_header_Content_Encoding NOT IMPLEMENTED. Will be added as Dummy header.");
  141. }
  142. # /*== Content_Language: ==*/
  143. action parse_header_Content_Language
  144. {
  145. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  146. ADD_HEADER(header);
  147. //TSK_DEBUG_WARN("parse_header_Content_Language NOT IMPLEMENTED. Will be added as Dummy header.");
  148. }
  149. # /*== Content_Length: ==*/
  150. action parse_header_Content_Length
  151. {
  152. if(!message->Content_Length){
  153. message->Content_Length = thttp_header_Content_Length_parse(state->tag_start, (state->tag_end-state->tag_start));
  154. }
  155. else{
  156. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  157. ADD_HEADER(header);
  158. //TSK_DEBUG_WARN("The message already have 'Content-Length' header.");
  159. }
  160. }
  161. # /*== Content_Location: ==*/
  162. action parse_header_Content_Location
  163. {
  164. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  165. ADD_HEADER(header);
  166. //TSK_DEBUG_WARN("parse_header_Content_Location NOT IMPLEMENTED. Will be added as Dummy header.");
  167. }
  168. # /*== Content_MD5: ==*/
  169. action parse_header_Content_MD5
  170. {
  171. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  172. ADD_HEADER(header);
  173. //TSK_DEBUG_WARN("parse_header_Content_MD5 NOT IMPLEMENTED. Will be added as Dummy header.");
  174. }
  175. # /*== Content_Range: ==*/
  176. action parse_header_Content_Range
  177. {
  178. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  179. ADD_HEADER(header);
  180. //TSK_DEBUG_WARN("parse_header_Content_Range NOT IMPLEMENTED. Will be added as Dummy header.");
  181. }
  182. # /*== Content_Type: ==*/
  183. action parse_header_Content_Type
  184. {
  185. if(!message->Content_Type){
  186. message->Content_Type = thttp_header_Content_Type_parse(state->tag_start, (state->tag_end-state->tag_start));
  187. }
  188. else{
  189. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  190. ADD_HEADER(header);
  191. //TSK_DEBUG_WARN("The message already have 'Content-Type' header.");
  192. }
  193. }
  194. # /*== Date: ==*/
  195. action parse_header_Date
  196. {
  197. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  198. ADD_HEADER(header);
  199. //TSK_DEBUG_WARN("parse_header_Date NOT IMPLEMENTED. Will be added as Dummy header.");
  200. }
  201. # /*== ETag: ==*/
  202. action parse_header_ETag
  203. {
  204. thttp_header_ETag_t *header = thttp_header_ETag_parse(state->tag_start, (state->tag_end-state->tag_start));
  205. ADD_HEADER(header);
  206. //TSK_DEBUG_WARN("parse_header_Expires NOT IMPLEMENTED. Will be added as Dummy header.");
  207. }
  208. # /*== Expect: ==*/
  209. action parse_header_Expect
  210. {
  211. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  212. ADD_HEADER(header);
  213. //TSK_DEBUG_WARN("parse_header_Expect NOT IMPLEMENTED. Will be added as Dummy header.");
  214. }
  215. # /*== Expires: ==*/
  216. action parse_header_Expires
  217. {
  218. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  219. ADD_HEADER(header);
  220. //TSK_DEBUG_WARN("parse_header_Expires NOT IMPLEMENTED. Will be added as Dummy header.");
  221. }
  222. # /*== From: ==*/
  223. action parse_header_From
  224. {
  225. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  226. ADD_HEADER(header);
  227. //TSK_DEBUG_WARN("parse_header_From NOT IMPLEMENTED. Will be added as Dummy header.");
  228. }
  229. # /*== Host: ==*/
  230. action parse_header_Host
  231. {
  232. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  233. ADD_HEADER(header);
  234. //TSK_DEBUG_WARN("parse_header_Host NOT IMPLEMENTED. Will be added as Dummy header.");
  235. }
  236. # /*== If_Match: ==*/
  237. action parse_header_If_Match
  238. {
  239. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  240. ADD_HEADER(header);
  241. //TSK_DEBUG_WARN("parse_header_If_Match NOT IMPLEMENTED. Will be added as Dummy header.");
  242. }
  243. # /*== If_Modified_Since: ==*/
  244. action parse_header_If_Modified_Since
  245. {
  246. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  247. ADD_HEADER(header);
  248. //TSK_DEBUG_WARN("parse_header_If_Modified_Since NOT IMPLEMENTED. Will be added as Dummy header.");
  249. }
  250. # /*== If_None_Match: ==*/
  251. action parse_header_If_None_Match
  252. {
  253. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  254. ADD_HEADER(header);
  255. //TSK_DEBUG_WARN("parse_header_If_None_Match NOT IMPLEMENTED. Will be added as Dummy header.");
  256. }
  257. # /*== If_Range: ==*/
  258. action parse_header_If_Range
  259. {
  260. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  261. ADD_HEADER(header);
  262. //TSK_DEBUG_WARN("parse_header_If_Range NOT IMPLEMENTED. Will be added as Dummy header.");
  263. }
  264. # /*== If_Unmodified_Since: ==*/
  265. action parse_header_If_Unmodified_Since
  266. {
  267. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  268. ADD_HEADER(header);
  269. //TSK_DEBUG_WARN("parse_header_If_Unmodified_Since NOT IMPLEMENTED. Will be added as Dummy header.");
  270. }
  271. # /*== Last_Modified: ==*/
  272. action parse_header_Last_Modified
  273. {
  274. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  275. ADD_HEADER(header);
  276. //TSK_DEBUG_WARN("parse_header_Last_Modified NOT IMPLEMENTED. Will be added as Dummy header.");
  277. }
  278. # /*== Max_Forwards: ==*/
  279. action parse_header_Max_Forwards
  280. {
  281. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  282. ADD_HEADER(header);
  283. //TSK_DEBUG_WARN("parse_header_Max_Forwards NOT IMPLEMENTED. Will be added as Dummy header.");
  284. }
  285. # /*== Pragma: ==*/
  286. action parse_header_Pragma
  287. {
  288. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  289. ADD_HEADER(header);
  290. //TSK_DEBUG_WARN("parse_header_Pragma NOT IMPLEMENTED. Will be added as Dummy header.");
  291. }
  292. # /*== Proxy_Authenticate: ==*/
  293. action parse_header_Proxy_Authenticate
  294. {
  295. thttp_header_Proxy_Authenticate_t *header = thttp_header_Proxy_Authenticate_parse(state->tag_start, (state->tag_end-state->tag_start));
  296. ADD_HEADER(header);
  297. }
  298. # /*== Proxy_Authorization: ==*/
  299. action parse_header_Proxy_Authorization
  300. {
  301. thttp_header_Proxy_Authorization_t *header = thttp_header_Proxy_Authorization_parse(state->tag_start, (state->tag_end-state->tag_start));
  302. ADD_HEADER(header);
  303. }
  304. # /*== Range: ==*/
  305. action parse_header_Range
  306. {
  307. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  308. ADD_HEADER(header);
  309. //TSK_DEBUG_WARN("parse_header_Range NOT IMPLEMENTED. Will be added as Dummy header.");
  310. }
  311. # /*== Referer: ==*/
  312. action parse_header_Referer
  313. {
  314. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  315. ADD_HEADER(header);
  316. //TSK_DEBUG_WARN("parse_header_Referer NOT IMPLEMENTED. Will be added as Dummy header.");
  317. }
  318. # /* == Sec-WebSocket-Accept == */
  319. action parse_header_Sec_WebSocket_Accept
  320. {
  321. thttp_header_Sec_WebSocket_Accept_t* header = thttp_header_Sec_WebSocket_Accept_parse(state->tag_start, (state->tag_end-state->tag_start));
  322. ADD_HEADER(header);
  323. }
  324. # /* == Sec-WebSocket-Key == */
  325. action parse_header_Sec_WebSocket_Key
  326. {
  327. thttp_header_Sec_WebSocket_Key_t* header = thttp_header_Sec_WebSocket_Key_parse(state->tag_start, (state->tag_end-state->tag_start));
  328. ADD_HEADER(header);
  329. }
  330. # /* == Sec-WebSocket-Protocol == */
  331. action parse_header_Sec_WebSocket_Protocol
  332. {
  333. thttp_header_Sec_WebSocket_Protocol_t* header = thttp_header_Sec_WebSocket_Protocol_parse(state->tag_start, (state->tag_end-state->tag_start));
  334. ADD_HEADER(header);
  335. }
  336. # /* == Sec-WebSocket-Version == */
  337. action parse_header_Sec_WebSocket_Version
  338. {
  339. thttp_header_Sec_WebSocket_Version_t* header = thttp_header_Sec_WebSocket_Version_parse(state->tag_start, (state->tag_end-state->tag_start));
  340. ADD_HEADER(header);
  341. }
  342. # /*== TE: ==*/
  343. action parse_header_TE
  344. {
  345. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  346. ADD_HEADER(header);
  347. //TSK_DEBUG_WARN("parse_header_TE NOT IMPLEMENTED. Will be added as Dummy header.");
  348. }
  349. # /*== Trailer: ==*/
  350. action parse_header_Trailer
  351. {
  352. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  353. ADD_HEADER(header);
  354. //TSK_DEBUG_WARN("parse_header_Trailer NOT IMPLEMENTED. Will be added as Dummy header.");
  355. }
  356. # /*== Transfer_Encoding: ==*/
  357. action parse_header_Transfer_Encoding
  358. {
  359. thttp_header_Transfer_Encoding_t *header = thttp_header_Transfer_Encoding_parse(state->tag_start, (state->tag_end-state->tag_start));
  360. ADD_HEADER(header);
  361. //TSK_DEBUG_WARN("parse_header_Trailer NOT IMPLEMENTED. Will be added as Dummy header.");
  362. }
  363. # /*== Upgrade: ==*/
  364. action parse_header_Upgrade
  365. {
  366. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  367. ADD_HEADER(header);
  368. //TSK_DEBUG_WARN("parse_header_Upgrade NOT IMPLEMENTED. Will be added as Dummy header.");
  369. }
  370. # /*== User_Agent: ==*/
  371. action parse_header_User_Agent
  372. {
  373. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  374. ADD_HEADER(header);
  375. //TSK_DEBUG_WARN("parse_header_User_Agent NOT IMPLEMENTED. Will be added as Dummy header.");
  376. }
  377. # /*== Via: ==*/
  378. action parse_header_Via
  379. {
  380. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  381. ADD_HEADER(header);
  382. //TSK_DEBUG_WARN("parse_header_Via NOT IMPLEMENTED. Will be added as Dummy header.");
  383. }
  384. # /*== Warning: ==*/
  385. action parse_header_Warning
  386. {
  387. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  388. ADD_HEADER(header);
  389. //TSK_DEBUG_WARN("parse_header_Warning NOT IMPLEMENTED. Will be added as Dummy header.");
  390. }
  391. # /*== WWW-Authenticate: ==*/
  392. action parse_header_WWW_Authenticate
  393. {
  394. thttp_header_WWW_Authenticate_t *header = thttp_header_WWW_Authenticate_parse(state->tag_start, (state->tag_end-state->tag_start));
  395. ADD_HEADER(header);
  396. }
  397. # /*== extension_header: ==*/
  398. action parse_header_extension_header
  399. {
  400. thttp_header_Dummy_t *header = thttp_header_Dummy_parse(state->tag_start, (state->tag_end-state->tag_start));
  401. ADD_HEADER(header);
  402. //TSK_DEBUG_WARN("parse_header_extension_header NOT IMPLEMENTED. Will be added as Dummy header.");
  403. }
  404. # Includes
  405. include thttp_machine_utils "./ragel/thttp_machine_utils.rl";
  406. include thttp_machine_header "./ragel/thttp_machine_header.rl";
  407. # Entry point
  408. main := HEADER;
  409. }%%
  410. int thttp_header_parse(tsk_ragel_state_t *state, thttp_message_t *message)
  411. {
  412. int cs = 0;
  413. const char *p = state->tag_start;
  414. const char *pe = state->tag_end;
  415. const char *eof = pe;
  416. TSK_RAGEL_DISABLE_WARNINGS_BEGIN()
  417. %%write data;
  418. (void)(eof);
  419. (void)(thttp_machine_parser_headers_first_final);
  420. (void)(thttp_machine_parser_headers_error);
  421. (void)(thttp_machine_parser_headers_en_main);
  422. %%write init;
  423. %%write exec;
  424. TSK_RAGEL_DISABLE_WARNINGS_END()
  425. return ( cs >= %%{ write first_final; }%% ) ? 0 : -1;
  426. }