http_parser.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. /* Also update SONAME in the Makefile whenever you change these. */
  27. #define HTTP_PARSER_VERSION_MAJOR 2
  28. #define HTTP_PARSER_VERSION_MINOR 9
  29. #define HTTP_PARSER_VERSION_PATCH 4
  30. #include <stddef.h>
  31. #if defined(_WIN32) && !defined(__MINGW32__) && \
  32. (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
  33. #include <BaseTsd.h>
  34. typedef __int8 int8_t;
  35. typedef unsigned __int8 uint8_t;
  36. typedef __int16 int16_t;
  37. typedef unsigned __int16 uint16_t;
  38. typedef __int32 int32_t;
  39. typedef unsigned __int32 uint32_t;
  40. typedef __int64 int64_t;
  41. typedef unsigned __int64 uint64_t;
  42. #else
  43. #include <stdint.h>
  44. #endif
  45. /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
  46. * faster
  47. */
  48. #ifndef HTTP_PARSER_STRICT
  49. # define HTTP_PARSER_STRICT 1
  50. #endif
  51. /* Maximium header size allowed. If the macro is not defined
  52. * before including this header then the default is used. To
  53. * change the maximum header size, define the macro in the build
  54. * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
  55. * the effective limit on the size of the header, define the macro
  56. * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
  57. */
  58. #ifndef HTTP_MAX_HEADER_SIZE
  59. # define HTTP_MAX_HEADER_SIZE (80*1024)
  60. #endif
  61. typedef struct http_parser http_parser;
  62. typedef struct http_parser_settings http_parser_settings;
  63. /* Callbacks should return non-zero to indicate an error. The parser will
  64. * then halt execution.
  65. *
  66. * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
  67. * returning '1' from on_headers_complete will tell the parser that it
  68. * should not expect a body. This is used when receiving a response to a
  69. * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
  70. * chunked' headers that indicate the presence of a body.
  71. *
  72. * Returning `2` from on_headers_complete will tell parser that it should not
  73. * expect neither a body nor any futher responses on this connection. This is
  74. * useful for handling responses to a CONNECT request which may not contain
  75. * `Upgrade` or `Connection: upgrade` headers.
  76. *
  77. * http_data_cb does not return data chunks. It will be called arbitrarily
  78. * many times for each string. E.G. you might get 10 callbacks for "on_url"
  79. * each providing just a few characters more data.
  80. */
  81. typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
  82. typedef int (*http_cb) (http_parser*);
  83. /* Status Codes */
  84. #define HTTP_STATUS_MAP(XX) \
  85. XX(100, CONTINUE, Continue) \
  86. XX(101, SWITCHING_PROTOCOLS, Switching Protocols) \
  87. XX(102, PROCESSING, Processing) \
  88. XX(200, OK, OK) \
  89. XX(201, CREATED, Created) \
  90. XX(202, ACCEPTED, Accepted) \
  91. XX(203, NON_AUTHORITATIVE_INFORMATION, Non-Authoritative Information) \
  92. XX(204, NO_CONTENT, No Content) \
  93. XX(205, RESET_CONTENT, Reset Content) \
  94. XX(206, PARTIAL_CONTENT, Partial Content) \
  95. XX(207, MULTI_STATUS, Multi-Status) \
  96. XX(208, ALREADY_REPORTED, Already Reported) \
  97. XX(226, IM_USED, IM Used) \
  98. XX(300, MULTIPLE_CHOICES, Multiple Choices) \
  99. XX(301, MOVED_PERMANENTLY, Moved Permanently) \
  100. XX(302, FOUND, Found) \
  101. XX(303, SEE_OTHER, See Other) \
  102. XX(304, NOT_MODIFIED, Not Modified) \
  103. XX(305, USE_PROXY, Use Proxy) \
  104. XX(307, TEMPORARY_REDIRECT, Temporary Redirect) \
  105. XX(308, PERMANENT_REDIRECT, Permanent Redirect) \
  106. XX(400, BAD_REQUEST, Bad Request) \
  107. XX(401, UNAUTHORIZED, Unauthorized) \
  108. XX(402, PAYMENT_REQUIRED, Payment Required) \
  109. XX(403, FORBIDDEN, Forbidden) \
  110. XX(404, NOT_FOUND, Not Found) \
  111. XX(405, METHOD_NOT_ALLOWED, Method Not Allowed) \
  112. XX(406, NOT_ACCEPTABLE, Not Acceptable) \
  113. XX(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) \
  114. XX(408, REQUEST_TIMEOUT, Request Timeout) \
  115. XX(409, CONFLICT, Conflict) \
  116. XX(410, GONE, Gone) \
  117. XX(411, LENGTH_REQUIRED, Length Required) \
  118. XX(412, PRECONDITION_FAILED, Precondition Failed) \
  119. XX(413, PAYLOAD_TOO_LARGE, Payload Too Large) \
  120. XX(414, URI_TOO_LONG, URI Too Long) \
  121. XX(415, UNSUPPORTED_MEDIA_TYPE, Unsupported Media Type) \
  122. XX(416, RANGE_NOT_SATISFIABLE, Range Not Satisfiable) \
  123. XX(417, EXPECTATION_FAILED, Expectation Failed) \
  124. XX(421, MISDIRECTED_REQUEST, Misdirected Request) \
  125. XX(422, UNPROCESSABLE_ENTITY, Unprocessable Entity) \
  126. XX(423, LOCKED, Locked) \
  127. XX(424, FAILED_DEPENDENCY, Failed Dependency) \
  128. XX(426, UPGRADE_REQUIRED, Upgrade Required) \
  129. XX(428, PRECONDITION_REQUIRED, Precondition Required) \
  130. XX(429, TOO_MANY_REQUESTS, Too Many Requests) \
  131. XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \
  132. XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, Unavailable For Legal Reasons) \
  133. XX(500, INTERNAL_SERVER_ERROR, Internal Server Error) \
  134. XX(501, NOT_IMPLEMENTED, Not Implemented) \
  135. XX(502, BAD_GATEWAY, Bad Gateway) \
  136. XX(503, SERVICE_UNAVAILABLE, Service Unavailable) \
  137. XX(504, GATEWAY_TIMEOUT, Gateway Timeout) \
  138. XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) \
  139. XX(506, VARIANT_ALSO_NEGOTIATES, Variant Also Negotiates) \
  140. XX(507, INSUFFICIENT_STORAGE, Insufficient Storage) \
  141. XX(508, LOOP_DETECTED, Loop Detected) \
  142. XX(510, NOT_EXTENDED, Not Extended) \
  143. XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \
  144. enum http_status
  145. {
  146. #define XX(num, name, string) HTTP_STATUS_##name = num,
  147. HTTP_STATUS_MAP(XX)
  148. #undef XX
  149. };
  150. /* Request Methods */
  151. #define HTTP_METHOD_MAP(XX) \
  152. XX(0, DELETE, DELETE) \
  153. XX(1, GET, GET) \
  154. XX(2, HEAD, HEAD) \
  155. XX(3, POST, POST) \
  156. XX(4, PUT, PUT) \
  157. /* pathological */ \
  158. XX(5, CONNECT, CONNECT) \
  159. XX(6, OPTIONS, OPTIONS) \
  160. XX(7, TRACE, TRACE) \
  161. /* WebDAV */ \
  162. XX(8, COPY, COPY) \
  163. XX(9, LOCK, LOCK) \
  164. XX(10, MKCOL, MKCOL) \
  165. XX(11, MOVE, MOVE) \
  166. XX(12, PROPFIND, PROPFIND) \
  167. XX(13, PROPPATCH, PROPPATCH) \
  168. XX(14, SEARCH, SEARCH) \
  169. XX(15, UNLOCK, UNLOCK) \
  170. XX(16, BIND, BIND) \
  171. XX(17, REBIND, REBIND) \
  172. XX(18, UNBIND, UNBIND) \
  173. XX(19, ACL, ACL) \
  174. /* subversion */ \
  175. XX(20, REPORT, REPORT) \
  176. XX(21, MKACTIVITY, MKACTIVITY) \
  177. XX(22, CHECKOUT, CHECKOUT) \
  178. XX(23, MERGE, MERGE) \
  179. /* upnp */ \
  180. XX(24, MSEARCH, M-SEARCH) \
  181. XX(25, NOTIFY, NOTIFY) \
  182. XX(26, SUBSCRIBE, SUBSCRIBE) \
  183. XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
  184. /* RFC-5789 */ \
  185. XX(28, PATCH, PATCH) \
  186. XX(29, PURGE, PURGE) \
  187. /* CalDAV */ \
  188. XX(30, MKCALENDAR, MKCALENDAR) \
  189. /* RFC-2068, section 19.6.1.2 */ \
  190. XX(31, LINK, LINK) \
  191. XX(32, UNLINK, UNLINK) \
  192. /* icecast */ \
  193. XX(33, SOURCE, SOURCE) \
  194. enum http_method
  195. {
  196. #define XX(num, name, string) HTTP_##name = num,
  197. HTTP_METHOD_MAP(XX)
  198. #undef XX
  199. };
  200. enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  201. /* Flag values for http_parser.flags field */
  202. enum flags
  203. { F_CHUNKED = 1 << 0
  204. , F_CONNECTION_KEEP_ALIVE = 1 << 1
  205. , F_CONNECTION_CLOSE = 1 << 2
  206. , F_CONNECTION_UPGRADE = 1 << 3
  207. , F_TRAILING = 1 << 4
  208. , F_UPGRADE = 1 << 5
  209. , F_SKIPBODY = 1 << 6
  210. , F_CONTENTLENGTH = 1 << 7
  211. , F_TRANSFER_ENCODING = 1 << 8 /* Never set in http_parser.flags */
  212. };
  213. /* Map for errno-related constants
  214. *
  215. * The provided argument should be a macro that takes 2 arguments.
  216. */
  217. #define HTTP_ERRNO_MAP(XX) \
  218. /* No error */ \
  219. XX(OK, "success") \
  220. \
  221. /* Callback-related errors */ \
  222. XX(CB_message_begin, "the on_message_begin callback failed") \
  223. XX(CB_url, "the on_url callback failed") \
  224. XX(CB_header_field, "the on_header_field callback failed") \
  225. XX(CB_header_value, "the on_header_value callback failed") \
  226. XX(CB_headers_complete, "the on_headers_complete callback failed") \
  227. XX(CB_body, "the on_body callback failed") \
  228. XX(CB_message_complete, "the on_message_complete callback failed") \
  229. XX(CB_status, "the on_status callback failed") \
  230. XX(CB_chunk_header, "the on_chunk_header callback failed") \
  231. XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
  232. \
  233. /* Parsing-related errors */ \
  234. XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
  235. XX(HEADER_OVERFLOW, \
  236. "too many header bytes seen; overflow detected") \
  237. XX(CLOSED_CONNECTION, \
  238. "data received after completed connection: close message") \
  239. XX(INVALID_VERSION, "invalid HTTP version") \
  240. XX(INVALID_STATUS, "invalid HTTP status code") \
  241. XX(INVALID_METHOD, "invalid HTTP method") \
  242. XX(INVALID_URL, "invalid URL") \
  243. XX(INVALID_HOST, "invalid host") \
  244. XX(INVALID_PORT, "invalid port") \
  245. XX(INVALID_PATH, "invalid path") \
  246. XX(INVALID_QUERY_STRING, "invalid query string") \
  247. XX(INVALID_FRAGMENT, "invalid fragment") \
  248. XX(LF_EXPECTED, "LF character expected") \
  249. XX(INVALID_HEADER_TOKEN, "invalid character in header") \
  250. XX(INVALID_CONTENT_LENGTH, \
  251. "invalid character in content-length header") \
  252. XX(UNEXPECTED_CONTENT_LENGTH, \
  253. "unexpected content-length header") \
  254. XX(INVALID_CHUNK_SIZE, \
  255. "invalid character in chunk size header") \
  256. XX(INVALID_CONSTANT, "invalid constant string") \
  257. XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
  258. XX(STRICT, "strict mode assertion failed") \
  259. XX(PAUSED, "parser is paused") \
  260. XX(UNKNOWN, "an unknown error occurred") \
  261. XX(INVALID_TRANSFER_ENCODING, \
  262. "request has invalid transfer-encoding") \
  263. /* Define HPE_* values for each errno value above */
  264. #define HTTP_ERRNO_GEN(n, s) HPE_##n,
  265. enum http_errno {
  266. HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
  267. };
  268. #undef HTTP_ERRNO_GEN
  269. /* Get an http_errno value from an http_parser */
  270. #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
  271. struct http_parser {
  272. /** PRIVATE **/
  273. unsigned int type : 2; /* enum http_parser_type */
  274. unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
  275. unsigned int state : 7; /* enum state from http_parser.c */
  276. unsigned int header_state : 7; /* enum header_state from http_parser.c */
  277. unsigned int index : 5; /* index into current matcher */
  278. unsigned int extra_flags : 2;
  279. unsigned int lenient_http_headers : 1;
  280. uint32_t nread; /* # bytes read in various scenarios */
  281. uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
  282. /** READ-ONLY **/
  283. unsigned short http_major;
  284. unsigned short http_minor;
  285. unsigned int status_code : 16; /* responses only */
  286. unsigned int method : 8; /* requests only */
  287. unsigned int http_errno : 7;
  288. /* 1 = Upgrade header was present and the parser has exited because of that.
  289. * 0 = No upgrade header present.
  290. * Should be checked when http_parser_execute() returns in addition to
  291. * error checking.
  292. */
  293. unsigned int upgrade : 1;
  294. /** PUBLIC **/
  295. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  296. };
  297. struct http_parser_settings {
  298. http_cb on_message_begin;
  299. http_data_cb on_url;
  300. http_data_cb on_status;
  301. http_data_cb on_header_field;
  302. http_data_cb on_header_value;
  303. http_cb on_headers_complete;
  304. http_data_cb on_body;
  305. http_cb on_message_complete;
  306. /* When on_chunk_header is called, the current chunk length is stored
  307. * in parser->content_length.
  308. */
  309. http_cb on_chunk_header;
  310. http_cb on_chunk_complete;
  311. };
  312. enum http_parser_url_fields
  313. { UF_SCHEMA = 0
  314. , UF_HOST = 1
  315. , UF_PORT = 2
  316. , UF_PATH = 3
  317. , UF_QUERY = 4
  318. , UF_FRAGMENT = 5
  319. , UF_USERINFO = 6
  320. , UF_MAX = 7
  321. };
  322. /* Result structure for http_parser_parse_url().
  323. *
  324. * Callers should index into field_data[] with UF_* values iff field_set
  325. * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
  326. * because we probably have padding left over), we convert any port to
  327. * a uint16_t.
  328. */
  329. struct http_parser_url {
  330. uint16_t field_set; /* Bitmask of (1 << UF_*) values */
  331. uint16_t port; /* Converted UF_PORT string */
  332. struct {
  333. uint32_t off; /* Offset into buffer in which field starts */
  334. uint32_t len; /* Length of run in buffer */
  335. } field_data[UF_MAX];
  336. };
  337. /* Returns the library version. Bits 16-23 contain the major version number,
  338. * bits 8-15 the minor version number and bits 0-7 the patch level.
  339. * Usage example:
  340. *
  341. * unsigned long version = http_parser_version();
  342. * unsigned major = (version >> 16) & 255;
  343. * unsigned minor = (version >> 8) & 255;
  344. * unsigned patch = version & 255;
  345. * printf("http_parser v%u.%u.%u\n", major, minor, patch);
  346. */
  347. unsigned long http_parser_version(void);
  348. void http_parser_init(http_parser *parser, enum http_parser_type type);
  349. /* Initialize http_parser_settings members to 0
  350. */
  351. void http_parser_settings_init(http_parser_settings *settings);
  352. /* Executes the parser. Returns number of parsed bytes. Sets
  353. * `parser->http_errno` on error. */
  354. size_t http_parser_execute(http_parser *parser,
  355. const http_parser_settings *settings,
  356. const char *data,
  357. size_t len);
  358. /* If http_should_keep_alive() in the on_headers_complete or
  359. * on_message_complete callback returns 0, then this should be
  360. * the last message on the connection.
  361. * If you are the server, respond with the "Connection: close" header.
  362. * If you are the client, close the connection.
  363. */
  364. int http_should_keep_alive(const http_parser *parser);
  365. /* Returns a string version of the HTTP method. */
  366. const char *http_method_str(enum http_method m);
  367. /* Returns a string version of the HTTP status code. */
  368. const char *http_status_str(enum http_status s);
  369. /* Return a string name of the given error */
  370. const char *http_errno_name(enum http_errno err);
  371. /* Return a string description of the given error */
  372. const char *http_errno_description(enum http_errno err);
  373. /* Initialize all http_parser_url members to 0 */
  374. void http_parser_url_init(struct http_parser_url *u);
  375. /* Parse a URL; return nonzero on failure */
  376. int http_parser_parse_url(const char *buf, size_t buflen,
  377. int is_connect,
  378. struct http_parser_url *u);
  379. /* Pause or un-pause the parser; a nonzero value pauses */
  380. void http_parser_pause(http_parser *parser, int paused);
  381. /* Checks if this is the final chunk of the body. */
  382. int http_body_is_final(const http_parser *parser);
  383. /* Change the maximum header size provided at compile time. */
  384. void http_parser_set_max_header_size(uint32_t size);
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif