http_parser.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 5
  29. #define HTTP_PARSER_VERSION_PATCH 0
  30. #include <sys/types.h>
  31. #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
  32. #include <BaseTsd.h>
  33. #include <stddef.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. * http_data_cb does not return data chunks. It will be called arbitrarily
  73. * many times for each string. E.G. you might get 10 callbacks for "on_url"
  74. * each providing just a few characters more data.
  75. */
  76. typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
  77. typedef int (*http_cb) (http_parser*);
  78. /* Request Methods */
  79. #define HTTP_METHOD_MAP(XX) \
  80. XX(0, DELETE, DELETE) \
  81. XX(1, GET, GET) \
  82. XX(2, HEAD, HEAD) \
  83. XX(3, POST, POST) \
  84. XX(4, PUT, PUT) \
  85. /* pathological */ \
  86. XX(5, CONNECT, CONNECT) \
  87. XX(6, OPTIONS, OPTIONS) \
  88. XX(7, TRACE, TRACE) \
  89. /* webdav */ \
  90. XX(8, COPY, COPY) \
  91. XX(9, LOCK, LOCK) \
  92. XX(10, MKCOL, MKCOL) \
  93. XX(11, MOVE, MOVE) \
  94. XX(12, PROPFIND, PROPFIND) \
  95. XX(13, PROPPATCH, PROPPATCH) \
  96. XX(14, SEARCH, SEARCH) \
  97. XX(15, UNLOCK, UNLOCK) \
  98. /* subversion */ \
  99. XX(16, REPORT, REPORT) \
  100. XX(17, MKACTIVITY, MKACTIVITY) \
  101. XX(18, CHECKOUT, CHECKOUT) \
  102. XX(19, MERGE, MERGE) \
  103. /* upnp */ \
  104. XX(20, MSEARCH, M-SEARCH) \
  105. XX(21, NOTIFY, NOTIFY) \
  106. XX(22, SUBSCRIBE, SUBSCRIBE) \
  107. XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
  108. /* RFC-5789 */ \
  109. XX(24, PATCH, PATCH) \
  110. XX(25, PURGE, PURGE) \
  111. /* CalDAV */ \
  112. XX(26, MKCALENDAR, MKCALENDAR) \
  113. enum http_method
  114. {
  115. #define XX(num, name, string) HTTP_##name = num,
  116. HTTP_METHOD_MAP(XX)
  117. #undef XX
  118. };
  119. enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
  120. /* Flag values for http_parser.flags field */
  121. enum flags
  122. { F_CHUNKED = 1 << 0
  123. , F_CONNECTION_KEEP_ALIVE = 1 << 1
  124. , F_CONNECTION_CLOSE = 1 << 2
  125. , F_CONNECTION_UPGRADE = 1 << 3
  126. , F_TRAILING = 1 << 4
  127. , F_UPGRADE = 1 << 5
  128. , F_SKIPBODY = 1 << 6
  129. };
  130. /* Map for errno-related constants
  131. *
  132. * The provided argument should be a macro that takes 2 arguments.
  133. */
  134. #define HTTP_ERRNO_MAP(XX) \
  135. /* No error */ \
  136. XX(OK, "success") \
  137. \
  138. /* Callback-related errors */ \
  139. XX(CB_message_begin, "the on_message_begin callback failed") \
  140. XX(CB_url, "the on_url callback failed") \
  141. XX(CB_header_field, "the on_header_field callback failed") \
  142. XX(CB_header_value, "the on_header_value callback failed") \
  143. XX(CB_headers_complete, "the on_headers_complete callback failed") \
  144. XX(CB_body, "the on_body callback failed") \
  145. XX(CB_message_complete, "the on_message_complete callback failed") \
  146. XX(CB_status, "the on_status callback failed") \
  147. XX(CB_chunk_header, "the on_chunk_header callback failed") \
  148. XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
  149. \
  150. /* Parsing-related errors */ \
  151. XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
  152. XX(HEADER_OVERFLOW, \
  153. "too many header bytes seen; overflow detected") \
  154. XX(CLOSED_CONNECTION, \
  155. "data received after completed connection: close message") \
  156. XX(INVALID_VERSION, "invalid HTTP version") \
  157. XX(INVALID_STATUS, "invalid HTTP status code") \
  158. XX(INVALID_METHOD, "invalid HTTP method") \
  159. XX(INVALID_URL, "invalid URL") \
  160. XX(INVALID_HOST, "invalid host") \
  161. XX(INVALID_PORT, "invalid port") \
  162. XX(INVALID_PATH, "invalid path") \
  163. XX(INVALID_QUERY_STRING, "invalid query string") \
  164. XX(INVALID_FRAGMENT, "invalid fragment") \
  165. XX(LF_EXPECTED, "LF character expected") \
  166. XX(INVALID_HEADER_TOKEN, "invalid character in header") \
  167. XX(INVALID_CONTENT_LENGTH, \
  168. "invalid character in content-length header") \
  169. XX(INVALID_CHUNK_SIZE, \
  170. "invalid character in chunk size header") \
  171. XX(INVALID_CONSTANT, "invalid constant string") \
  172. XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
  173. XX(STRICT, "strict mode assertion failed") \
  174. XX(PAUSED, "parser is paused") \
  175. XX(UNKNOWN, "an unknown error occurred")
  176. /* Define HPE_* values for each errno value above */
  177. #define HTTP_ERRNO_GEN(n, s) HPE_##n,
  178. enum http_errno {
  179. HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
  180. };
  181. #undef HTTP_ERRNO_GEN
  182. /* Get an http_errno value from an http_parser */
  183. #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
  184. struct http_parser {
  185. /** PRIVATE **/
  186. unsigned int type : 2; /* enum http_parser_type */
  187. unsigned int flags : 7; /* F_* values from 'flags' enum; semi-public */
  188. unsigned int state : 7; /* enum state from http_parser.c */
  189. unsigned int header_state : 8; /* enum header_state from http_parser.c */
  190. unsigned int index : 8; /* index into current matcher */
  191. uint32_t nread; /* # bytes read in various scenarios */
  192. uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
  193. /** READ-ONLY **/
  194. unsigned short http_major;
  195. unsigned short http_minor;
  196. unsigned int status_code : 16; /* responses only */
  197. unsigned int method : 8; /* requests only */
  198. unsigned int http_errno : 7;
  199. /* 1 = Upgrade header was present and the parser has exited because of that.
  200. * 0 = No upgrade header present.
  201. * Should be checked when http_parser_execute() returns in addition to
  202. * error checking.
  203. */
  204. unsigned int upgrade : 1;
  205. /** PUBLIC **/
  206. void *data; /* A pointer to get hook to the "connection" or "socket" object */
  207. };
  208. struct http_parser_settings {
  209. http_cb on_message_begin;
  210. http_data_cb on_url;
  211. http_data_cb on_status;
  212. http_data_cb on_header_field;
  213. http_data_cb on_header_value;
  214. http_cb on_headers_complete;
  215. http_data_cb on_body;
  216. http_cb on_message_complete;
  217. /* When on_chunk_header is called, the current chunk length is stored
  218. * in parser->content_length.
  219. */
  220. http_cb on_chunk_header;
  221. http_cb on_chunk_complete;
  222. };
  223. enum http_parser_url_fields
  224. { UF_SCHEMA = 0
  225. , UF_HOST = 1
  226. , UF_PORT = 2
  227. , UF_PATH = 3
  228. , UF_QUERY = 4
  229. , UF_FRAGMENT = 5
  230. , UF_USERINFO = 6
  231. , UF_MAX = 7
  232. };
  233. /* Result structure for http_parser_parse_url().
  234. *
  235. * Callers should index into field_data[] with UF_* values iff field_set
  236. * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
  237. * because we probably have padding left over), we convert any port to
  238. * a uint16_t.
  239. */
  240. struct http_parser_url {
  241. uint16_t field_set; /* Bitmask of (1 << UF_*) values */
  242. uint16_t port; /* Converted UF_PORT string */
  243. struct {
  244. uint16_t off; /* Offset into buffer in which field starts */
  245. uint16_t len; /* Length of run in buffer */
  246. } field_data[UF_MAX];
  247. };
  248. /* Returns the library version. Bits 16-23 contain the major version number,
  249. * bits 8-15 the minor version number and bits 0-7 the patch level.
  250. * Usage example:
  251. *
  252. * unsigned long version = http_parser_version();
  253. * unsigned major = (version >> 16) & 255;
  254. * unsigned minor = (version >> 8) & 255;
  255. * unsigned patch = version & 255;
  256. * printf("http_parser v%u.%u.%u\n", major, minor, patch);
  257. */
  258. unsigned long http_parser_version(void);
  259. void http_parser_init(http_parser *parser, enum http_parser_type type);
  260. /* Initialize http_parser_settings members to 0
  261. */
  262. void http_parser_settings_init(http_parser_settings *settings);
  263. /* Executes the parser. Returns number of parsed bytes. Sets
  264. * `parser->http_errno` on error. */
  265. size_t http_parser_execute(http_parser *parser,
  266. const http_parser_settings *settings,
  267. const char *data,
  268. size_t len);
  269. /* If http_should_keep_alive() in the on_headers_complete or
  270. * on_message_complete callback returns 0, then this should be
  271. * the last message on the connection.
  272. * If you are the server, respond with the "Connection: close" header.
  273. * If you are the client, close the connection.
  274. */
  275. int http_should_keep_alive(const http_parser *parser);
  276. /* Returns a string version of the HTTP method. */
  277. const char *http_method_str(enum http_method m);
  278. /* Return a string name of the given error */
  279. const char *http_errno_name(enum http_errno err);
  280. /* Return a string description of the given error */
  281. const char *http_errno_description(enum http_errno err);
  282. /* Parse a URL; return nonzero on failure */
  283. int http_parser_parse_url(const char *buf, size_t buflen,
  284. int is_connect,
  285. struct http_parser_url *u);
  286. /* Pause or un-pause the parser; a nonzero value pauses */
  287. void http_parser_pause(http_parser *parser, int paused);
  288. /* Checks if this is the final chunk of the body. */
  289. int http_body_is_final(const http_parser *parser);
  290. #ifdef __cplusplus
  291. }
  292. #endif
  293. #endif