From e0b6b017ce80f468499ed60b1b6a95b5218fa814 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 02:49:28 -0400 Subject: add header-value-parser.c --- Makefile | 2 +- fhp.c | 110 ------------------------------------------------- header-value-parser.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ internal.h | 9 ++++ 4 files changed, 121 insertions(+), 111 deletions(-) create mode 100644 header-value-parser.c diff --git a/Makefile b/Makefile index c76e497..0ab3d8f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC ?= cc CFLAGS=-std=c99 -W -Wall -pedantic -O2 -Iinclude -fPIC -OBJS=fhp.o hash.o error.o token.o env.o te-parser.o +OBJS=fhp.o hash.o error.o token.o env.o te-parser.o header-value-parser.o SONAME=libfhp.so LIB=libfhp.so STATIC_LIB=libfhp.a diff --git a/fhp.c b/fhp.c index 6048b2b..baff585 100644 --- a/fhp.c +++ b/fhp.c @@ -1,114 +1,4 @@ #include "internal.h" -#include - -// -// header value parser -// - -static fhp_err_t -fhp_header_value_parser_init(fhp_t * const fhp) { - uint32_t hash = fhp->header_name_hash; - fhp_header_value_parser_t parser = FHP_HEADER_VALUE_PARSER_NONE; - fhp_err_t err; - - /* fprintf( - stderr, - "hashes: header = %u, env = %u, str = %u, str(lc) = %u\n", - hash, - fhp->env->hashes[FHP_STR_TRANSFER_ENCODING], - fhp_hash_string("Transfer-Encoding"), - fhp_lc_hash_string("Transfer-Encoding") - ); */ - - if (hash == fhp->env->hashes[FHP_STR_TRANSFER_ENCODING]) { - // set parser type - parser = FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING; - - // init parser - if ((err = fhp_te_parser_init(&(fhp->parsers.te))) != FHP_OK) - return err; - } else if (hash == fhp->env->hashes[FHP_STR_CONTENT_LENGTH]) { - // set parser type - parser = FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH; - } else { - // set default parser type - parser = FHP_HEADER_VALUE_PARSER_NONE; - } - - // set value parser - fhp->header_value_parser = parser; - - // return success - return FHP_OK; -} - -static fhp_err_t -fhp_header_value_parser_push( - fhp_t * const fhp, - uint8_t * const buf, - size_t len -) { - fhp_err_t r = FHP_OK; - - switch (fhp->header_value_parser) { - case FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING: - r = fhp_te_parser_push(&(fhp->parsers.te), buf, len); - break; - case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: - // TODO - r = FHP_OK; - break; - default: - // do nothing - r = FHP_OK; - break; - } - - // return result - return r; -} - -static fhp_err_t -fhp_header_value_parser_done(fhp_t * const fhp) { - fhp_err_t r = FHP_OK; - - switch (fhp->header_value_parser) { - case FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING: - // finish parsing tes - if ((r = fhp_te_parser_done(&(fhp->parsers.te), &(fhp->num_tes))) != FHP_OK) - return r; - - // check number of tes - if (fhp->num_tes > FHP_MAX_TRANSFER_ENCODINGS) - return FHP_ERR_TOO_MANY_TES; - - // copy tes to context - if ((r = fhp_te_parser_get_tes(&(fhp->parsers.te), fhp->tes, FHP_MAX_TRANSFER_ENCODINGS)) != FHP_OK) - return r; - - // notify callback - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_TRANSFER_ENCODING, 0, 0)) - return FHP_ERR_CB; - - break; - case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: - // TODO - r = FHP_OK; - - break; - default: - // do nothing - r = FHP_OK; - - break; - } - - // clear header value parser - fhp->header_value_parser = FHP_HEADER_VALUE_PARSER_NONE; - - // return result - return r; -} // // context functions diff --git a/header-value-parser.c b/header-value-parser.c new file mode 100644 index 0000000..d31deca --- /dev/null +++ b/header-value-parser.c @@ -0,0 +1,111 @@ +#include "internal.h" + +// +// header value parser +// + +fhp_err_t +fhp_header_value_parser_init(fhp_t * const fhp) { + uint32_t hash = fhp->header_name_hash; + fhp_header_value_parser_t parser = FHP_HEADER_VALUE_PARSER_NONE; + fhp_err_t err; + + /* fprintf( + stderr, + "hashes: header = %u, env = %u, str = %u, str(lc) = %u\n", + hash, + fhp->env->hashes[FHP_STR_TRANSFER_ENCODING], + fhp_hash_string("Transfer-Encoding"), + fhp_lc_hash_string("Transfer-Encoding") + ); */ + + if (hash == fhp->env->hashes[FHP_STR_TRANSFER_ENCODING]) { + // set parser type + parser = FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING; + + // init parser + if ((err = fhp_te_parser_init(&(fhp->parsers.te))) != FHP_OK) + return err; + } else if (hash == fhp->env->hashes[FHP_STR_CONTENT_LENGTH]) { + // set parser type + parser = FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH; + } else { + // set default parser type + parser = FHP_HEADER_VALUE_PARSER_NONE; + } + + // set value parser + fhp->header_value_parser = parser; + + // return success + return FHP_OK; +} + +fhp_err_t +fhp_header_value_parser_push( + fhp_t * const fhp, + uint8_t * const buf, + size_t len +) { + fhp_err_t r = FHP_OK; + + switch (fhp->header_value_parser) { + case FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING: + r = fhp_te_parser_push(&(fhp->parsers.te), buf, len); + break; + case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: + // TODO + r = FHP_OK; + break; + default: + // do nothing + r = FHP_OK; + break; + } + + // return result + return r; +} + +fhp_err_t +fhp_header_value_parser_done(fhp_t * const fhp) { + fhp_err_t r = FHP_OK; + + switch (fhp->header_value_parser) { + case FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING: + // finish parsing tes + if ((r = fhp_te_parser_done(&(fhp->parsers.te), &(fhp->num_tes))) != FHP_OK) + return r; + + // check number of tes + if (fhp->num_tes > FHP_MAX_TRANSFER_ENCODINGS) + return FHP_ERR_TOO_MANY_TES; + + // copy tes to context + if ((r = fhp_te_parser_get_tes(&(fhp->parsers.te), fhp->tes, FHP_MAX_TRANSFER_ENCODINGS)) != FHP_OK) + return r; + + // notify callback + if (!fhp->cb(fhp, FHP_TOKEN_HEADER_TRANSFER_ENCODING, 0, 0)) + return FHP_ERR_CB; + + break; + case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: + // TODO + r = FHP_OK; + + break; + default: + // do nothing + r = FHP_OK; + + break; + } + + // clear header value parser + fhp->header_value_parser = FHP_HEADER_VALUE_PARSER_NONE; + + // return result + return r; +} + diff --git a/internal.h b/internal.h index 94caebd..e393f4b 100644 --- a/internal.h +++ b/internal.h @@ -198,3 +198,12 @@ typedef enum { FHP_STR_CHUNKED, FHP_STR_LAST } fhp_str_t; + +fhp_err_t +fhp_header_value_parser_init(fhp_t * const); + +fhp_err_t +fhp_header_value_parser_push(fhp_t * const, uint8_t * const, size_t); + +fhp_err_t +fhp_header_value_parser_done(fhp_t * const); -- cgit v1.2.3