http_parser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #ifndef http_parser_h
  22. #define http_parser_h
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #pragma warning(push)
  27. #pragma warning(disable: 4005)
  28. /* Also update SONAME in the Makefile whenever you change these. */
  29. #define HTTP_PARSER_VERSION_MAJOR 2
  30. #define HTTP_PARSER_VERSION_MINOR 7
  31. #define HTTP_PARSER_VERSION_PATCH 1
  32. #include <sys/types.h>
  33. #if defined(_WIN32) && !defined(__MINGW32__) && \
  34. (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
  35. #include <BaseTsd.h>
  36. #include <stddef.h>
  37. typedef __int8 int8_t;
  38. typedef unsigned __int8 uint8_t;
  39. typedef __int16 int16_t;
  40. typedef unsigned __int16 uint16_t;
  41. typedef __int32 int32_t;
  42. typedef unsigned __int32 uint32_t;
  43. typedef __int64 int64_t;
  44. typedef unsigned __int64 uint64_t;
  45. #else
  46. #include <stdint.h>
  47. #endif
  48. /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
  49. * faster
  50. */
  51. #ifndef HTTP_PARSER_STRICT
  52. # define HTTP_PARSER_STRICT 1
  53. #endif
  54. /* Maximium header size allowed. If the macro is not defined
  55. * before including this header then the default is used. To
  56. * change the maximum header size, define the macro in the build
  57. * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
  58. * the effective limit on the size of the header, define the macro
  59. * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
  60. */
  61. #ifndef HTTP_MAX_HEADER_SIZE
  62. # define HTTP_MAX_HEADER_SIZE (80*1024)
  63. #endif
  64. typedef struct http_parser http_parser;
  65. typedef struct http_parser_settings http_parser_settings;
  66. /* Callbacks should return non-zero to indicate an error. The parser will
  67. * then halt execution.
  68. *
  69. * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
  70. * returning '1' from on_headers_complete will tell the parser that it
  71. * should not expect a body. This is used when receiving a response to a
  72. * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
  73. * chunked' headers that indicate the presence of a body.
  74. *
  75. * Returning `2` from on_headers_complete will tell parser that it should not
  76. * expect neither a body nor any futher responses on this connection. This is
  77. * useful for handling responses to a CONNECT request which may not contain
  78. * `Upgrade` or `Connection: upgrade` headers.
  79. *
  80. * http_data_cb does not return data chunks. It will be called arbitrarily
  81. * many times for each string. E.G. you might get 10 callbacks for "on_url"
  82. * each providing just a few characters more data.
  83. */
  84. typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
  85. typedef int (*http_cb) (http_parser*);
  86. /* Request Methods */
  87. #define HTTP_METHOD_MAP(XX) \
  88. XX(0, DELETE, DELETE) \
  89. XX(1, GET, GET) \
  90. XX(2, HEAD, HEAD) \
  91. XX(3, POST, POST) \
  92. XX(4, PUT, PUT) \
  93. /* pathological */ \
  94. XX(5, CONNECT, CONNECT) \
  95. XX(6, OPTIONS, OPTIONS) \
  96. XX(7, TRACE, TRACE) \
  97. /* WebDAV */ \
  98. XX(8, COPY, COPY) \
  99. XX(9, LOCK, LOCK) \
  100. XX(10, MKCOL, MKCOL) \
  101. XX(11, MOVE, MOVE) \
  102. XX(12, PROPFIND, PROPFIND) \
  103. XX(13, PROPPATCH, PROPPATCH) \
  104. XX(14, SEARCH, SEARCH) \
  105. XX(15, UNLOCK, UNLOCK) \
  106. XX(16, BIND, BIND) \
  107. XX(17, REBIND, REBIND) \
  108. XX(18, UNBIND, UNBIND) \
  109. XX(19, ACL, ACL) \
  110. /* subversion */ \
  111. XX(20, REPORT, REPORT) \
  112. XX(21, MKACTIVITY, MKACTIVITY) \
  113. XX(22, CHECKOUT, CHECKOUT) \
  114. XX(23, MERGE, MERGE) \
  115. /* upnp */ \
  116. XX(24, MSEARCH, M-SEARCH) \
  117. XX(25, NOTIFY, NOTIFY) \
  118. XX(26, SUBSCRIBE, SUBSCRIBE) \
  119. XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
  120. /* RFC-5789 */ \
  121. XX(28, PATCH, PATCH) \
  122. XX(29, PURGE, PURGE) \
  123. /* CalDAV */ \
  124. XX(30, MKCALENDAR, MKCALENDAR) \
  125. /* RFC-2068, section 19.6.1.2 */ \
  126. XX(31, LINK, LINK) \
  127. XX(32, UNLINK, UNLINK) \
  128. enum http_method
  129. {
  130. #define XX(num, name, string) HTTP_##name = num,
  131. HTTP_METHOD_MAP(XX)
  132. #undef XX
  133. };
  134. enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  135. enum state
  136. { s_dead = 1 /* important that this is > 0 */
  137. , s_start_req_or_res
  138. , s_res_or_resp_H
  139. , s_start_res
  140. , s_res_H = 5
  141. , s_res_HT
  142. , s_res_HTT
  143. , s_res_HTTP
  144. , s_res_first_http_major
  145. , s_res_http_major = 10
  146. , s_res_first_http_minor
  147. , s_res_http_minor
  148. , s_res_first_status_code
  149. , s_res_status_code
  150. , s_res_status_start = 15
  151. , s_res_status
  152. , s_res_line_almost_done
  153. , s_start_req
  154. , s_req_method
  155. , s_req_spaces_before_url = 20
  156. , s_req_schema
  157. , s_req_schema_slash
  158. , s_req_schema_slash_slash
  159. , s_req_server_start
  160. , s_req_server = 25
  161. , s_req_server_with_at
  162. , s_req_path
  163. , s_req_query_string_start
  164. , s_req_query_string
  165. , s_req_fragment_start = 30
  166. , s_req_fragment
  167. , s_req_http_start
  168. , s_req_http_H
  169. , s_req_http_HT
  170. , s_req_http_HTT = 35
  171. , s_req_http_HTTP
  172. , s_req_first_http_major
  173. , s_req_http_major
  174. , s_req_first_http_minor
  175. , s_req_http_minor = 40
  176. , s_req_line_almost_done
  177. , s_header_field_start
  178. , s_header_field
  179. , s_header_value_discard_ws
  180. , s_header_value_discard_ws_almost_done = 45
  181. , s_header_value_discard_lws
  182. , s_header_value_start
  183. , s_header_value
  184. , s_header_value_lws
  185. , s_header_almost_done = 50
  186. , s_chunk_size_start
  187. , s_chunk_size
  188. , s_chunk_parameters
  189. , s_chunk_size_almost_done
  190. , s_headers_almost_done = 55
  191. , s_headers_done
  192. /* Important: 's_headers_done' must be the last 'header' state. All
  193. * states beyond this must be 'body' states. It is used for overflow
  194. * checking. See the PARSING_HEADER() macro.
  195. */
  196. , s_chunk_data
  197. , s_chunk_data_almost_done
  198. , s_chunk_data_done
  199. , s_body_identity = 60
  200. , s_body_identity_eof
  201. , s_message_done
  202. };
  203. /* Flag values for http_parser.flags field */
  204. enum flags
  205. { F_CHUNKED = 1 << 0
  206. , F_CONNECTION_KEEP_ALIVE = 1 << 1
  207. , F_CONNECTION_CLOSE = 1 << 2
  208. , F_CONNECTION_UPGRADE = 1 << 3
  209. , F_TRAILING = 1 << 4
  210. , F_UPGRADE = 1 << 5
  211. , F_SKIPBODY = 1 << 6
  212. , F_CONTENTLENGTH = 1 << 7
  213. };
  214. /* Map for errno-related constants
  215. *
  216. * The provided argument should be a macro that takes 2 arguments.
  217. */
  218. #define HTTP_ERRNO_MAP(XX) \
  219. /* No error */ \
  220. XX(OK, "success") \
  221. \
  222. /* Callback-related errors */ \
  223. XX(CB_message_begin, "the on_message_begin callback failed") \
  224. XX(CB_url, "the on_url callback failed") \
  225. XX(CB_header_field, "the on_header_field callback failed") \
  226. XX(CB_header_value, "the on_header_value callback failed") \
  227. XX(CB_headers_complete, "the on_headers_complete callback failed") \
  228. XX(CB_body, "the on_body callback failed") \
  229. XX(CB_message_complete, "the on_message_complete callback failed") \
  230. XX(CB_status, "the on_status callback failed") \
  231. XX(CB_chunk_header, "the on_chunk_header callback failed") \
  232. XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
  233. \
  234. /* Parsing-related errors */ \
  235. XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
  236. XX(HEADER_OVERFLOW, \
  237. "too many header bytes seen; overflow detected") \
  238. XX(CLOSED_CONNECTION, \
  239. "data received after completed connection: close message") \
  240. XX(INVALID_VERSION, "invalid HTTP version") \
  241. XX(INVALID_STATUS, "invalid HTTP status code") \
  242. XX(INVALID_METHOD, "invalid HTTP method") \
  243. XX(INVALID_URL, "invalid URL") \
  244. XX(INVALID_HOST, "invalid host") \
  245. XX(INVALID_PORT, "invalid port") \
  246. XX(INVALID_PATH, "invalid path") \
  247. XX(INVALID_QUERY_STRING, "invalid query string") \
  248. XX(INVALID_FRAGMENT, "invalid fragment") \
  249. XX(LF_EXPECTED, "LF character expected") \
  250. XX(INVALID_HEADER_TOKEN, "invalid character in header") \
  251. XX(INVALID_CONTENT_LENGTH, \
  252. "invalid character in content-length header") \
  253. XX(UNEXPECTED_CONTENT_LENGTH, \
  254. "unexpected content-length header") \
  255. XX(INVALID_CHUNK_SIZE, \
  256. "invalid character in chunk size header") \
  257. XX(INVALID_CONSTANT, "invalid constant string") \
  258. XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
  259. XX(STRICT, "strict mode assertion failed") \
  260. XX(PAUSED, "parser is paused") \
  261. XX(UNKNOWN, "an unknown error occurred")
  262. /* Define HPE_* values for each errno value above */
  263. #define HTTP_ERRNO_GEN(n, s) HPE_##n,
  264. enum http_errno {
  265. HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
  266. };
  267. #undef HTTP_ERRNO_GEN
  268. /* Get an http_errno value from an http_parser */
  269. #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
  270. struct http_parser {
  271. /** PRIVATE **/
  272. unsigned int type : 2; /* enum http_parser_type */
  273. unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
  274. unsigned int state : 7; /* enum state from http_parser.c */
  275. unsigned int header_state : 7; /* enum header_state from http_parser.c */
  276. unsigned int index : 7; /* index into current matcher */
  277. unsigned int lenient_http_headers : 1;
  278. uint32_t nread; /* # bytes read in various scenarios */
  279. uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
  280. /** READ-ONLY **/
  281. unsigned short http_major;
  282. unsigned short http_minor;
  283. unsigned int status_code : 16; /* responses only */
  284. unsigned int method : 8; /* requests only */
  285. unsigned int http_errno : 7;
  286. /* 1 = Upgrade header was present and the parser has exited because of that.
  287. * 0 = No upgrade header present.
  288. * Should be checked when http_parser_execute() returns in addition to
  289. * error checking.
  290. */
  291. unsigned int upgrade : 1;
  292. /** PUBLIC **/
  293. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  294. };
  295. struct http_parser_settings {
  296. http_cb on_message_begin;
  297. http_data_cb on_url;
  298. http_data_cb on_status;
  299. http_data_cb on_header_field;
  300. http_data_cb on_header_value;
  301. http_cb on_headers_complete;
  302. http_data_cb on_body;
  303. http_cb on_message_complete;
  304. /* When on_chunk_header is called, the current chunk length is stored
  305. * in parser->content_length.
  306. */
  307. http_cb on_chunk_header;
  308. http_cb on_chunk_complete;
  309. };
  310. enum http_parser_url_fields
  311. { UF_SCHEMA = 0
  312. , UF_HOST = 1
  313. , UF_PORT = 2
  314. , UF_PATH = 3
  315. , UF_QUERY = 4
  316. , UF_FRAGMENT = 5
  317. , UF_USERINFO = 6
  318. , UF_MAX = 7
  319. };
  320. /* Result structure for http_parser_parse_url().
  321. *
  322. * Callers should index into field_data[] with UF_* values iff field_set
  323. * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
  324. * because we probably have padding left over), we convert any port to
  325. * a uint16_t.
  326. */
  327. struct http_parser_url {
  328. uint16_t field_set; /* Bitmask of (1 << UF_*) values */
  329. uint16_t port; /* Converted UF_PORT string */
  330. struct {
  331. uint16_t off; /* Offset into buffer in which field starts */
  332. uint16_t len; /* Length of run in buffer */
  333. } field_data[UF_MAX];
  334. };
  335. /* Returns the library version. Bits 16-23 contain the major version number,
  336. * bits 8-15 the minor version number and bits 0-7 the patch level.
  337. * Usage example:
  338. *
  339. * unsigned long version = http_parser_version();
  340. * unsigned major = (version >> 16) & 255;
  341. * unsigned minor = (version >> 8) & 255;
  342. * unsigned patch = version & 255;
  343. * printf("http_parser v%u.%u.%u\n", major, minor, patch);
  344. */
  345. unsigned long http_parser_version(void);
  346. void http_parser_init(http_parser *parser, enum http_parser_type type);
  347. /* Initialize http_parser_settings members to 0
  348. */
  349. void http_parser_settings_init(http_parser_settings *settings);
  350. /* Executes the parser. Returns number of parsed bytes. Sets
  351. * `parser->http_errno` on error. */
  352. size_t http_parser_execute(http_parser *parser,
  353. const http_parser_settings *settings,
  354. const char *data,
  355. size_t len);
  356. /* If http_should_keep_alive() in the on_headers_complete or
  357. * on_message_complete callback returns 0, then this should be
  358. * the last message on the connection.
  359. * If you are the server, respond with the "Connection: close" header.
  360. * If you are the client, close the connection.
  361. */
  362. int http_should_keep_alive(const http_parser *parser);
  363. /* Returns a string version of the HTTP method. */
  364. const char *http_method_str(enum http_method m);
  365. /* Return a string name of the given error */
  366. const char *http_errno_name(enum http_errno err);
  367. /* Return a string description of the given error */
  368. const char *http_errno_description(enum http_errno err);
  369. /* Initialize all http_parser_url members to 0 */
  370. void http_parser_url_init(struct http_parser_url *u);
  371. /* Parse a URL; return nonzero on failure */
  372. int http_parser_parse_url(const char *buf, size_t buflen,
  373. int is_connect,
  374. struct http_parser_url *u);
  375. /* Pause or un-pause the parser; a nonzero value pauses */
  376. void http_parser_pause(http_parser *parser, int paused);
  377. /* Checks if this is the final chunk of the body. */
  378. int http_body_is_final(const http_parser *parser);
  379. #pragma warning(pop)
  380. #ifdef __cplusplus
  381. }
  382. #endif
  383. #endif