event_stream
Ryan Dahl 15 years ago
parent 6cefbc13af
commit 3834853a8a

@ -68,7 +68,7 @@ static inline int FOR##_callback (http_parser *parser, const char *p) \
return r; \
}
DEFINE_CALLBACK(uri)
DEFINE_CALLBACK(url)
DEFINE_CALLBACK(path)
DEFINE_CALLBACK(query_string)
DEFINE_CALLBACK(fragment)
@ -163,7 +163,7 @@ enum state
, s_method_DELE
, s_method_DELET
, s_spaces_before_uri
, s_spaces_before_url
, s_schema
, s_schema_slash
@ -268,7 +268,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
if (parser->fragment_mark) parser->fragment_mark = data;
if (parser->query_string_mark) parser->query_string_mark = data;
if (parser->path_mark) parser->path_mark = data;
if (parser->uri_mark) parser->uri_mark = data;
if (parser->url_mark) parser->url_mark = data;
for (p=data, pe=data+len; p != pe; p++) {
ch = *p;
@ -321,7 +321,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
case s_method_GE:
if (ch != 'T') return ERROR;
parser->method = HTTP_GET;
state = s_spaces_before_uri;
state = s_spaces_before_url;
break;
/* HEAD */
@ -339,7 +339,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
case s_method_HEA:
if (ch != 'D') return ERROR;
parser->method = HTTP_HEAD;
state = s_spaces_before_uri;
state = s_spaces_before_url;
break;
/* POST, PUT */
@ -364,7 +364,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
case s_method_PU:
if (ch != 'T') return ERROR;
parser->method = HTTP_PUT;
state = s_spaces_before_uri;
state = s_spaces_before_url;
break;
/* POST */
@ -377,7 +377,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
case s_method_POS:
if (ch != 'T') return ERROR;
parser->method = HTTP_POST;
state = s_spaces_before_uri;
state = s_spaces_before_url;
break;
/* DELETE */
@ -405,16 +405,16 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
case s_method_DELET:
if (ch != 'E') return ERROR;
parser->method = HTTP_DELETE;
state = s_spaces_before_uri;
state = s_spaces_before_url;
break;
case s_spaces_before_uri:
case s_spaces_before_url:
{
if (ch == ' ') break;
if (ch == '/') {
MARK(uri);
MARK(url);
MARK(path);
state = s_path;
break;
@ -423,7 +423,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
c = LOWER(ch);
if (c >= 'a' && c <= 'z') {
MARK(uri);
MARK(url);
state = s_schema;
break;
}
@ -473,7 +473,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
* "GET http://foo.bar.com HTTP/1.1"
* That is, there is no path.
*/
CALLBACK(uri);
CALLBACK(url);
state = s_http_start;
break;
default:
@ -495,7 +495,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
* "GET http://foo.bar.com:1234 HTTP/1.1"
* That is, there is no path.
*/
CALLBACK(uri);
CALLBACK(url);
state = s_http_start;
break;
default:
@ -510,18 +510,18 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
switch (ch) {
case ' ':
CALLBACK(uri);
CALLBACK(url);
CALLBACK(path);
state = s_http_start;
break;
case CR:
CALLBACK(uri);
CALLBACK(url);
CALLBACK(path);
parser->http_minor = 9;
state = s_req_line_almost_done;
break;
case LF:
CALLBACK(uri);
CALLBACK(url);
CALLBACK(path);
parser->http_minor = 9;
state = s_header_field_start;
@ -552,16 +552,16 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
case '?':
break; // XXX ignore extra '?' ... is this right?
case ' ':
CALLBACK(uri);
CALLBACK(url);
state = s_http_start;
break;
case CR:
CALLBACK(uri);
CALLBACK(url);
parser->http_minor = 9;
state = s_req_line_almost_done;
break;
case LF:
CALLBACK(uri);
CALLBACK(url);
parser->http_minor = 9;
state = s_header_field_start;
break;
@ -580,18 +580,18 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
switch (ch) {
case ' ':
CALLBACK(uri);
CALLBACK(url);
CALLBACK(query_string);
state = s_http_start;
break;
case CR:
CALLBACK(uri);
CALLBACK(url);
CALLBACK(query_string);
parser->http_minor = 9;
state = s_req_line_almost_done;
break;
case LF:
CALLBACK(uri);
CALLBACK(url);
CALLBACK(query_string);
parser->http_minor = 9;
state = s_header_field_start;
@ -616,16 +616,16 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
switch (ch) {
case ' ':
CALLBACK(uri);
CALLBACK(url);
state = s_http_start;
break;
case CR:
CALLBACK(uri);
CALLBACK(url);
parser->http_minor = 9;
state = s_req_line_almost_done;
break;
case LF:
CALLBACK(uri);
CALLBACK(url);
parser->http_minor = 9;
state = s_header_field_start;
break;
@ -647,18 +647,18 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
switch (ch) {
case ' ':
CALLBACK(uri);
CALLBACK(url);
CALLBACK(fragment);
state = s_http_start;
break;
case CR:
CALLBACK(uri);
CALLBACK(url);
CALLBACK(fragment);
parser->http_minor = 9;
state = s_req_line_almost_done;
break;
case LF:
CALLBACK(uri);
CALLBACK(url);
CALLBACK(fragment);
parser->http_minor = 9;
state = s_header_field_start;
@ -1201,7 +1201,7 @@ size_t http_parser_execute (http_parser *parser, const char *data, size_t len)
CALLBACK_NOCLEAR(fragment);
CALLBACK_NOCLEAR(query_string);
CALLBACK_NOCLEAR(path);
CALLBACK_NOCLEAR(uri);
CALLBACK_NOCLEAR(url);
parser->state = state;
parser->header_state = header_state;
@ -1222,7 +1222,7 @@ http_parser_init (http_parser *parser, enum http_parser_type type)
parser->on_message_begin = NULL;
parser->on_path = NULL;
parser->on_query_string = NULL;
parser->on_uri = NULL;
parser->on_url = NULL;
parser->on_fragment = NULL;
parser->on_header_field = NULL;
parser->on_header_value = NULL;

@ -52,8 +52,8 @@ struct http_parser {
size_t query_string_size;
const char *path_mark;
size_t path_size;
const char *uri_mark;
size_t uri_size;
const char *url_mark;
size_t url_size;
const char *fragment_mark;
size_t fragment_size;
@ -76,7 +76,7 @@ struct http_parser {
/* requests only */
http_data_cb on_path;
http_data_cb on_query_string;
http_data_cb on_uri;
http_data_cb on_url;
http_data_cb on_fragment;
http_data_cb on_header_field;

@ -41,7 +41,7 @@ struct message {
enum http_method method;
int status_code;
char request_path[MAX_ELEMENT_SIZE];
char request_uri[MAX_ELEMENT_SIZE];
char request_url[MAX_ELEMENT_SIZE];
char fragment[MAX_ELEMENT_SIZE];
char query_string[MAX_ELEMENT_SIZE];
char body[MAX_ELEMENT_SIZE];
@ -73,7 +73,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/test"
,.request_uri= "/test"
,.request_url= "/test"
,.num_headers= 3
,.headers=
{ { "User-Agent", "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" }
@ -101,7 +101,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/favicon.ico"
,.request_uri= "/favicon.ico"
,.request_url= "/favicon.ico"
,.num_headers= 8
,.headers=
{ { "Host", "0.0.0.0=5000" }
@ -127,7 +127,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/dumbfuck"
,.request_uri= "/dumbfuck"
,.request_url= "/dumbfuck"
,.num_headers= 1
,.headers=
{ { "aaaaaaaaaaaaa", "++++++++++" }
@ -136,7 +136,7 @@ const struct message requests[] =
}
#define FRAGMENT_IN_URI 3
, {.name= "fragment in uri"
, {.name= "fragment in url"
,.type= HTTP_REQUEST
,.raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n"
"\r\n"
@ -145,8 +145,8 @@ const struct message requests[] =
,.query_string= "page=1"
,.fragment= "posts-17408"
,.request_path= "/forums/1/topics/2375"
/* XXX request uri does include fragment? */
,.request_uri= "/forums/1/topics/2375?page=1#posts-17408"
/* XXX request url does include fragment? */
,.request_url= "/forums/1/topics/2375?page=1#posts-17408"
,.num_headers= 0
,.body= ""
}
@ -161,7 +161,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/get_no_headers_no_body/world"
,.request_uri= "/get_no_headers_no_body/world"
,.request_url= "/get_no_headers_no_body/world"
,.num_headers= 0
,.body= ""
}
@ -177,7 +177,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/get_one_header_no_body"
,.request_uri= "/get_one_header_no_body"
,.request_url= "/get_one_header_no_body"
,.num_headers= 1
,.headers=
{ { "Accept" , "*/*" }
@ -197,7 +197,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/get_funky_content_length_body_hello"
,.request_uri= "/get_funky_content_length_body_hello"
,.request_url= "/get_funky_content_length_body_hello"
,.num_headers= 1
,.headers=
{ { "conTENT-Length" , "5" }
@ -219,7 +219,7 @@ const struct message requests[] =
,.query_string= "q=search"
,.fragment= "hey"
,.request_path= "/post_identity_body_world"
,.request_uri= "/post_identity_body_world?q=search#hey"
,.request_url= "/post_identity_body_world?q=search#hey"
,.num_headers= 3
,.headers=
{ { "Accept", "*/*" }
@ -243,7 +243,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/post_chunked_all_your_base"
,.request_uri= "/post_chunked_all_your_base"
,.request_url= "/post_chunked_all_your_base"
,.num_headers= 1
,.headers=
{ { "Transfer-Encoding" , "chunked" }
@ -266,7 +266,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/two_chunks_mult_zero_end"
,.request_uri= "/two_chunks_mult_zero_end"
,.request_url= "/two_chunks_mult_zero_end"
,.num_headers= 1
,.headers=
{ { "Transfer-Encoding", "chunked" }
@ -291,7 +291,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/chunked_w_trailing_headers"
,.request_uri= "/chunked_w_trailing_headers"
,.request_url= "/chunked_w_trailing_headers"
,.num_headers= 3
,.headers=
{ { "Transfer-Encoding", "chunked" }
@ -316,7 +316,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/chunked_w_bullshit_after_length"
,.request_uri= "/chunked_w_bullshit_after_length"
,.request_url= "/chunked_w_bullshit_after_length"
,.num_headers= 1
,.headers=
{ { "Transfer-Encoding", "chunked" }
@ -333,7 +333,7 @@ const struct message requests[] =
,.query_string= "foo=\"bar\""
,.fragment= ""
,.request_path= "/with_\"stupid\"_quotes"
,.request_uri= "/with_\"stupid\"_quotes?foo=\"bar\""
,.request_url= "/with_\"stupid\"_quotes?foo=\"bar\""
,.num_headers= 0
,.headers= { }
,.body= ""
@ -351,7 +351,7 @@ const struct message requests[] =
,.query_string= ""
,.fragment= ""
,.request_path= "/test"
,.request_uri= "/test"
,.request_url= "/test"
,.num_headers= 3
,.headers= { { "Host", "0.0.0.0:5000" }
, { "User-Agent", "ApacheBench/2.3" }
@ -500,10 +500,10 @@ request_path_cb (http_parser *parser, const char *p, size_t len)
}
int
request_uri_cb (http_parser *parser, const char *p, size_t len)
request_url_cb (http_parser *parser, const char *p, size_t len)
{
assert(parser);
strncat(messages[num_messages].request_uri, p, len);
strncat(messages[num_messages].request_url, p, len);
return 0;
}
@ -602,7 +602,7 @@ parser_init (enum http_parser_type type)
parser.on_header_field = header_field_cb;
parser.on_header_value = header_value_cb;
parser.on_path = request_path_cb;
parser.on_uri = request_uri_cb;
parser.on_url = request_url_cb;
parser.on_fragment = fragment_cb;
parser.on_query_string = query_string_cb;
parser.on_body = body_cb;
@ -661,7 +661,7 @@ message_eq (int index, const struct message *expected)
MESSAGE_CHECK_STR_EQ(expected, m, request_path);
MESSAGE_CHECK_STR_EQ(expected, m, query_string);
MESSAGE_CHECK_STR_EQ(expected, m, fragment);
MESSAGE_CHECK_STR_EQ(expected, m, request_uri);
MESSAGE_CHECK_STR_EQ(expected, m, request_url);
MESSAGE_CHECK_STR_EQ(expected, m, body);
MESSAGE_CHECK_NUM_EQ(expected, m, num_headers);
@ -901,15 +901,25 @@ error:
int
main (void)
{
int i, j, k;
int request_count;
int response_count;
printf("sizeof(http_parser) = %d\n", sizeof(http_parser));
int request_count;
for (request_count = 0; requests[request_count].name; request_count++);
int response_count;
for (response_count = 0; responses[response_count].name; response_count++);
//// RESPONSES
for (i = 0; i < response_count; i++) {
test_message(&responses[i]);
}
puts("responses okay");
/// REQUESTS
@ -969,12 +979,10 @@ main (void)
/* check to make sure our predefined requests are okay */
int i;
for (i = 0; requests[i].name; i++) {
test_message(&requests[i]);
}
int j, k;
for (i = 0; i < request_count; i++) {
@ -1005,18 +1013,5 @@ main (void)
puts("requests okay");
#if 0
//// RESPONSES
for (i = 0; i < response_count; i++) {
test_message(&responses[i]);
}
puts("responses okay");
#endif
return 0;
}

Loading…
Cancel
Save