httpparser.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*******************************************************************************
  2. *
  3. * Copyright (c) 2000-2003 Intel Corporation
  4. * All rights reserved.
  5. * Copyright (c) 2012 France Telecom All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * - Neither name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  27. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************/
  32. #ifndef GENLIB_NET_HTTP_HTTPPARSER_H
  33. #define GENLIB_NET_HTTP_HTTPPARSER_H
  34. /*!
  35. * \file
  36. */
  37. #include "LinkedList.h"
  38. #include "membuffer.h"
  39. #include "uri.h"
  40. #include "upnputil.h"
  41. /* private types */
  42. /* scanner */
  43. /* Used to represent different types of tokens in input. */
  44. typedef enum
  45. {
  46. TT_IDENTIFIER,
  47. TT_WHITESPACE,
  48. TT_CRLF,
  49. TT_CTRL,
  50. TT_SEPARATOR,
  51. TT_QUOTEDSTRING,
  52. } token_type_t;
  53. typedef struct
  54. {
  55. /*! raw http msg. */
  56. membuffer* msg;
  57. /*! current position in buffer. */
  58. size_t cursor;
  59. /*! set this to TRUE if the entire msg is loaded in 'msg';
  60. * else FALSE if only partial msg in 'msg' (default is FALSE). */
  61. int entire_msg_loaded;
  62. } scanner_t;
  63. typedef enum
  64. {
  65. POS_REQUEST_LINE,
  66. POS_RESPONSE_LINE,
  67. POS_HEADERS,
  68. POS_ENTITY,
  69. POS_COMPLETE,
  70. } parser_pos_t;
  71. #define ENTREAD_DETERMINE_READ_METHOD 1
  72. #define ENTREAD_USING_CLEN 2
  73. #define ENTREAD_USING_CHUNKED 3
  74. #define ENTREAD_UNTIL_CLOSE 4
  75. #define ENTREAD_CHUNKY_BODY 5
  76. #define ENTREAD_CHUNKY_HEADERS 6
  77. /* end of private section. */
  78. /* method in a HTTP request. */
  79. typedef enum
  80. {
  81. HTTPMETHOD_POST,
  82. HTTPMETHOD_MPOST,
  83. HTTPMETHOD_SUBSCRIBE,
  84. HTTPMETHOD_UNSUBSCRIBE,
  85. HTTPMETHOD_NOTIFY,
  86. HTTPMETHOD_GET,
  87. HTTPMETHOD_HEAD,
  88. HTTPMETHOD_MSEARCH,
  89. HTTPMETHOD_UNKNOWN,
  90. SOAPMETHOD_POST,
  91. HTTPMETHOD_SIMPLEGET
  92. } http_method_t;
  93. /* different types of HTTP headers */
  94. #define HDR_UNKNOWN -1
  95. #define HDR_CACHE_CONTROL 1
  96. #define HDR_CALLBACK 2
  97. #define HDR_CONTENT_LENGTH 3
  98. #define HDR_CONTENT_TYPE 4
  99. #define HDR_DATE 5
  100. #define HDR_EXT 6
  101. #define HDR_HOST 7
  102. /*define HDR_IF_MODIFIED_SINCE 8 */
  103. /*define HDR_IF_UNMODIFIED_SINCE 9 */
  104. /*define HDR_LAST_MODIFIED 10 */
  105. #define HDR_LOCATION 11
  106. #define HDR_MAN 12
  107. #define HDR_MX 13
  108. #define HDR_NT 14
  109. #define HDR_NTS 15
  110. #define HDR_SERVER 16
  111. #define HDR_SEQ 17
  112. #define HDR_SID 18
  113. #define HDR_SOAPACTION 19
  114. #define HDR_ST 20
  115. #define HDR_TIMEOUT 21
  116. #define HDR_TRANSFER_ENCODING 22
  117. #define HDR_USN 23
  118. #define HDR_USER_AGENT 24
  119. /* Adding new header difinition */
  120. #define HDR_ACCEPT 25
  121. #define HDR_ACCEPT_ENCODING 26
  122. #define HDR_ACCEPT_CHARSET 27
  123. #define HDR_ACCEPT_LANGUAGE 28
  124. #define HDR_ACCEPT_RANGE 29
  125. #define HDR_CONTENT_ENCODING 30
  126. #define HDR_CONTENT_LANGUAGE 31
  127. #define HDR_CONTENT_LOCATION 32
  128. #define HDR_CONTENT_RANGE 33
  129. #define HDR_IF_RANGE 34
  130. #define HDR_RANGE 35
  131. #define HDR_TE 36
  132. /*! status of parsing */
  133. typedef enum {
  134. /*! msg was parsed successfully. */
  135. PARSE_SUCCESS = 0,
  136. /*! need more data to continue. */
  137. PARSE_INCOMPLETE,
  138. /*! for responses that don't have length specified. */
  139. PARSE_INCOMPLETE_ENTITY,
  140. /*! parse failed; check status code for details. */
  141. PARSE_FAILURE,
  142. /*! done partial. */
  143. PARSE_OK,
  144. /*! token not matched. */
  145. PARSE_NO_MATCH,
  146. /*! private. */
  147. PARSE_CONTINUE_1
  148. } parse_status_t;
  149. typedef struct {
  150. /*! header name as a string. */
  151. memptr name;
  152. /*! header name id (for a selective group of headers only). */
  153. int name_id;
  154. /*! raw-value; could be multi-lined; min-length = 0. */
  155. membuffer value;
  156. /* private. */
  157. membuffer name_buf;
  158. } http_header_t;
  159. typedef struct {
  160. int initialized;
  161. /*! request only. */
  162. http_method_t method;
  163. /*! request only. */
  164. uri_type uri;
  165. /*! response only. */
  166. http_method_t request_method;
  167. /*! response only. */
  168. int status_code;
  169. /*! response only. */
  170. membuffer status_msg;
  171. /*! response only. the amount of data that's been read by the user, that's no
  172. * longer in the raw message buffer.
  173. */
  174. size_t amount_discarded;
  175. /* fields used in both request or response messages. */
  176. /*! if TRUE, msg is a request, else response. */
  177. int is_request;
  178. /* http major version. */
  179. int major_version;
  180. /* http minor version. */
  181. int minor_version;
  182. /*! . */
  183. LinkedList headers;
  184. /*! message body(entity). */
  185. memptr entity;
  186. /* private fields. */
  187. /*! entire raw message. */
  188. membuffer msg;
  189. /*! storage for url string. */
  190. char *urlbuf;
  191. } http_message_t;
  192. typedef struct {
  193. http_message_t msg;
  194. /*! read-only; in case of parse error, this
  195. * contains the HTTP error code (4XX or 5XX). */
  196. int http_error_code;
  197. /*! read-only; this is set to true if a NOTIFY request has no
  198. * content-length. used to read valid sspd notify msg. */
  199. int valid_ssdp_notify_hack;
  200. /* private data -- don't touch. */
  201. parser_pos_t position;
  202. int ent_position;
  203. unsigned int content_length;
  204. size_t chunk_size;
  205. /*! offset in the the raw message buffer, which contains the message body.
  206. * preceding this are the headers of the messsage. */
  207. size_t entity_start_position;
  208. scanner_t scanner;
  209. } http_parser_t;
  210. #ifdef __cplusplus
  211. extern "C" {
  212. #endif /* __cplusplus */
  213. /************************************************************************
  214. * Function : httpmsg_init
  215. *
  216. * Parameters :
  217. * INOUT http_message_t* msg ; HTTP Message Object
  218. *
  219. * Description : Initialize and allocate memory for http message
  220. *
  221. * Return : void ;
  222. *
  223. * Note :
  224. ************************************************************************/
  225. void httpmsg_init( INOUT http_message_t* msg );
  226. /************************************************************************
  227. * Function : httpmsg_destroy
  228. *
  229. * Parameters :
  230. * INOUT http_message_t* msg ; HTTP Message Object
  231. *
  232. * Description : Free memory allocated for the http message
  233. *
  234. * Return : void ;
  235. *
  236. * Note :
  237. ************************************************************************/
  238. void httpmsg_destroy( INOUT http_message_t* msg );
  239. /************************************************************************
  240. * Function : httpmsg_find_hdr_str
  241. *
  242. * Parameters :
  243. * IN http_message_t* msg ; HTTP Message Object
  244. * IN const char* header_name ; Header name to be compared with
  245. *
  246. * Description : Compares the header name with the header names stored
  247. * in the linked list of messages
  248. *
  249. * Return : http_header_t* - Pointer to a header on success;
  250. * NULL on failure
  251. * Note :
  252. ************************************************************************/
  253. http_header_t* httpmsg_find_hdr_str( IN http_message_t* msg,
  254. IN const char* header_name );
  255. /************************************************************************
  256. * Function : httpmsg_find_hdr
  257. *
  258. * Parameters :
  259. * IN http_message_t* msg ; HTTP Message Object
  260. * IN int header_name_id ; Header Name ID to be compared with
  261. * OUT memptr* value ; Buffer to get the ouput to.
  262. *
  263. * Description : Finds header from a list, with the given 'name_id'.
  264. *
  265. * Return : http_header_t* - Pointer to a header on success;
  266. * NULL on failure
  267. *
  268. * Note :
  269. ************************************************************************/
  270. http_header_t* httpmsg_find_hdr( IN http_message_t* msg,
  271. IN int header_name_id, OUT memptr* value );
  272. /************************************************************************
  273. * Function: parser_request_init
  274. *
  275. * Parameters:
  276. * OUT http_parser_t* parser ; HTTP Parser object
  277. *
  278. * Description: Initializes parser object for a request
  279. *
  280. * Returns:
  281. * void
  282. ************************************************************************/
  283. void parser_request_init( OUT http_parser_t* parser );
  284. /************************************************************************
  285. * Function: parser_response_init
  286. *
  287. * Parameters:
  288. * OUT http_parser_t* parser ; HTTP Parser object
  289. * IN http_method_t request_method ; Request method
  290. *
  291. * Description: Initializes parser object for a response
  292. *
  293. * Returns:
  294. * void
  295. ************************************************************************/
  296. void parser_response_init( OUT http_parser_t* parser,
  297. IN http_method_t request_method );
  298. /************************************************************************
  299. * Function: parser_parse
  300. *
  301. * Parameters:
  302. * INOUT http_parser_t* parser ; HTTP Parser object
  303. *
  304. * Description: The parser function. Depending on the position of the
  305. * parser object the actual parsing function is invoked
  306. *
  307. * Returns:
  308. * void
  309. ************************************************************************/
  310. parse_status_t parser_parse(INOUT http_parser_t * parser);
  311. /************************************************************************
  312. * Function: parser_parse_responseline
  313. *
  314. * Parameters:
  315. * INOUT http_parser_t* parser ; HTTP Parser object
  316. *
  317. * Description: Get HTTP Method, URL location and version information.
  318. *
  319. * Returns:
  320. * PARSE_OK
  321. * PARSE_SUCCESS
  322. * PARSE_FAILURE
  323. ************************************************************************/
  324. parse_status_t parser_parse_responseline(INOUT http_parser_t *parser);
  325. /************************************************************************
  326. * Function: parser_parse_headers
  327. *
  328. * Parameters:
  329. * INOUT http_parser_t* parser ; HTTP Parser object
  330. *
  331. * Description: Get HTTP Method, URL location and version information.
  332. *
  333. * Returns:
  334. * PARSE_OK
  335. * PARSE_SUCCESS
  336. * PARSE_FAILURE
  337. ************************************************************************/
  338. parse_status_t parser_parse_headers(INOUT http_parser_t *parser);
  339. /************************************************************************
  340. * Function: parser_parse_entity
  341. *
  342. * Parameters:
  343. * INOUT http_parser_t* parser ; HTTP Parser object
  344. *
  345. * Description: Determines method to read entity
  346. *
  347. * Returns:
  348. * PARSE_OK
  349. * PARSE_FAILURE
  350. * PARSE_COMPLETE -- no more reading to do
  351. ************************************************************************/
  352. parse_status_t parser_parse_entity(INOUT http_parser_t *parser);
  353. /************************************************************************
  354. * Function: parser_get_entity_read_method
  355. *
  356. * Parameters:
  357. * INOUT http_parser_t* parser ; HTTP Parser object
  358. *
  359. * Description: Determines method to read entity
  360. *
  361. * Returns:
  362. * PARSE_OK
  363. * PARSE_FAILURE
  364. * PARSE_COMPLETE -- no more reading to do
  365. ************************************************************************/
  366. parse_status_t parser_get_entity_read_method( INOUT http_parser_t* parser );
  367. /************************************************************************
  368. * Function: parser_append
  369. *
  370. * Parameters:
  371. * INOUT http_parser_t* parser ; HTTP Parser Object
  372. * IN const char* buf ; buffer to be appended to the parser
  373. * buffer
  374. * IN size_t buf_length ; Size of the buffer
  375. *
  376. * Description: The parser function. Depending on the position of the
  377. * parser object the actual parsing function is invoked
  378. *
  379. * Returns:
  380. * void
  381. ************************************************************************/
  382. parse_status_t parser_append( INOUT http_parser_t* parser,
  383. IN const char* buf,
  384. IN size_t buf_length );
  385. /************************************************************************
  386. * Function: matchstr
  387. *
  388. * Parameters:
  389. * IN char *str ; String to be matched
  390. * IN size_t slen ; Length of the string
  391. * IN const char* fmt ; Pattern format
  392. * ...
  393. *
  394. * Description: Matches a variable parameter list with a string
  395. * and takes actions based on the data type specified.
  396. *
  397. * Returns:
  398. * PARSE_OK
  399. * PARSE_NO_MATCH -- failure to match pattern 'fmt'
  400. * PARSE_FAILURE -- 'str' is bad input
  401. ************************************************************************/
  402. parse_status_t matchstr( IN char *str, IN size_t slen, IN const char* fmt, ... );
  403. /************************************************************************
  404. * Function: raw_to_int
  405. *
  406. * Parameters:
  407. * IN memptr* raw_value ; Buffer to be converted
  408. * IN int base ; Base to use for conversion
  409. *
  410. * Description: Converts raw character data to long-integer value
  411. *
  412. * Returns:
  413. * int
  414. ************************************************************************/
  415. int raw_to_int( IN memptr* raw_value, int base );
  416. /************************************************************************
  417. * Function: raw_find_str
  418. *
  419. * Parameters:
  420. * IN memptr* raw_value ; Buffer containg the string
  421. * IN const char* str ; Substring to be found
  422. *
  423. * Description: Find a substring from raw character string buffer
  424. *
  425. * Side effects: raw_value is transformed to lowercase.
  426. *
  427. * Returns:
  428. * int - index at which the substring is found.
  429. ************************************************************************/
  430. int raw_find_str( IN memptr* raw_value, IN const char* str );
  431. /************************************************************************
  432. * Function: method_to_str
  433. *
  434. * Parameters:
  435. * IN http_method_t method ; HTTP method
  436. *
  437. * Description: A wrapper function that maps a method id to a method
  438. * nameConverts a http_method id stored in the HTTP Method
  439. *
  440. * Returns:
  441. * const char* ptr - Ptr to the HTTP Method
  442. ************************************************************************/
  443. const char* method_to_str( IN http_method_t method );
  444. /*!
  445. * \brief Print the HTTP headers.
  446. */
  447. #ifdef DEBUG
  448. void print_http_headers(
  449. /*! [in] HTTP Message object. */
  450. http_message_t *hmsg);
  451. #else
  452. static UPNP_INLINE void print_http_headers(http_message_t *hmsg)
  453. {
  454. return;
  455. hmsg = hmsg;
  456. }
  457. #endif
  458. #ifdef __cplusplus
  459. } /* extern "C" */
  460. #endif /* __cplusplus */
  461. #endif /* GENLIB_NET_HTTP_HTTPPARSER_H */