Tasteful vertical whitespace.

event_stream
Ryan Dahl 15 years ago
parent 4bce6b4467
commit e07e0b952e

@ -26,16 +26,20 @@
#include <assert.h> #include <assert.h>
#include <string.h> /* strncmp */ #include <string.h> /* strncmp */
#ifndef NULL #ifndef NULL
# define NULL ((void*)0) # define NULL ((void*)0)
#endif #endif
#ifndef MIN #ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b)) # define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif #endif
#define MAX_FIELD_SIZE (80*1024) #define MAX_FIELD_SIZE (80*1024)
#define CALLBACK2(FOR) \ #define CALLBACK2(FOR) \
do { \ do { \
if (settings.on_##FOR) { \ if (settings.on_##FOR) { \
@ -43,12 +47,14 @@ do { \
} \ } \
} while (0) } while (0)
#define MARK(FOR) \ #define MARK(FOR) \
do { \ do { \
parser->FOR##_mark = p; \ parser->FOR##_mark = p; \
parser->FOR##_size = 0; \ parser->FOR##_size = 0; \
} while (0) } while (0)
#define CALLBACK_NOCLEAR(FOR) \ #define CALLBACK_NOCLEAR(FOR) \
do { \ do { \
if (parser->FOR##_mark) { \ if (parser->FOR##_mark) { \
@ -65,6 +71,7 @@ do { \
} \ } \
} while (0) } while (0)
#define CALLBACK(FOR) \ #define CALLBACK(FOR) \
do { \ do { \
CALLBACK_NOCLEAR(FOR); \ CALLBACK_NOCLEAR(FOR); \
@ -76,11 +83,11 @@ do { \
#define CONNECTION "connection" #define CONNECTION "connection"
#define CONTENT_LENGTH "content-length" #define CONTENT_LENGTH "content-length"
#define TRANSFER_ENCODING "transfer-encoding" #define TRANSFER_ENCODING "transfer-encoding"
#define CHUNKED "chunked" #define CHUNKED "chunked"
#define KEEP_ALIVE "keep-alive" #define KEEP_ALIVE "keep-alive"
#define CLOSE "close" #define CLOSE "close"
static const unsigned char lowcase[] = static const unsigned char lowcase[] =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0-\0\0" "0123456789\0\0\0\0\0\0"
@ -91,6 +98,7 @@ static const unsigned char lowcase[] =
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
static const int unhex[] = static const int unhex[] =
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
@ -103,6 +111,7 @@ static const int unhex[] =
}; };
static const uint32_t usual[] = { static const uint32_t usual[] = {
0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */ 0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */
@ -123,6 +132,7 @@ static const uint32_t usual[] = {
#define USUAL(c) (usual[c >> 5] & (1 << (c & 0x1f))) #define USUAL(c) (usual[c >> 5] & (1 << (c & 0x1f)))
enum state enum state
{ s_dead = 1 /* important that this is > 0 */ { s_dead = 1 /* important that this is > 0 */
@ -191,6 +201,7 @@ enum state
#define PARSING_HEADER(state) (state <= s_headers_almost_done) #define PARSING_HEADER(state) (state <= s_headers_almost_done)
enum header_states enum header_states
{ h_general = 0 { h_general = 0
, h_C , h_C
@ -215,6 +226,7 @@ enum header_states
, h_connection_close , h_connection_close
}; };
enum flags enum flags
{ F_CHUNKED = 1 << 0 { F_CHUNKED = 1 << 0
, F_CONNECTION_KEEP_ALIVE = 1 << 1 , F_CONNECTION_KEEP_ALIVE = 1 << 1
@ -222,12 +234,15 @@ enum flags
, F_TRAILING = 1 << 3 , F_TRAILING = 1 << 3
}; };
#define CR '\r' #define CR '\r'
#define LF '\n' #define LF '\n'
#define LOWER(c) (unsigned char)(c | 0x20) #define LOWER(c) (unsigned char)(c | 0x20)
#define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res) #define start_state (parser->type == HTTP_REQUEST ? s_start_req : s_start_res)
#if HTTP_PARSER_STRICT #if HTTP_PARSER_STRICT
# define STRICT_CHECK(cond) if (cond) goto error # define STRICT_CHECK(cond) if (cond) goto error
# define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead) # define NEW_MESSAGE() (http_should_keep_alive(parser) ? start_state : s_dead)
@ -236,6 +251,7 @@ enum flags
# define NEW_MESSAGE() start_state # define NEW_MESSAGE() start_state
#endif #endif
#define ngx_str3_cmp(m, c0, c1, c2) \ #define ngx_str3_cmp(m, c0, c1, c2) \
m[0] == c0 && m[1] == c1 && m[2] == c2 m[0] == c0 && m[1] == c1 && m[2] == c2
@ -263,6 +279,8 @@ enum flags
#define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \ #define ngx_str9cmp(m, c0, c1, c2, c3, c4, c5, c6, c7, c8) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \ m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 \
&& m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 && m[8] == c8 && m[4] == c4 && m[5] == c5 && m[6] == c6 && m[7] == c7 && m[8] == c8
size_t http_parser_execute (http_parser *parser, size_t http_parser_execute (http_parser *parser,
http_parser_settings settings, http_parser_settings settings,
const char *data, const char *data,

@ -24,11 +24,13 @@
extern "C" { extern "C" {
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
# include <stddef.h> # include <stddef.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
* faster * faster
*/ */
@ -38,12 +40,15 @@ extern "C" {
# define HTTP_PARSER_STRICT 0 # define HTTP_PARSER_STRICT 0
#endif #endif
/* Maximium header size allowed */ /* Maximium header size allowed */
#define HTTP_MAX_HEADER_SIZE (80*1024) #define HTTP_MAX_HEADER_SIZE (80*1024)
typedef struct http_parser http_parser; typedef struct http_parser http_parser;
typedef struct http_parser_settings http_parser_settings; typedef struct http_parser_settings http_parser_settings;
/* Callbacks should return non-zero to indicate an error. The parser will /* Callbacks should return non-zero to indicate an error. The parser will
* then halt execution. * then halt execution.
* *
@ -54,9 +59,11 @@ typedef struct http_parser_settings http_parser_settings;
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
typedef int (*http_cb) (http_parser*); typedef int (*http_cb) (http_parser*);
/* Should be at least one longer than the longest request method */ /* Should be at least one longer than the longest request method */
#define HTTP_PARSER_MAX_METHOD_LEN 10 #define HTTP_PARSER_MAX_METHOD_LEN 10
/* Request Methods */ /* Request Methods */
enum http_method enum http_method
{ HTTP_DELETE = 0x0001 { HTTP_DELETE = 0x0001
@ -78,8 +85,10 @@ enum http_method
, HTTP_UNLOCK = 0x4000 , HTTP_UNLOCK = 0x4000
}; };
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE }; enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE };
struct http_parser { struct http_parser {
/** PRIVATE **/ /** PRIVATE **/
enum http_parser_type type; enum http_parser_type type;
@ -117,17 +126,13 @@ struct http_parser {
void *data; /* A pointer to get hook to the "connection" or "socket" object */ void *data; /* A pointer to get hook to the "connection" or "socket" object */
}; };
struct http_parser_settings {
/* an ordered list of callbacks */
struct http_parser_settings {
http_cb on_message_begin; http_cb on_message_begin;
/* requests only */
http_data_cb on_path; http_data_cb on_path;
http_data_cb on_query_string; http_data_cb on_query_string;
http_data_cb on_url; http_data_cb on_url;
http_data_cb on_fragment; http_data_cb on_fragment;
http_data_cb on_header_field; http_data_cb on_header_field;
http_data_cb on_header_value; http_data_cb on_header_value;
http_cb on_headers_complete; http_cb on_headers_complete;
@ -135,13 +140,16 @@ struct http_parser_settings {
http_cb on_message_complete; http_cb on_message_complete;
}; };
void http_parser_init(http_parser *parser, enum http_parser_type type); void http_parser_init(http_parser *parser, enum http_parser_type type);
size_t http_parser_execute(http_parser *parser, size_t http_parser_execute(http_parser *parser,
http_parser_settings settings, http_parser_settings settings,
const char *data, const char *data,
size_t len); size_t len);
/* If http_should_keep_alive() in the on_headers_complete or /* If http_should_keep_alive() in the on_headers_complete or
* on_message_complete callback returns true, then this will be should be * on_message_complete callback returns true, then this will be should be
* the last message on the connection. * the last message on the connection.
@ -150,6 +158,7 @@ size_t http_parser_execute(http_parser *parser,
*/ */
int http_should_keep_alive(http_parser *parser); int http_should_keep_alive(http_parser *parser);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save