Get HTTP/1.1 message length logic working for HTTP/1.0

- Port message length logic from #72 to HTTP/1.0.
- Add a bunch of unit tests for handling 0-length messages.
v0.10
Peter Griess 13 years ago
parent c48351fbde
commit 248fbc3ab4

@ -1712,7 +1712,17 @@ http_should_keep_alive (http_parser *parser)
if (parser->flags & F_CONNECTION_CLOSE) {
return 0;
}
if (parser->type == HTTP_RESPONSE) {
} else {
/* HTTP/1.0 or earlier */
if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
return 0;
}
}
if (parser->type == HTTP_REQUEST) {
return 1;
}
/* See RFC 2616 section 4.4 */
if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
parser->status_code == 204 || /* No Content */
@ -1720,19 +1730,12 @@ http_should_keep_alive (http_parser *parser)
parser->flags & F_SKIPBODY) { /* response to a HEAD request */
return 1;
}
if (!(parser->flags & F_CHUNKED) && parser->content_length == -1) {
return 0;
}
}
return 1;
} else {
/* HTTP/1.0 or earlier */
if (parser->flags & F_CONNECTION_KEEP_ALIVE) {
if (parser->flags & F_CHUNKED || parser->content_length >= 0) {
return 1;
} else {
return 0;
}
}
return 0;
}

105
test.c

@ -1141,10 +1141,65 @@ const struct message responses[] =
,.body= "hello world"
}
#define NO_HEADERS_NO_BODY_204 13
, {.name= "204 no headers no body"
#define NO_BODY_HTTP10_KA_200 13
, {.name= "HTTP/1.0 with keep-alive and EOF-terminated 200 status"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 204 No Content\r\n\r\n"
,.raw= "HTTP/1.0 200 OK\r\n"
"Connection: keep-alive\r\n"
"\r\n"
,.should_keep_alive= FALSE
,.message_complete_on_eof= TRUE
,.http_major= 1
,.http_minor= 0
,.status_code= 200
,.num_headers= 1
,.headers=
{ { "Connection", "keep-alive" }
}
,.body_size= 0
,.body= ""
}
#define NO_BODY_HTTP10_KA_204 14
, {.name= "HTTP/1.0 with keep-alive and a 204 status"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.0 204 No content\r\n"
"Connection: keep-alive\r\n"
"\r\n"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 0
,.status_code= 204
,.num_headers= 1
,.headers=
{ { "Connection", "keep-alive" }
}
,.body_size= 0
,.body= ""
}
#define NO_BODY_HTTP11_KA_200 15
, {.name= "HTTP/1.1 with an EOF-terminated 200 status"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 200 OK\r\n"
"\r\n"
,.should_keep_alive= FALSE
,.message_complete_on_eof= TRUE
,.http_major= 1
,.http_minor= 1
,.status_code= 200
,.num_headers= 0
,.headers={}
,.body_size= 0
,.body= ""
}
#define NO_BODY_HTTP11_KA_204 16
, {.name= "HTTP/1.1 with a 204 status"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 204 No content\r\n"
"\r\n"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
@ -1156,8 +1211,48 @@ const struct message responses[] =
,.body= ""
}
#define NO_BODY_HTTP11_NOKA_204 17
, {.name= "HTTP/1.1 with a 204 status and keep-alive disabled"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 204 No content\r\n"
"Connection: close\r\n"
"\r\n"
,.should_keep_alive= FALSE
,.message_complete_on_eof= TRUE
,.http_major= 1
,.http_minor= 1
,.status_code= 204
,.num_headers= 1
,.headers=
{ { "Connection", "close" }
}
,.body_size= 0
,.body= ""
}
#define NO_BODY_HTTP11_KA_CHUNKED_200 18
, {.name= "HTTP/1.1 with chunked endocing and a 200 response"
,.type= HTTP_RESPONSE
,.raw= "HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"0\r\n"
"\r\n"
,.should_keep_alive= TRUE
,.message_complete_on_eof= FALSE
,.http_major= 1
,.http_minor= 1
,.status_code= 200
,.num_headers= 1
,.headers=
{ { "Transfer-Encoding", "chunked" }
}
,.body_size= 0
,.body= ""
}
#if !HTTP_PARSER_STRICT
#define SPACE_IN_FIELD_RES 14
#define SPACE_IN_FIELD_RES 19
/* Should handle spaces in header fields */
, {.name= "field space"
,.type= HTTP_RESPONSE
@ -2056,7 +2151,7 @@ main (void)
printf("response scan 1/2 ");
test_scan( &responses[TRAILING_SPACE_ON_CHUNKED_BODY]
, &responses[NO_HEADERS_NO_BODY_204]
, &responses[NO_BODY_HTTP10_KA_204]
, &responses[NO_REASON_PHRASE]
);

Loading…
Cancel
Save