Fix the SCGI parser and tests

This commit is contained in:
Přemysl Eric Janouch 2018-10-18 06:34:16 +02:00
parent 3e4e4e5103
commit bca7167d03
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 27 additions and 9 deletions

View File

@ -664,10 +664,11 @@ scgi_parser_push (struct scgi_parser *self,
if (digit == ':') if (digit == ':')
{ {
self->state = SCGI_READING_NAME; self->state = SCGI_READING_NAME;
str_remove_slice (&self->input, 0, 1);
break; break;
} }
if (digit < '0' || digit >= '9') if (digit < '0' || digit > '9')
return error_set (e, "invalid header netstring"); return error_set (e, "invalid header netstring");
size_t new_len = self->headers_len * 10 + (digit - '0'); size_t new_len = self->headers_len * 10 + (digit - '0');
@ -700,6 +701,7 @@ scgi_parser_push (struct scgi_parser *self,
self->state = SCGI_READING_VALUE; self->state = SCGI_READING_VALUE;
str_remove_slice (&self->input, 0, 1); str_remove_slice (&self->input, 0, 1);
self->headers_len--;
break; break;
} }
case SCGI_READING_VALUE: case SCGI_READING_VALUE:
@ -728,6 +730,7 @@ scgi_parser_push (struct scgi_parser *self,
} }
str_remove_slice (&self->input, 0, 1); str_remove_slice (&self->input, 0, 1);
self->headers_len--;
break; break;
} }
case SCGI_READING_CONTENT: case SCGI_READING_CONTENT:

View File

@ -105,10 +105,20 @@ test_http_parser (void)
http_protocol_destroy (iter); http_protocol_destroy (iter);
} }
struct scgi_fixture
{
struct scgi_parser parser;
bool seen_headers;
bool seen_content;
};
static bool static bool
test_scgi_parser_on_headers_read (void *user_data) test_scgi_parser_on_headers_read (void *user_data)
{ {
struct scgi_parser *parser = user_data; struct scgi_fixture *fixture = user_data;
struct scgi_parser *parser = &fixture->parser;
fixture->seen_headers = true;
soft_assert (parser->headers.len == 4); soft_assert (parser->headers.len == 4);
soft_assert (!strcmp (str_map_find (&parser->headers, soft_assert (!strcmp (str_map_find (&parser->headers,
"CONTENT_LENGTH"), "27")); "CONTENT_LENGTH"), "27"));
@ -124,7 +134,9 @@ test_scgi_parser_on_headers_read (void *user_data)
static bool static bool
test_scgi_parser_on_content (void *user_data, const void *data, size_t len) test_scgi_parser_on_content (void *user_data, const void *data, size_t len)
{ {
(void) user_data; struct scgi_fixture *fixture = user_data;
fixture->seen_content = true;
soft_assert (!strncmp (data, "What is the answer to life?", len)); soft_assert (!strncmp (data, "What is the answer to life?", len));
return true; return true;
} }
@ -132,10 +144,12 @@ test_scgi_parser_on_content (void *user_data, const void *data, size_t len)
static void static void
test_scgi_parser (void) test_scgi_parser (void)
{ {
struct scgi_parser parser = scgi_parser_make (); struct scgi_fixture fixture = { scgi_parser_make(), false, false };
parser.on_headers_read = test_scgi_parser_on_headers_read; struct scgi_parser *parser = &fixture.parser;
parser.on_content = test_scgi_parser_on_content;
parser.user_data = &parser; parser->on_headers_read = test_scgi_parser_on_headers_read;
parser->on_content = test_scgi_parser_on_content;
parser->user_data = &fixture;
// This is an example straight from the specification // This is an example straight from the specification
const char example[] = const char example[] =
@ -147,8 +161,9 @@ test_scgi_parser (void)
"," ","
"What is the answer to life?"; "What is the answer to life?";
soft_assert (scgi_parser_push (&parser, example, sizeof example, NULL)); soft_assert (scgi_parser_push (parser, example, sizeof example, NULL));
scgi_parser_free (&parser); soft_assert (fixture.seen_headers && fixture.seen_content);
scgi_parser_free (parser);
} }
static bool static bool