Browse Source

[request parser] adapt whitespace removal from response parser, fixes bug removing last character if CR was missing

Change-Id: Idd39363b460f1141aa2bebde187f3ca10a7bb344
master
Stefan Bühler 5 years ago
parent
commit
4375aba174
4 changed files with 81 additions and 8 deletions
  1. +1
    -0
      src/CMakeLists.txt
  2. +11
    -7
      src/main/http_request_parser.rl
  3. +7
    -1
      src/unittests/Makefile.am
  4. +62
    -0
      src/unittests/test-http-request-parser.c

+ 1
- 0
src/CMakeLists.txt View File

@@ -467,6 +467,7 @@ IF(BUILD_UNIT_TESTS)
ENDMACRO(ADD_TEST_BINARY)

ADD_TEST_BINARY(Chunk-UnitTest test-chunk unittests/test-chunk.c)
ADD_TEST_BINARY(HttpRequestParser-UnitTest test-http-request-parser unittests/test-http-request-parser.c)
ADD_TEST_BINARY(IpParser-UnitTest test-ip-parser unittests/test-ip-parser.c)
ADD_TEST_BINARY(Radix-UnitTest test-radix unittests/test-radix.c)
ADD_TEST_BINARY(RangeParser-UnitTest test-range-parser unittests/test-range-parser.c)


+ 11
- 7
src/main/http_request_parser.rl View File

@@ -31,15 +31,19 @@
li_g_string_clear(ctx->h_value);
}
action header_value {
guint i;
/* strip whitespace */
getStringTo(fpc, ctx->h_value);
/* Remove CRLF */
if (ctx->h_value->len > 2) {
ctx->h_value->len -= 2;
ctx->h_value->str[ctx->h_value->len] = '\0';
/* g_string_truncate(ctx->h_value, ctx->h_value->len - 2); */
} else {
li_g_string_clear(ctx->h_value);
for (i = ctx->h_value->len; i-- > 0; ) {
switch (ctx->h_value->str[i]) {
case '\r':
case '\n':
case ' ':
continue;
}
break;
}
g_string_truncate(ctx->h_value, i+1);
}
action header {
li_http_header_insert(ctx->request->headers, GSTR_LEN(ctx->h_key), GSTR_LEN(ctx->h_value));


+ 7
- 1
src/unittests/Makefile.am View File

@@ -5,7 +5,13 @@ AM_CFLAGS += $(GTHREAD_CFLAGS) $(GMODULE_CFLAGS) $(LIBEV_CFLAGS) $(LUA_CFLAGS)
AM_LDFLAGS = -export-dynamic -avoid-version -no-undefined $(GTHREAD_LIBS) $(GMODULE_LIBS) $(LIBEV_LIBS) $(LUA_LIBS)
LDADD = ../common/liblighttpd2-common.la ../main/liblighttpd2-shared.la

test_binaries=test-chunk test-ip-parser test-range-parser test-utils test-radix
test_binaries=\
test-chunk \
test-http-request-parser \
test-ip-parser \
test-range-parser \
test-utils \
test-radix

check_PROGRAMS=$(test_binaries)



+ 62
- 0
src/unittests/test-http-request-parser.c View File

@@ -0,0 +1,62 @@

#include <lighttpd/base.h>
#include <lighttpd/http_request_parser.h>

static void test_crlf_newlines(void) {
liRequest req;
liHttpRequestCtx http_req_ctx;
liChunkQueue* cq = li_chunkqueue_new();
liHandlerResult res;

li_chunkqueue_append_mem(cq, CONST_STR_LEN(
"GET / HTTP/1.0\r\n"
"Host: www.example.com\r\n"
"\r\n"
"\ntrash"));
li_request_init(&req);
li_http_request_parser_init(&http_req_ctx, &req, cq);

res = li_http_request_parse(NULL, &http_req_ctx);
if (LI_HANDLER_GO_ON != res) g_error("li_http_request_parse didn't finish parsing or failed: %i", res);

g_assert_true(6 == cq->length);
g_assert_true(li_http_header_is(req.headers, CONST_STR_LEN("host"), CONST_STR_LEN("www.example.com")));

li_chunkqueue_free(cq);
li_http_request_parser_clear(&http_req_ctx);
li_request_clear(&req);
}

static void test_lf_newlines(void) {
liRequest req;
liHttpRequestCtx http_req_ctx;
liChunkQueue* cq = li_chunkqueue_new();
liHandlerResult res;

li_chunkqueue_append_mem(cq, CONST_STR_LEN(
"GET / HTTP/1.0\n"
"Host: www.example.com\n"
"\n"
"\rtrash"));
li_request_init(&req);
li_http_request_parser_init(&http_req_ctx, &req, cq);

res = li_http_request_parse(NULL, &http_req_ctx);
if (LI_HANDLER_GO_ON != res) g_error("li_http_request_parse didn't finish parsing or failed: %i", res);

g_assert_true(6 == cq->length);
g_assert_true(li_http_header_is(req.headers, CONST_STR_LEN("host"), CONST_STR_LEN("www.example.com")));

li_chunkqueue_free(cq);
li_http_request_parser_clear(&http_req_ctx);
li_request_clear(&req);
}

int main(int argc, char **argv) {
g_test_init(&argc, &argv, NULL);

g_test_add_func("/http-request-parser/crlf_newlines", test_crlf_newlines);
g_test_add_func("/http-request-parser/lf_newlines", test_lf_newlines);

return g_test_run();
}

Loading…
Cancel
Save