|
@@ -24,11 +24,14 @@
|
|
|
extern "C" {
|
|
|
#endif
|
|
|
|
|
|
+/* Also update SONAME in the Makefile whenever you change these. */
|
|
|
#define HTTP_PARSER_VERSION_MAJOR 2
|
|
|
-#define HTTP_PARSER_VERSION_MINOR 1
|
|
|
+#define HTTP_PARSER_VERSION_MINOR 7
|
|
|
+#define HTTP_PARSER_VERSION_PATCH 1
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
-#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
|
|
|
+#if defined(_WIN32) && !defined(__MINGW32__) && \
|
|
|
+ (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
|
|
|
#include <BaseTsd.h>
|
|
|
#include <stddef.h>
|
|
|
typedef __int8 int8_t;
|
|
@@ -50,9 +53,16 @@ typedef unsigned __int64 uint64_t;
|
|
|
# define HTTP_PARSER_STRICT 1
|
|
|
#endif
|
|
|
|
|
|
-/* Maximium header size allowed */
|
|
|
-#define HTTP_MAX_HEADER_SIZE (80*1024)
|
|
|
-
|
|
|
+/* Maximium header size allowed. If the macro is not defined
|
|
|
+ * before including this header then the default is used. To
|
|
|
+ * change the maximum header size, define the macro in the build
|
|
|
+ * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
|
|
|
+ * the effective limit on the size of the header, define the macro
|
|
|
+ * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
|
|
|
+ */
|
|
|
+#ifndef HTTP_MAX_HEADER_SIZE
|
|
|
+# define HTTP_MAX_HEADER_SIZE (80*1024)
|
|
|
+#endif
|
|
|
|
|
|
typedef struct http_parser http_parser;
|
|
|
typedef struct http_parser_settings http_parser_settings;
|
|
@@ -67,7 +77,12 @@ typedef struct http_parser_settings http_parser_settings;
|
|
|
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
|
|
|
* chunked' headers that indicate the presence of a body.
|
|
|
*
|
|
|
- * http_data_cb does not return data chunks. It will be call arbitrarally
|
|
|
+ * Returning `2` from on_headers_complete will tell parser that it should not
|
|
|
+ * expect neither a body nor any futher responses on this connection. This is
|
|
|
+ * useful for handling responses to a CONNECT request which may not contain
|
|
|
+ * `Upgrade` or `Connection: upgrade` headers.
|
|
|
+ *
|
|
|
+ * http_data_cb does not return data chunks. It will be called arbitrarily
|
|
|
* many times for each string. E.G. you might get 10 callbacks for "on_url"
|
|
|
* each providing just a few characters more data.
|
|
|
*/
|
|
@@ -86,7 +101,7 @@ typedef int (*http_cb) (http_parser*);
|
|
|
XX(5, CONNECT, CONNECT) \
|
|
|
XX(6, OPTIONS, OPTIONS) \
|
|
|
XX(7, TRACE, TRACE) \
|
|
|
- /* webdav */ \
|
|
|
+ /* WebDAV */ \
|
|
|
XX(8, COPY, COPY) \
|
|
|
XX(9, LOCK, LOCK) \
|
|
|
XX(10, MKCOL, MKCOL) \
|
|
@@ -95,19 +110,28 @@ typedef int (*http_cb) (http_parser*);
|
|
|
XX(13, PROPPATCH, PROPPATCH) \
|
|
|
XX(14, SEARCH, SEARCH) \
|
|
|
XX(15, UNLOCK, UNLOCK) \
|
|
|
+ XX(16, BIND, BIND) \
|
|
|
+ XX(17, REBIND, REBIND) \
|
|
|
+ XX(18, UNBIND, UNBIND) \
|
|
|
+ XX(19, ACL, ACL) \
|
|
|
/* subversion */ \
|
|
|
- XX(16, REPORT, REPORT) \
|
|
|
- XX(17, MKACTIVITY, MKACTIVITY) \
|
|
|
- XX(18, CHECKOUT, CHECKOUT) \
|
|
|
- XX(19, MERGE, MERGE) \
|
|
|
+ XX(20, REPORT, REPORT) \
|
|
|
+ XX(21, MKACTIVITY, MKACTIVITY) \
|
|
|
+ XX(22, CHECKOUT, CHECKOUT) \
|
|
|
+ XX(23, MERGE, MERGE) \
|
|
|
/* upnp */ \
|
|
|
- XX(20, MSEARCH, M-SEARCH) \
|
|
|
- XX(21, NOTIFY, NOTIFY) \
|
|
|
- XX(22, SUBSCRIBE, SUBSCRIBE) \
|
|
|
- XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
|
|
|
+ XX(24, MSEARCH, M-SEARCH) \
|
|
|
+ XX(25, NOTIFY, NOTIFY) \
|
|
|
+ XX(26, SUBSCRIBE, SUBSCRIBE) \
|
|
|
+ XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
|
|
|
/* RFC-5789 */ \
|
|
|
- XX(24, PATCH, PATCH) \
|
|
|
- XX(25, PURGE, PURGE) \
|
|
|
+ XX(28, PATCH, PATCH) \
|
|
|
+ XX(29, PURGE, PURGE) \
|
|
|
+ /* CalDAV */ \
|
|
|
+ XX(30, MKCALENDAR, MKCALENDAR) \
|
|
|
+ /* RFC-2068, section 19.6.1.2 */ \
|
|
|
+ XX(31, LINK, LINK) \
|
|
|
+ XX(32, UNLINK, UNLINK) \
|
|
|
|
|
|
enum http_method
|
|
|
{
|
|
@@ -125,14 +149,16 @@ enum flags
|
|
|
{ F_CHUNKED = 1 << 0
|
|
|
, F_CONNECTION_KEEP_ALIVE = 1 << 1
|
|
|
, F_CONNECTION_CLOSE = 1 << 2
|
|
|
- , F_TRAILING = 1 << 3
|
|
|
- , F_UPGRADE = 1 << 4
|
|
|
- , F_SKIPBODY = 1 << 5
|
|
|
+ , F_CONNECTION_UPGRADE = 1 << 3
|
|
|
+ , F_TRAILING = 1 << 4
|
|
|
+ , F_UPGRADE = 1 << 5
|
|
|
+ , F_SKIPBODY = 1 << 6
|
|
|
+ , F_CONTENTLENGTH = 1 << 7
|
|
|
};
|
|
|
|
|
|
|
|
|
/* Map for errno-related constants
|
|
|
- *
|
|
|
+ *
|
|
|
* The provided argument should be a macro that takes 2 arguments.
|
|
|
*/
|
|
|
#define HTTP_ERRNO_MAP(XX) \
|
|
@@ -141,13 +167,15 @@ enum flags
|
|
|
\
|
|
|
/* Callback-related errors */ \
|
|
|
XX(CB_message_begin, "the on_message_begin callback failed") \
|
|
|
- XX(CB_status_complete, "the on_status_complete callback failed") \
|
|
|
XX(CB_url, "the on_url callback failed") \
|
|
|
XX(CB_header_field, "the on_header_field callback failed") \
|
|
|
XX(CB_header_value, "the on_header_value callback failed") \
|
|
|
XX(CB_headers_complete, "the on_headers_complete callback failed") \
|
|
|
XX(CB_body, "the on_body callback failed") \
|
|
|
XX(CB_message_complete, "the on_message_complete callback failed") \
|
|
|
+ XX(CB_status, "the on_status callback failed") \
|
|
|
+ XX(CB_chunk_header, "the on_chunk_header callback failed") \
|
|
|
+ XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
|
|
|
\
|
|
|
/* Parsing-related errors */ \
|
|
|
XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
|
|
@@ -168,6 +196,8 @@ enum flags
|
|
|
XX(INVALID_HEADER_TOKEN, "invalid character in header") \
|
|
|
XX(INVALID_CONTENT_LENGTH, \
|
|
|
"invalid character in content-length header") \
|
|
|
+ XX(UNEXPECTED_CONTENT_LENGTH, \
|
|
|
+ "unexpected content-length header") \
|
|
|
XX(INVALID_CHUNK_SIZE, \
|
|
|
"invalid character in chunk size header") \
|
|
|
XX(INVALID_CONSTANT, "invalid constant string") \
|
|
@@ -191,11 +221,12 @@ enum http_errno {
|
|
|
|
|
|
struct http_parser {
|
|
|
/** PRIVATE **/
|
|
|
- unsigned char type : 2; /* enum http_parser_type */
|
|
|
- unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
|
|
|
- unsigned char state; /* enum state from http_parser.c */
|
|
|
- unsigned char header_state; /* enum header_state from http_parser.c */
|
|
|
- unsigned char index; /* index into current matcher */
|
|
|
+ unsigned int type : 2; /* enum http_parser_type */
|
|
|
+ unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
|
|
|
+ unsigned int state : 7; /* enum state from http_parser.c */
|
|
|
+ unsigned int header_state : 7; /* enum header_state from http_parser.c */
|
|
|
+ unsigned int index : 7; /* index into current matcher */
|
|
|
+ unsigned int lenient_http_headers : 1;
|
|
|
|
|
|
uint32_t nread; /* # bytes read in various scenarios */
|
|
|
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
|
|
@@ -203,16 +234,16 @@ struct http_parser {
|
|
|
/** READ-ONLY **/
|
|
|
unsigned short http_major;
|
|
|
unsigned short http_minor;
|
|
|
- unsigned short status_code; /* responses only */
|
|
|
- unsigned char method; /* requests only */
|
|
|
- unsigned char http_errno : 7;
|
|
|
+ unsigned int status_code : 16; /* responses only */
|
|
|
+ unsigned int method : 8; /* requests only */
|
|
|
+ unsigned int http_errno : 7;
|
|
|
|
|
|
/* 1 = Upgrade header was present and the parser has exited because of that.
|
|
|
* 0 = No upgrade header present.
|
|
|
* Should be checked when http_parser_execute() returns in addition to
|
|
|
* error checking.
|
|
|
*/
|
|
|
- unsigned char upgrade : 1;
|
|
|
+ unsigned int upgrade : 1;
|
|
|
|
|
|
/** PUBLIC **/
|
|
|
void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
|
@@ -222,12 +253,17 @@ struct http_parser {
|
|
|
struct http_parser_settings {
|
|
|
http_cb on_message_begin;
|
|
|
http_data_cb on_url;
|
|
|
- http_cb on_status_complete;
|
|
|
+ http_data_cb on_status;
|
|
|
http_data_cb on_header_field;
|
|
|
http_data_cb on_header_value;
|
|
|
http_cb on_headers_complete;
|
|
|
http_data_cb on_body;
|
|
|
http_cb on_message_complete;
|
|
|
+ /* When on_chunk_header is called, the current chunk length is stored
|
|
|
+ * in parser->content_length.
|
|
|
+ */
|
|
|
+ http_cb on_chunk_header;
|
|
|
+ http_cb on_chunk_complete;
|
|
|
};
|
|
|
|
|
|
|
|
@@ -261,9 +297,28 @@ struct http_parser_url {
|
|
|
};
|
|
|
|
|
|
|
|
|
+/* Returns the library version. Bits 16-23 contain the major version number,
|
|
|
+ * bits 8-15 the minor version number and bits 0-7 the patch level.
|
|
|
+ * Usage example:
|
|
|
+ *
|
|
|
+ * unsigned long version = http_parser_version();
|
|
|
+ * unsigned major = (version >> 16) & 255;
|
|
|
+ * unsigned minor = (version >> 8) & 255;
|
|
|
+ * unsigned patch = version & 255;
|
|
|
+ * printf("http_parser v%u.%u.%u\n", major, minor, patch);
|
|
|
+ */
|
|
|
+unsigned long http_parser_version(void);
|
|
|
+
|
|
|
void http_parser_init(http_parser *parser, enum http_parser_type type);
|
|
|
|
|
|
|
|
|
+/* Initialize http_parser_settings members to 0
|
|
|
+ */
|
|
|
+void http_parser_settings_init(http_parser_settings *settings);
|
|
|
+
|
|
|
+
|
|
|
+/* Executes the parser. Returns number of parsed bytes. Sets
|
|
|
+ * `parser->http_errno` on error. */
|
|
|
size_t http_parser_execute(http_parser *parser,
|
|
|
const http_parser_settings *settings,
|
|
|
const char *data,
|
|
@@ -287,6 +342,9 @@ const char *http_errno_name(enum http_errno err);
|
|
|
/* Return a string description of the given error */
|
|
|
const char *http_errno_description(enum http_errno err);
|
|
|
|
|
|
+/* Initialize all http_parser_url members to 0 */
|
|
|
+void http_parser_url_init(struct http_parser_url *u);
|
|
|
+
|
|
|
/* Parse a URL; return nonzero on failure */
|
|
|
int http_parser_parse_url(const char *buf, size_t buflen,
|
|
|
int is_connect,
|