diff options
author | Paul Duncan <pabs@pablotron.org> | 2016-08-28 02:49:28 -0400 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2016-08-28 02:49:28 -0400 |
commit | e0b6b017ce80f468499ed60b1b6a95b5218fa814 (patch) | |
tree | bb5bf0b8c7c88857a0c6ceb8f9117adcb87f1519 | |
parent | ef52728f7e7461cab37ee3cbc3ef9b0d7e328c65 (diff) | |
download | libfhp-e0b6b017ce80f468499ed60b1b6a95b5218fa814.tar.bz2 libfhp-e0b6b017ce80f468499ed60b1b6a95b5218fa814.zip |
add header-value-parser.c
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | fhp.c | 110 | ||||
-rw-r--r-- | header-value-parser.c | 111 | ||||
-rw-r--r-- | internal.h | 9 |
4 files changed, 121 insertions, 111 deletions
@@ -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 @@ -1,114 +1,4 @@ #include "internal.h" -#include <stdio.h> - -// -// 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; +} + @@ -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); |