From 0485651622e4078532d8d834668b2ec9c255a820 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 15:31:47 -0400 Subject: s/fhp_t/fhp_ctx_t/g --- fhp.c | 278 +++++++++++++++++++++++++------------------------- header-value-parser.c | 48 ++++----- include/fhp/fhp.h | 22 ++-- internal.h | 6 +- test.c | 28 ++--- 5 files changed, 191 insertions(+), 191 deletions(-) diff --git a/fhp.c b/fhp.c index 1eceeee..e73fba8 100644 --- a/fhp.c +++ b/fhp.c @@ -4,7 +4,7 @@ // context functions // -static const fhp_t FHP_DEFAULT_CONTEXT = { +static const fhp_ctx_t FHP_DEFAULT_CONTEXT = { .state = FHP_STATE_INIT, .user_data = NULL, .cb = NULL, @@ -20,48 +20,48 @@ static const fhp_t FHP_DEFAULT_CONTEXT = { }; fhp_err_t -fhp_init( - fhp_t * const fhp, +fhp_ctx_init( + fhp_ctx_t * const ctx, fhp_env_t * const env, fhp_cb_t cb, void * const user_data ) { - *fhp = FHP_DEFAULT_CONTEXT; - fhp->env = env ? env : fhp_get_default_env(); - fhp->user_data = user_data; - fhp->cb = cb; + *ctx = FHP_DEFAULT_CONTEXT; + ctx->env = env ? env : fhp_get_default_env(); + ctx->user_data = user_data; + ctx->cb = cb; /* return success */ return FHP_OK; } static void -fhp_buf_clear(fhp_t * const fhp) { +fhp_ctx_buf_clear(fhp_ctx_t * const ctx) { // clear buffer - fhp->buf_len = 0; + ctx->buf_len = 0; } static fhp_err_t -fhp_buf_flush( - fhp_t * const fhp, +fhp_ctx_buf_flush( + fhp_ctx_t * const ctx, fhp_token_t token ) { - if (fhp->buf_len > 0) { + if (ctx->buf_len > 0) { // push data - if (!fhp->cb(fhp, token, fhp->buf, fhp->buf_len)) + if (!ctx->cb(ctx, token, ctx->buf, ctx->buf_len)) return FHP_ERR_CB; // update buffer hash - if (fhp->is_hashing) - fhp->buf_hash = fhp_lc_hash_push(fhp->buf_hash, fhp->buf, fhp->buf_len); + if (ctx->is_hashing) + ctx->buf_hash = fhp_lc_hash_push(ctx->buf_hash, ctx->buf, ctx->buf_len); // push to header value parser fhp_err_t err; - if ((err = fhp_header_value_parser_push(fhp, fhp->buf, fhp->buf_len)) != FHP_OK) + if ((err = fhp_header_value_parser_push(ctx, ctx->buf, ctx->buf_len)) != FHP_OK) return err; // clear buffer - fhp_buf_clear(fhp); + fhp_ctx_buf_clear(ctx); } // return success @@ -69,46 +69,46 @@ fhp_buf_flush( } static fhp_err_t -fhp_buf_push( - fhp_t * const fhp, +fhp_ctx_buf_push( + fhp_ctx_t * const ctx, fhp_token_t token, uint8_t byte ) { // flush buffer - if (fhp->buf_len + 1 >= FHP_MAX_BUF_SIZE) { + if (ctx->buf_len + 1 >= FHP_CTX_MAX_BUF_SIZE) { fhp_err_t err; - if ((err = fhp_buf_flush(fhp, token)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, token)) != FHP_OK) return err; } // append to buffer - fhp->buf[fhp->buf_len] = byte; - fhp->buf_len++; + ctx->buf[ctx->buf_len] = byte; + ctx->buf_len++; // return success return FHP_OK; } static fhp_err_t -fhp_handle_method(fhp_t * const fhp) { +fhp_ctx_handle_method(fhp_ctx_t * const ctx) { // get method token - fhp->http_method = FHP_TOKEN_METHOD_OTHER; - if (fhp->buf_hash == fhp->env->hashes[FHP_STR_GET]) { - fhp->http_method = FHP_TOKEN_METHOD_GET; - } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_POST]) { - fhp->http_method = FHP_TOKEN_METHOD_POST; - } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_HEAD]) { - fhp->http_method = FHP_TOKEN_METHOD_HEAD; - } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_PUT]) { - fhp->http_method = FHP_TOKEN_METHOD_PUT; - } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_DELETE]) { - fhp->http_method = FHP_TOKEN_METHOD_DELETE; - } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_OPTIONS]) { - fhp->http_method = FHP_TOKEN_METHOD_OPTIONS; + ctx->http_method = FHP_TOKEN_METHOD_OTHER; + if (ctx->buf_hash == ctx->env->hashes[FHP_STR_GET]) { + ctx->http_method = FHP_TOKEN_METHOD_GET; + } else if (ctx->buf_hash == ctx->env->hashes[FHP_STR_POST]) { + ctx->http_method = FHP_TOKEN_METHOD_POST; + } else if (ctx->buf_hash == ctx->env->hashes[FHP_STR_HEAD]) { + ctx->http_method = FHP_TOKEN_METHOD_HEAD; + } else if (ctx->buf_hash == ctx->env->hashes[FHP_STR_PUT]) { + ctx->http_method = FHP_TOKEN_METHOD_PUT; + } else if (ctx->buf_hash == ctx->env->hashes[FHP_STR_DELETE]) { + ctx->http_method = FHP_TOKEN_METHOD_DELETE; + } else if (ctx->buf_hash == ctx->env->hashes[FHP_STR_OPTIONS]) { + ctx->http_method = FHP_TOKEN_METHOD_OPTIONS; } // send method token - if (!fhp->cb(fhp, fhp->http_method, 0, 0)) + if (!ctx->cb(ctx, ctx->http_method, 0, 0)) return FHP_ERR_CB; // return success @@ -116,27 +116,27 @@ fhp_handle_method(fhp_t * const fhp) { } static fhp_err_t -fhp_push_byte( - fhp_t * const fhp, +fhp_ctx_push_byte( + fhp_ctx_t * const ctx, uint8_t byte ) { fhp_err_t err; retry: - switch (fhp->state) { + switch (ctx->state) { case FHP_STATE_INIT: switch (byte) { CASE_TOKEN_CHARS // send start token - if (!fhp->cb(fhp, FHP_TOKEN_METHOD_START, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_METHOD_START, 0, 0)) return FHP_ERR_CB; // enable buffer hashing - fhp->is_hashing = true; - fhp->buf_hash = fhp_hash_init(); + ctx->is_hashing = true; + ctx->buf_hash = fhp_hash_init(); // set state - fhp->state = FHP_STATE_METHOD; + ctx->state = FHP_STATE_METHOD; goto retry; break; @@ -149,29 +149,29 @@ retry: switch (byte) { CASE_TOKEN_CHARS // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_METHOD_FRAGMENT, byte)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_METHOD_FRAGMENT, byte)) != FHP_OK) return err; break; case ' ': // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_METHOD_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_METHOD_FRAGMENT)) != FHP_OK) return err; // disable buffer hashing - fhp->is_hashing = false; + ctx->is_hashing = false; // send end token - if (!fhp->cb(fhp, FHP_TOKEN_METHOD_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_METHOD_END, 0, 0)) return FHP_ERR_CB; // handle method - fhp_err_t err = fhp_handle_method(fhp); + fhp_err_t err = fhp_ctx_handle_method(ctx); if (err != FHP_OK) return err; // set state - fhp->state = FHP_STATE_METHOD_END; + ctx->state = FHP_STATE_METHOD_END; goto retry; break; @@ -187,10 +187,10 @@ retry: // ignore break; CASE_URL_CHARS - if (!fhp->cb(fhp, FHP_TOKEN_URL_START, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_URL_START, 0, 0)) return FHP_ERR_CB; - fhp->state = FHP_STATE_URL; + ctx->state = FHP_STATE_URL; goto retry; default: return FHP_ERR_INVALID_CHAR_IN_URL; @@ -201,26 +201,26 @@ retry: switch (byte) { case ' ': // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_URL_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_URL_FRAGMENT)) != FHP_OK) return err; // send end token - if (!fhp->cb(fhp, FHP_TOKEN_URL_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_URL_END, 0, 0)) return FHP_ERR_CB; // set state - fhp->state = FHP_STATE_URL_END; + ctx->state = FHP_STATE_URL_END; goto retry; break; case '%': // set state - fhp->state = FHP_STATE_URL_PERCENT; + ctx->state = FHP_STATE_URL_PERCENT; break; CASE_URL_CHARS // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_URL_FRAGMENT, byte)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_URL_FRAGMENT, byte)) != FHP_OK) return err; } @@ -228,13 +228,13 @@ retry: case FHP_STATE_URL_PERCENT: switch (byte) { CASE_DIGIT_CHARS - fhp->hex = byte - '0'; - fhp->state = FHP_STATE_URL_PERCENT_LAST; + ctx->hex = byte - '0'; + ctx->state = FHP_STATE_URL_PERCENT_LAST; break; CASE_HEX_ALPHA_CHARS - fhp->hex = 10 + byte - ((byte >= 'a') ? 'a' : 'A'); - fhp->state = FHP_STATE_URL_PERCENT_LAST; + ctx->hex = 10 + byte - ((byte >= 'a') ? 'a' : 'A'); + ctx->state = FHP_STATE_URL_PERCENT_LAST; break; default: @@ -245,21 +245,21 @@ retry: case FHP_STATE_URL_PERCENT_LAST: switch (byte) { CASE_DIGIT_CHARS - fhp->hex = (fhp->hex << 4) + (byte - '0'); + ctx->hex = (ctx->hex << 4) + (byte - '0'); break; CASE_HEX_ALPHA_CHARS - fhp->hex = (fhp->hex << 4) + (10 + byte - ((byte >= 'a') ? 'a' : 'A')); + ctx->hex = (ctx->hex << 4) + (10 + byte - ((byte >= 'a') ? 'a' : 'A')); break; default: return FHP_ERR_INVALID_CHAR_IN_URL_PERCENT; } // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_URL_FRAGMENT, fhp->hex)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_URL_FRAGMENT, ctx->hex)) != FHP_OK) return err; // set state - fhp->state = FHP_STATE_URL; + ctx->state = FHP_STATE_URL; break; case FHP_STATE_URL_END: @@ -268,15 +268,15 @@ retry: // ignore break; default: - if (!fhp->cb(fhp, FHP_TOKEN_VERSION_START, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_VERSION_START, 0, 0)) return FHP_ERR_CB; // enable buffer hashing - fhp->is_hashing = true; - fhp->buf_hash = fhp_hash_init(); + ctx->is_hashing = true; + ctx->buf_hash = fhp_hash_init(); // set state - fhp->state = FHP_STATE_VERSION; + ctx->state = FHP_STATE_VERSION; goto retry; } @@ -286,36 +286,36 @@ retry: case '\r': case '\n': // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_VERSION_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_VERSION_FRAGMENT)) != FHP_OK) return err; // send end token - if (!fhp->cb(fhp, FHP_TOKEN_VERSION_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_VERSION_END, 0, 0)) return FHP_ERR_CB; // disable buffer hashing - fhp->is_hashing = false; + ctx->is_hashing = false; // get version token - fhp->http_version = FHP_TOKEN_VERSION_OTHER; - if (fhp->buf_hash == fhp->env->hashes[FHP_STR_HTTP_10]) { - fhp->http_version = FHP_TOKEN_VERSION_HTTP_10; - } else if (fhp->buf_hash == fhp->env->hashes[FHP_STR_HTTP_11]) { - fhp->http_version = FHP_TOKEN_VERSION_HTTP_11; + ctx->http_version = FHP_TOKEN_VERSION_OTHER; + if (ctx->buf_hash == ctx->env->hashes[FHP_STR_HTTP_10]) { + ctx->http_version = FHP_TOKEN_VERSION_HTTP_10; + } else if (ctx->buf_hash == ctx->env->hashes[FHP_STR_HTTP_11]) { + ctx->http_version = FHP_TOKEN_VERSION_HTTP_11; } // send version token - if (!fhp->cb(fhp, fhp->http_version, 0, 0)) + if (!ctx->cb(ctx, ctx->http_version, 0, 0)) return FHP_ERR_CB; // set state - fhp->state = FHP_STATE_VERSION_END; + ctx->state = FHP_STATE_VERSION_END; goto retry; break; CASE_VERSION_CHARS // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_VERSION_FRAGMENT, byte)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_VERSION_FRAGMENT, byte)) != FHP_OK) return err; break; @@ -327,11 +327,11 @@ retry: case FHP_STATE_VERSION_END: switch (byte) { case '\r': - fhp->state = FHP_STATE_VERSION_END_CR; + ctx->state = FHP_STATE_VERSION_END_CR; break; case '\n': - fhp->state = FHP_STATE_STATUS_END; + ctx->state = FHP_STATE_STATUS_END; break; default: @@ -344,7 +344,7 @@ retry: case FHP_STATE_VERSION_END_CR: switch (byte) { case '\n': - fhp->state = FHP_STATE_STATUS_END; + ctx->state = FHP_STATE_STATUS_END; break; default: @@ -356,26 +356,26 @@ retry: switch (byte) { CASE_TOKEN_CHARS // send start token - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_NAME_START, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_NAME_START, 0, 0)) return FHP_ERR_CB; // enable buffer hashing - fhp->is_hashing = true; - fhp->buf_hash = fhp_hash_init(); + ctx->is_hashing = true; + ctx->buf_hash = fhp_hash_init(); // set state - fhp->state = FHP_STATE_HEADER_NAME; + ctx->state = FHP_STATE_HEADER_NAME; goto retry; break; case '\r': // set state - fhp->state = FHP_STATE_STATUS_END_CR; + ctx->state = FHP_STATE_STATUS_END_CR; break; case '\n': // set state - fhp->state = FHP_STATE_HEADERS_END; + ctx->state = FHP_STATE_HEADERS_END; break; } @@ -385,7 +385,7 @@ retry: switch (byte) { case '\n': // set state - fhp->state = FHP_STATE_HEADERS_END; + ctx->state = FHP_STATE_HEADERS_END; break; default: @@ -397,25 +397,25 @@ retry: switch (byte) { CASE_TOKEN_CHARS // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_HEADER_NAME_FRAGMENT, byte)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_HEADER_NAME_FRAGMENT, byte)) != FHP_OK) return err; break; case ':': // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_HEADER_NAME_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_HEADER_NAME_FRAGMENT)) != FHP_OK) return err; // disable buffer hashing and cache header name hash - fhp->is_hashing = false; - fhp->header_name_hash = fhp->buf_hash; + ctx->is_hashing = false; + ctx->header_name_hash = ctx->buf_hash; // send end token - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_NAME_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_NAME_END, 0, 0)) return FHP_ERR_CB; // set state - fhp->state = FHP_STATE_HEADER_NAME_END; + ctx->state = FHP_STATE_HEADER_NAME_END; break; default: @@ -431,16 +431,16 @@ retry: break; default: // send start token - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_START, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_VALUE_START, 0, 0)) return FHP_ERR_CB; // init header value parser fhp_err_t err; - if ((err = fhp_header_value_parser_init(fhp)) != FHP_OK) + if ((err = fhp_header_value_parser_init(ctx)) != FHP_OK) return err; // set state - fhp->state = FHP_STATE_HEADER_VALUE; + ctx->state = FHP_STATE_HEADER_VALUE; goto retry; break; @@ -450,17 +450,17 @@ retry: case FHP_STATE_HEADER_VALUE: switch (byte) { case '\r': - fhp->state = FHP_STATE_HEADER_VALUE_END_CR; + ctx->state = FHP_STATE_HEADER_VALUE_END_CR; break; case '\n': - fhp->state = FHP_STATE_HEADER_VALUE_END; + ctx->state = FHP_STATE_HEADER_VALUE_END; break; default: // FIXME: need more limits on valid octets // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT, byte)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_HEADER_VALUE_FRAGMENT, byte)) != FHP_OK) return err; break; @@ -470,7 +470,7 @@ retry: case FHP_STATE_HEADER_VALUE_END_CR: switch (byte) { case '\n': - fhp->state = FHP_STATE_HEADER_VALUE_END; + ctx->state = FHP_STATE_HEADER_VALUE_END; break; default: return FHP_ERR_INVALID_CHAR; @@ -485,74 +485,74 @@ retry: // add space to buffer // folding to ' ', as per RFC7230 3.2.4 // https://tools.ietf.org/html/rfc7230#section-3.2.4 - if ((err = fhp_buf_push(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT, ' ')) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_HEADER_VALUE_FRAGMENT, ' ')) != FHP_OK) return err; // set state - fhp->state = FHP_STATE_HEADER_VALUE; + ctx->state = FHP_STATE_HEADER_VALUE; break; CASE_TOKEN_CHARS // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_HEADER_VALUE_FRAGMENT)) != FHP_OK) return err; // end header value - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_VALUE_END, 0, 0)) return FHP_ERR_CB; // end header value parser - if ((err = fhp_header_value_parser_done(fhp)) != FHP_OK) + if ((err = fhp_header_value_parser_done(ctx)) != FHP_OK) return err; // send start token - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_NAME_START, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_NAME_START, 0, 0)) return FHP_ERR_CB; // enable buffer hashing - fhp->is_hashing = true; - fhp->buf_hash = fhp_hash_init(); + ctx->is_hashing = true; + ctx->buf_hash = fhp_hash_init(); // add to buffer - if ((err = fhp_buf_push(fhp, FHP_TOKEN_HEADER_NAME_FRAGMENT, byte)) != FHP_OK) + if ((err = fhp_ctx_buf_push(ctx, FHP_TOKEN_HEADER_NAME_FRAGMENT, byte)) != FHP_OK) return err; // set state - fhp->state = FHP_STATE_HEADER_NAME; + ctx->state = FHP_STATE_HEADER_NAME; break; case '\r': // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_HEADER_VALUE_FRAGMENT)) != FHP_OK) return err; // end header value - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_VALUE_END, 0, 0)) return FHP_ERR_CB; // end header value parser - if ((err = fhp_header_value_parser_done(fhp)) != FHP_OK) + if ((err = fhp_header_value_parser_done(ctx)) != FHP_OK) return err; // set state - fhp->state = FHP_STATE_HEADERS_END_CR; + ctx->state = FHP_STATE_HEADERS_END_CR; break; case '\n': // flush buffer - if ((err = fhp_buf_flush(fhp, FHP_TOKEN_HEADER_VALUE_FRAGMENT)) != FHP_OK) + if ((err = fhp_ctx_buf_flush(ctx, FHP_TOKEN_HEADER_VALUE_FRAGMENT)) != FHP_OK) return err; // end header value - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_VALUE_END, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_VALUE_END, 0, 0)) return FHP_ERR_CB; // end header value parser - if ((err = fhp_header_value_parser_done(fhp)) != FHP_OK) + if ((err = fhp_header_value_parser_done(ctx)) != FHP_OK) return err; // set state - fhp->state = FHP_STATE_HEADERS_END; + ctx->state = FHP_STATE_HEADERS_END; break; default: @@ -563,7 +563,7 @@ retry: case FHP_STATE_HEADERS_END_CR: switch (byte) { case '\n': - fhp->state = FHP_STATE_HEADERS_END; + ctx->state = FHP_STATE_HEADERS_END; break; default: @@ -582,32 +582,32 @@ retry: } // increment byte offset - fhp->ofs++; + ctx->ofs++; /* return success */ return FHP_OK; } fhp_err_t -fhp_push( - fhp_t * const fhp, +fhp_ctx_push( + fhp_ctx_t * const ctx, uint8_t * const buf, size_t len ) { - switch (fhp->state) { + switch (ctx->state) { case FHP_STATE_ERROR: - return fhp->err; + return ctx->err; break; default: for (size_t i = 0; i < len; i++) { // push byte - fhp_err_t err = fhp_push_byte(fhp, buf[i]); + fhp_err_t err = fhp_ctx_push_byte(ctx, buf[i]); // check result if (err != FHP_OK) { - fhp->state = FHP_STATE_ERROR; - fhp->err = err; + ctx->state = FHP_STATE_ERROR; + ctx->err = err; return err; } } @@ -618,21 +618,21 @@ fhp_push( } fhp_env_t * -fhp_get_env(fhp_t * const fhp) { - return fhp->env; +fhp_ctx_get_env(fhp_ctx_t * const ctx) { + return ctx->env; } void * -fhp_get_user_data(fhp_t * const fhp) { - return fhp->user_data; +fhp_ctx_get_user_data(fhp_ctx_t * const ctx) { + return ctx->user_data; } size_t -fhp_get_num_tes(fhp_t * const fhp) { - return fhp->num_tes; +fhp_ctx_get_num_tes(fhp_ctx_t * const ctx) { + return ctx->num_tes; } uint64_t -fhp_get_content_length(fhp_t * const fhp) { - return fhp->content_length; +fhp_ctx_get_content_length(fhp_ctx_t * const ctx) { + return ctx->content_length; } diff --git a/header-value-parser.c b/header-value-parser.c index c1835a5..2a1bf74 100644 --- a/header-value-parser.c +++ b/header-value-parser.c @@ -5,8 +5,8 @@ // fhp_err_t -fhp_header_value_parser_init(fhp_t * const fhp) { - uint32_t hash = fhp->header_name_hash; +fhp_header_value_parser_init(fhp_ctx_t * const ctx) { + uint32_t hash = ctx->header_name_hash; fhp_header_value_parser_t parser = FHP_HEADER_VALUE_PARSER_NONE; fhp_err_t err; @@ -19,19 +19,19 @@ fhp_header_value_parser_init(fhp_t * const fhp) { fhp_lc_hash_string("Transfer-Encoding") ); */ - if (hash == fhp->env->hashes[FHP_STR_TRANSFER_ENCODING]) { + if (hash == ctx->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) + if ((err = fhp_te_parser_init(&(ctx->parsers.te))) != FHP_OK) return err; - } else if (hash == fhp->env->hashes[FHP_STR_CONTENT_LENGTH]) { + } else if (hash == ctx->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) + if ((err = fhp_cl_parser_init(&(ctx->parsers.cl))) != FHP_OK) return err; } else { // set default parser type @@ -39,7 +39,7 @@ fhp_header_value_parser_init(fhp_t * const fhp) { } // set value parser - fhp->header_value_parser = parser; + ctx->header_value_parser = parser; // return success return FHP_OK; @@ -47,18 +47,18 @@ fhp_header_value_parser_init(fhp_t * const fhp) { fhp_err_t fhp_header_value_parser_push( - fhp_t * const fhp, + fhp_ctx_t * const ctx, uint8_t * const buf, size_t len ) { fhp_err_t r = FHP_OK; - switch (fhp->header_value_parser) { + switch (ctx->header_value_parser) { case FHP_HEADER_VALUE_PARSER_TRANSFER_ENCODING: - r = fhp_te_parser_push(&(fhp->parsers.te), buf, len); + r = fhp_te_parser_push(&(ctx->parsers.te), buf, len); break; case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: - r = fhp_cl_parser_push(&(fhp->parsers.cl), buf, len); + r = fhp_cl_parser_push(&(ctx->parsers.cl), buf, len); break; default: // do nothing @@ -71,30 +71,30 @@ fhp_header_value_parser_push( } fhp_err_t -fhp_header_value_parser_done(fhp_t * const fhp) { +fhp_header_value_parser_done(fhp_ctx_t * const ctx) { fhp_err_t r = FHP_OK; - switch (fhp->header_value_parser) { + switch (ctx->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) + if ((r = fhp_te_parser_done(&(ctx->parsers.te), &(ctx->num_tes))) != FHP_OK) return r; // check number of tes - if (fhp->num_tes > FHP_MAX_TRANSFER_ENCODINGS) + if (ctx->num_tes > FHP_CTX_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) + if ((r = fhp_te_parser_get_tes(&(ctx->parsers.te), ctx->tes, FHP_CTX_MAX_TRANSFER_ENCODINGS)) != FHP_OK) return r; // update body type // FIXME: follow RFC7230 3.3.3 // https://tools.ietf.org/html/rfc7230#section-3.3.3 - switch (fhp->body_type) { + switch (ctx->body_type) { case FHP_BODY_TYPE_NONE: // set body type - fhp->body_type = FHP_BODY_TYPE_CONTENT_LENGTH; + ctx->body_type = FHP_BODY_TYPE_CONTENT_LENGTH; break; case FHP_BODY_TYPE_CONTENT_LENGTH: @@ -113,22 +113,22 @@ fhp_header_value_parser_done(fhp_t * const fhp) { } // notify callback - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_TRANSFER_ENCODING, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_TRANSFER_ENCODING, 0, 0)) return FHP_ERR_CB; break; case FHP_HEADER_VALUE_PARSER_CONTENT_LENGTH: // finish parsing content-length and copy to context - if ((r = fhp_cl_parser_done(&(fhp->parsers.cl), &(fhp->content_length))) != FHP_OK) + if ((r = fhp_cl_parser_done(&(ctx->parsers.cl), &(ctx->content_length))) != FHP_OK) return r; // update body type // FIXME: follow RFC7230 3.3.3 // https://tools.ietf.org/html/rfc7230#section-3.3.3 - switch (fhp->body_type) { + switch (ctx->body_type) { case FHP_BODY_TYPE_NONE: // set body type - fhp->body_type = FHP_BODY_TYPE_TRANSFER_ENCODING; + ctx->body_type = FHP_BODY_TYPE_TRANSFER_ENCODING; break; case FHP_BODY_TYPE_CONTENT_LENGTH: @@ -147,7 +147,7 @@ fhp_header_value_parser_done(fhp_t * const fhp) { } // notify callback - if (!fhp->cb(fhp, FHP_TOKEN_HEADER_CONTENT_LENGTH, 0, 0)) + if (!ctx->cb(ctx, FHP_TOKEN_HEADER_CONTENT_LENGTH, 0, 0)) return FHP_ERR_CB; break; @@ -159,7 +159,7 @@ fhp_header_value_parser_done(fhp_t * const fhp) { } // clear header value parser - fhp->header_value_parser = FHP_HEADER_VALUE_PARSER_NONE; + ctx->header_value_parser = FHP_HEADER_VALUE_PARSER_NONE; // return result return r; diff --git a/include/fhp/fhp.h b/include/fhp/fhp.h index 7f5d66f..639eb0f 100644 --- a/include/fhp/fhp.h +++ b/include/fhp/fhp.h @@ -194,10 +194,10 @@ fhp_te_parser_get_tes(fhp_te_parser_t * const, uint32_t * const, size_t); // context functions // -typedef struct fhp_t_ fhp_t; +typedef struct fhp_ctx_t_ fhp_ctx_t; typedef bool (*fhp_cb_t)( - fhp_t * const, + fhp_ctx_t * const, fhp_token_t, uint8_t * const, size_t @@ -235,10 +235,10 @@ typedef enum { FHP_HEADER_VALUE_PARSER_LAST } fhp_header_value_parser_t; -#define FHP_MAX_BUF_SIZE 1024 -#define FHP_MAX_TRANSFER_ENCODINGS 4 +#define FHP_CTX_MAX_BUF_SIZE 1024 +#define FHP_CTX_MAX_TRANSFER_ENCODINGS 4 -struct fhp_t_ { +struct fhp_ctx_t_ { // env pointer fhp_env_t *env; @@ -258,7 +258,7 @@ struct fhp_t_ { uint64_t ofs; // fragment data buffer - uint8_t buf[FHP_MAX_BUF_SIZE]; + uint8_t buf[FHP_CTX_MAX_BUF_SIZE]; size_t buf_len; // buffer hashing state @@ -286,7 +286,7 @@ struct fhp_t_ { uint64_t content_length; // transfer encodings - uint32_t tes[FHP_MAX_TRANSFER_ENCODINGS]; + uint32_t tes[FHP_CTX_MAX_TRANSFER_ENCODINGS]; size_t num_tes; // state for url hex decoder @@ -294,15 +294,15 @@ struct fhp_t_ { }; fhp_err_t -fhp_init(fhp_t * const, fhp_env_t * const, fhp_cb_t, void * const); +fhp_ctx_init(fhp_ctx_t * const, fhp_env_t * const, fhp_cb_t, void * const); fhp_err_t -fhp_push(fhp_t * const, uint8_t * const, size_t); +fhp_ctx_push(fhp_ctx_t * const, uint8_t * const, size_t); fhp_env_t * -fhp_get_env(fhp_t * const); +fhp_ctx_get_env(fhp_ctx_t * const); void * -fhp_get_user_data(fhp_t * const); +fhp_ctx_get_user_data(fhp_ctx_t * const); #endif /* FHP_H */ diff --git a/internal.h b/internal.h index e393f4b..994b208 100644 --- a/internal.h +++ b/internal.h @@ -200,10 +200,10 @@ typedef enum { } fhp_str_t; fhp_err_t -fhp_header_value_parser_init(fhp_t * const); +fhp_header_value_parser_init(fhp_ctx_t * const); fhp_err_t -fhp_header_value_parser_push(fhp_t * const, uint8_t * const, size_t); +fhp_header_value_parser_push(fhp_ctx_t * const, uint8_t * const, size_t); fhp_err_t -fhp_header_value_parser_done(fhp_t * const); +fhp_header_value_parser_done(fhp_ctx_t * const); diff --git a/test.c b/test.c index 66ada5a..a2ccb0a 100644 --- a/test.c +++ b/test.c @@ -38,14 +38,14 @@ basic_str = static bool basic_cb( - fhp_t *fhp, + fhp_ctx_t *ctx, fhp_token_t token, uint8_t * const buf, size_t len ) { fhp_err_t err; - UNUSED(fhp); + UNUSED(ctx); UNUSED(buf); // get token name @@ -63,17 +63,17 @@ basic_cb( static void test_basic(void) { fhp_err_t err; - fhp_t fhp; + fhp_ctx_t ctx; // init parser - if ((err = fhp_init(&fhp, NULL, basic_cb, NULL)) != FHP_OK) { - die("test_basic", "fhp_init", err); + if ((err = fhp_ctx_init(&ctx, NULL, basic_cb, NULL)) != FHP_OK) { + die("test_basic", "fhp_ctx_init", err); } // parse data size_t len = strlen(basic_str); - if ((err = fhp_push(&fhp, (uint8_t*) basic_str, len)) != FHP_OK) { - die("test_basic", "fhp_push", err); + if ((err = fhp_ctx_push(&ctx, (uint8_t*) basic_str, len)) != FHP_OK) { + die("test_basic", "fhp_ctx_push", err); } } @@ -95,12 +95,12 @@ percent_str = static bool percent_cb( - fhp_t *fhp, + fhp_ctx_t *ctx, fhp_token_t token, uint8_t * const buf, size_t len ) { - percent_data *data = fhp_get_user_data(fhp); + percent_data *data = fhp_ctx_get_user_data(ctx); switch (token) { case FHP_TOKEN_URL_START: @@ -132,18 +132,18 @@ percent_cb( static void test_percent(void) { fhp_err_t err; - fhp_t fhp; + fhp_ctx_t ctx; percent_data data; // init parser - if ((err = fhp_init(&fhp, NULL, percent_cb, &data)) != FHP_OK) { - die("test_percent", "fhp_init", err); + if ((err = fhp_ctx_init(&ctx, NULL, percent_cb, &data)) != FHP_OK) { + die("test_percent", "fhp_ctx_init", err); } // parse data size_t len = strlen(percent_str); - if ((err = fhp_push(&fhp, (uint8_t*) percent_str, len)) != FHP_OK) { - die("test_percent", "fhp_push", err); + if ((err = fhp_ctx_push(&ctx, (uint8_t*) percent_str, len)) != FHP_OK) { + die("test_percent", "fhp_ctx_push", err); } } -- cgit v1.2.3