http_parser.h 11 KB

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