Tasteful vertical whitespace.

event_stream
Ryan Dahl 15 years ago
parent 4bce6b4467
commit e07e0b952e

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

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

Loading…
Cancel
Save