http_parser.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "httpdef.h"
  27. /* Also update SONAME in the Makefile whenever you change these. */
  28. #define HTTP_PARSER_VERSION_MAJOR 2
  29. #define HTTP_PARSER_VERSION_MINOR 9
  30. #define HTTP_PARSER_VERSION_PATCH 1
  31. #include <stddef.h>
  32. #if defined(_WIN32) && !defined(__MINGW32__) && \
  33. (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
  34. #include <BaseTsd.h>
  35. typedef __int8 int8_t;
  36. typedef unsigned __int8 uint8_t;
  37. typedef __int16 int16_t;
  38. typedef unsigned __int16 uint16_t;
  39. typedef __int32 int32_t;
  40. typedef unsigned __int32 uint32_t;
  41. typedef __int64 int64_t;
  42. typedef unsigned __int64 uint64_t;
  43. #else
  44. #include <stdint.h>
  45. #endif
  46. /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
  47. * faster
  48. */
  49. #ifndef HTTP_PARSER_STRICT
  50. # define HTTP_PARSER_STRICT 1
  51. #endif
  52. /* Maximium header size allowed. If the macro is not defined
  53. * before including this header then the default is used. To
  54. * change the maximum header size, define the macro in the build
  55. * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
  56. * the effective limit on the size of the header, define the macro
  57. * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
  58. */
  59. #ifndef HTTP_MAX_HEADER_SIZE
  60. # define HTTP_MAX_HEADER_SIZE (80*1024)
  61. #endif
  62. typedef struct http_parser http_parser;
  63. typedef struct http_parser_settings http_parser_settings;
  64. /* Callbacks should return non-zero to indicate an error. The parser will
  65. * then halt execution.
  66. *
  67. * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
  68. * returning '1' from on_headers_complete will tell the parser that it
  69. * should not expect a body. This is used when receiving a response to a
  70. * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
  71. * chunked' headers that indicate the presence of a body.
  72. *
  73. * Returning `2` from on_headers_complete will tell parser that it should not
  74. * expect neither a body nor any futher responses on this connection. This is
  75. * useful for handling responses to a CONNECT request which may not contain
  76. * `Upgrade` or `Connection: upgrade` headers.
  77. *
  78. * http_data_cb does not return data chunks. It will be called arbitrarily
  79. * many times for each string. E.G. you might get 10 callbacks for "on_url"
  80. * each providing just a few characters more data.
  81. */
  82. typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
  83. typedef int (*http_cb) (http_parser*);
  84. /* Flag values for http_parser.flags field */
  85. enum flags
  86. { F_CHUNKED = 1 << 0
  87. , F_CONNECTION_KEEP_ALIVE = 1 << 1
  88. , F_CONNECTION_CLOSE = 1 << 2
  89. , F_CONNECTION_UPGRADE = 1 << 3
  90. , F_TRAILING = 1 << 4
  91. , F_UPGRADE = 1 << 5
  92. , F_SKIPBODY = 1 << 6
  93. , F_CONTENTLENGTH = 1 << 7
  94. };
  95. /* Map for errno-related constants
  96. *
  97. * The provided argument should be a macro that takes 2 arguments.
  98. */
  99. #define HTTP_ERRNO_MAP(XX) \
  100. /* No error */ \
  101. XX(OK, "success") \
  102. \
  103. /* Callback-related errors */ \
  104. XX(CB_message_begin, "the on_message_begin callback failed") \
  105. XX(CB_url, "the on_url callback failed") \
  106. XX(CB_header_field, "the on_header_field callback failed") \
  107. XX(CB_header_value, "the on_header_value callback failed") \
  108. XX(CB_headers_complete, "the on_headers_complete callback failed") \
  109. XX(CB_body, "the on_body callback failed") \
  110. XX(CB_message_complete, "the on_message_complete callback failed") \
  111. XX(CB_status, "the on_status callback failed") \
  112. XX(CB_chunk_header, "the on_chunk_header callback failed") \
  113. XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
  114. \
  115. /* Parsing-related errors */ \
  116. XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
  117. XX(HEADER_OVERFLOW, \
  118. "too many header bytes seen; overflow detected") \
  119. XX(CLOSED_CONNECTION, \
  120. "data received after completed connection: close message") \
  121. XX(INVALID_VERSION, "invalid HTTP version") \
  122. XX(INVALID_STATUS, "invalid HTTP status code") \
  123. XX(INVALID_METHOD, "invalid HTTP method") \
  124. XX(INVALID_URL, "invalid URL") \
  125. XX(INVALID_HOST, "invalid host") \
  126. XX(INVALID_PORT, "invalid port") \
  127. XX(INVALID_PATH, "invalid path") \
  128. XX(INVALID_QUERY_STRING, "invalid query string") \
  129. XX(INVALID_FRAGMENT, "invalid fragment") \
  130. XX(LF_EXPECTED, "LF character expected") \
  131. XX(INVALID_HEADER_TOKEN, "invalid character in header") \
  132. XX(INVALID_CONTENT_LENGTH, \
  133. "invalid character in content-length header") \
  134. XX(UNEXPECTED_CONTENT_LENGTH, \
  135. "unexpected content-length header") \
  136. XX(INVALID_CHUNK_SIZE, \
  137. "invalid character in chunk size header") \
  138. XX(INVALID_CONSTANT, "invalid constant string") \
  139. XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
  140. XX(STRICT, "strict mode assertion failed") \
  141. XX(PAUSED, "parser is paused") \
  142. XX(UNKNOWN, "an unknown error occurred")
  143. /* Define HPE_* values for each errno value above */
  144. #define HTTP_ERRNO_GEN(n, s) HPE_##n,
  145. enum http_errno {
  146. HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
  147. };
  148. #undef HTTP_ERRNO_GEN
  149. /* Get an http_errno value from an http_parser */
  150. #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
  151. struct http_parser {
  152. /** PRIVATE **/
  153. unsigned int type : 2; /* enum http_parser_type */
  154. unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
  155. unsigned int state : 7; /* enum state from http_parser.c */
  156. unsigned int header_state : 7; /* enum header_state from http_parser.c */
  157. unsigned int index : 7; /* index into current matcher */
  158. unsigned int lenient_http_headers : 1;
  159. uint32_t nread; /* # bytes read in various scenarios */
  160. uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
  161. /** READ-ONLY **/
  162. unsigned short http_major;
  163. unsigned short http_minor;
  164. unsigned int status_code : 16; /* responses only */
  165. unsigned int method : 8; /* requests only */
  166. unsigned int http_errno : 7;
  167. /* 1 = Upgrade header was present and the parser has exited because of that.
  168. * 0 = No upgrade header present.
  169. * Should be checked when http_parser_execute() returns in addition to
  170. * error checking.
  171. */
  172. unsigned int upgrade : 1;
  173. /** PUBLIC **/
  174. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  175. };
  176. struct http_parser_settings {
  177. http_cb on_message_begin;
  178. http_data_cb on_url;
  179. http_data_cb on_status;
  180. http_data_cb on_header_field;
  181. http_data_cb on_header_value;
  182. http_cb on_headers_complete;
  183. http_data_cb on_body;
  184. http_cb on_message_complete;
  185. /* When on_chunk_header is called, the current chunk length is stored
  186. * in parser->content_length.
  187. */
  188. http_cb on_chunk_header;
  189. http_cb on_chunk_complete;
  190. };
  191. #ifdef WITH_HTTP_PRASER_URL
  192. enum http_parser_url_fields
  193. { UF_SCHEMA = 0
  194. , UF_HOST = 1
  195. , UF_PORT = 2
  196. , UF_PATH = 3
  197. , UF_QUERY = 4
  198. , UF_FRAGMENT = 5
  199. , UF_USERINFO = 6
  200. , UF_MAX = 7
  201. };
  202. /* Result structure for http_parser_parse_url().
  203. *
  204. * Callers should index into field_data[] with UF_* values iff field_set
  205. * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
  206. * because we probably have padding left over), we convert any port to
  207. * a uint16_t.
  208. */
  209. struct http_parser_url {
  210. uint16_t field_set; /* Bitmask of (1 << UF_*) values */
  211. uint16_t port; /* Converted UF_PORT string */
  212. struct {
  213. uint16_t off; /* Offset into buffer in which field starts */
  214. uint16_t len; /* Length of run in buffer */
  215. } field_data[UF_MAX];
  216. };
  217. #endif
  218. /* Returns the library version. Bits 16-23 contain the major version number,
  219. * bits 8-15 the minor version number and bits 0-7 the patch level.
  220. * Usage example:
  221. *
  222. * unsigned long version = http_parser_version();
  223. * unsigned major = (version >> 16) & 255;
  224. * unsigned minor = (version >> 8) & 255;
  225. * unsigned patch = version & 255;
  226. * printf("http_parser v%u.%u.%u\n", major, minor, patch);
  227. */
  228. unsigned long http_parser_version(void);
  229. void http_parser_init(http_parser *parser, enum http_parser_type type);
  230. /* Initialize http_parser_settings members to 0
  231. */
  232. void http_parser_settings_init(http_parser_settings *settings);
  233. /* Executes the parser. Returns number of parsed bytes. Sets
  234. * `parser->http_errno` on error. */
  235. size_t http_parser_execute(http_parser *parser,
  236. const http_parser_settings *settings,
  237. const char *data,
  238. size_t len);
  239. /* If http_should_keep_alive() in the on_headers_complete or
  240. * on_message_complete callback returns 0, then this should be
  241. * the last message on the connection.
  242. * If you are the server, respond with the "Connection: close" header.
  243. * If you are the client, close the connection.
  244. */
  245. int http_should_keep_alive(const http_parser *parser);
  246. /* Return a string name of the given error */
  247. const char *http_errno_name(enum http_errno err);
  248. /* Return a string description of the given error */
  249. const char *http_errno_description(enum http_errno err);
  250. #ifdef WITH_HTTP_PRASER_URL
  251. /* Initialize all http_parser_url members to 0 */
  252. void http_parser_url_init(struct http_parser_url *u);
  253. /* Parse a URL; return nonzero on failure */
  254. int http_parser_parse_url(const char *buf, size_t buflen,
  255. int is_connect,
  256. struct http_parser_url *u);
  257. #endif
  258. /* Pause or un-pause the parser; a nonzero value pauses */
  259. void http_parser_pause(http_parser *parser, int paused);
  260. /* Checks if this is the final chunk of the body. */
  261. int http_body_is_final(const http_parser *parser);
  262. /* Change the maximum header size provided at compile time. */
  263. void http_parser_set_max_header_size(uint32_t size);
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif