From e69874791daf88461c6813e5133bf775055ea2bf Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 03:38:59 -0400 Subject: add cl_parser test --- fhp.c | 10 ++++++++++ header-value-parser.c | 17 ++++++++++++----- include/fhp/fhp.h | 16 +++++++++++++--- test.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/fhp.c b/fhp.c index baff585..f8263c2 100644 --- a/fhp.c +++ b/fhp.c @@ -676,3 +676,13 @@ void * fhp_get_user_data(fhp_t * const fhp) { return fhp->user_data; } + +size_t +fhp_get_num_tes(fhp_t * const fhp) { + return fhp->num_tes; +} + +uint64_t +fhp_get_content_length(fhp_t * const fhp) { + return fhp->content_length; +} diff --git a/header-value-parser.c b/header-value-parser.c index d31deca..aeab7d3 100644 --- a/header-value-parser.c +++ b/header-value-parser.c @@ -29,6 +29,10 @@ fhp_header_value_parser_init(fhp_t * const fhp) { } else if (hash == fhp->env->hashes[FHP_STR_CONTENT_LENGTH]) { // set parser type parser = FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH; + + // init parser + if ((err = fhp_cl_parser_init(&(fhp->parsers.cl))) != FHP_OK) + return err; } else { // set default parser type parser = FHP_HEADER_VALUE_PARSER_NONE; @@ -54,8 +58,7 @@ fhp_header_value_parser_push( r = fhp_te_parser_push(&(fhp->parsers.te), buf, len); break; case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: - // TODO - r = FHP_OK; + r = fhp_cl_parser_push(&(fhp->parsers.cl), buf, len); break; default: // do nothing @@ -91,8 +94,13 @@ fhp_header_value_parser_done(fhp_t * const fhp) { break; case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: - // TODO - r = FHP_OK; + // finish parsing content-length and copy to context + if ((r = fhp_cl_parser_done(&(fhp->parsers.cl), &(fhp->content_length))) != FHP_OK) + return r; + + // notify callback + if (!fhp->cb(fhp, FHP_TOKEN_HEADER_CONTENT_LENGTH, 0, 0)) + return FHP_ERR_CB; break; default: @@ -108,4 +116,3 @@ fhp_header_value_parser_done(fhp_t * const fhp) { // return result return r; } - diff --git a/include/fhp/fhp.h b/include/fhp/fhp.h index 1e32a0d..2739bc9 100644 --- a/include/fhp/fhp.h +++ b/include/fhp/fhp.h @@ -120,7 +120,7 @@ void fhp_env_init(fhp_env_t * const env); fhp_env_t *fhp_get_default_env(void); // -// content length parser functions +// content-length parser functions // typedef enum { @@ -138,8 +138,17 @@ typedef struct { uint64_t val; } fhp_cl_parser_t; +fhp_err_t +fhp_cl_parser_init(fhp_cl_parser_t * const); + +fhp_err_t +fhp_cl_parser_push(fhp_cl_parser_t *, uint8_t * const, size_t); + +fhp_err_t +fhp_cl_parser_done(fhp_cl_parser_t * const, uint64_t * const); + // -// transfer encoding parser functions +// transfer-encoding parser functions // typedef enum { @@ -170,7 +179,7 @@ typedef struct { } fhp_te_parser_t; fhp_err_t -fhp_te_parser_init(fhp_te_parser_t * const te); +fhp_te_parser_init(fhp_te_parser_t * const); fhp_err_t fhp_te_parser_push(fhp_te_parser_t *, uint8_t * const, size_t); @@ -267,6 +276,7 @@ struct fhp_t_ { union { fhp_te_parser_t te; + fhp_cl_parser_t cl; } parsers; // request body type diff --git a/test.c b/test.c index 42e6f78..66ada5a 100644 --- a/test.c +++ b/test.c @@ -32,6 +32,7 @@ basic_str = "GET / HTTP/1.1\r\n" "Host: pablotron.org\r\n" "Connection: close\r\n" + "Content-Length: 123456\r\n" "Transfer-Encoding: deflate, chunked\r\n" "\r\n"; @@ -191,6 +192,52 @@ test_te_parser(void) { } } +/******************/ +/* cl parser test */ +/******************/ + +static char * +cl_parser_tests[] = { + " 12345 ", + "100", + "00120", + "123123123123", + "18446744073709551615", // UINT64_MAX + // "18446744073709551616", // UINT64_MAX + 1 + // "184--46744073709551615", // test embedded garbage + // "18446744073709551615 abc ", // test trailing garbage + NULL +}; + +static void +test_cl_parser(void) { + for (size_t i = 0; cl_parser_tests[i]; i++) { + fhp_err_t err; + + // get test string + char * const s = cl_parser_tests[i]; + + fprintf(stderr, "cl testing: \"%s\"\n", s); + + // init parser + fhp_cl_parser_t p; + fhp_cl_parser_init(&p); + + // parse data + if ((err = fhp_cl_parser_push(&p, (uint8_t*) s, strlen(s))) != FHP_OK) { + die("test_cl_parser", "fhp_cl_parser_push", err); + } + + // finish parsing + uint64_t val; + if ((err = fhp_cl_parser_done(&p, &val)) != FHP_OK) { + die("test_cl_parser", "fhp_cl_parser_push", err); + } + + fprintf(stderr, "cl test: \"%s\", val = %lu\n", s, val); + } +} + int main(int argc, char *argv[]) { UNUSED(argc); UNUSED(argv); @@ -198,6 +245,7 @@ int main(int argc, char *argv[]) { test_basic(); test_percent(); test_te_parser(); + test_cl_parser(); return EXIT_SUCCESS; } -- cgit v1.2.3