From 9315bbfd0f9e51da5438d29681cab6f9a6533d89 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 00:30:02 -0400 Subject: split up fhp.c --- Makefile | 2 +- env.c | 44 ++++++++++++++ error.c | 50 ++++++++++++++++ fhp.c | 203 +-------------------------------------------------------------- hash.c | 50 ++++++++++++++++ token.c | 65 ++++++++++++++++++++ 6 files changed, 211 insertions(+), 203 deletions(-) create mode 100644 env.c create mode 100644 error.c create mode 100644 hash.c create mode 100644 token.c diff --git a/Makefile b/Makefile index e80b70a..32976d7 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC ?= cc CFLAGS=-std=c99 -W -Wall -pedantic -O2 -Iinclude -fPIC -OBJS=fhp.o +OBJS=fhp.o hash.o error.o token.o env.o SONAME=libfhp.so LIB=libfhp.so diff --git a/env.c b/env.c new file mode 100644 index 0000000..721d392 --- /dev/null +++ b/env.c @@ -0,0 +1,44 @@ +#include +#include "fhp/fhp.h" + +static char * const +fhp_strings[] = { + "GET", + "POST", + "HEAD", + "PUT", + "DELETE", + "OPTIONS", + "HTTP/1.0", + "HTTP/1.1", + "content-length", + "transfer-encoding", + "gzip", + "x-gzip", + "deflate", + "x-deflate", + "compress", + "x-compress", + "chunked", + NULL +}; + +void +fhp_env_init(fhp_env_t * const env) { + for (size_t i = 0; fhp_strings[i]; i++) + env->hashes[i] = fhp_lc_hash_string(fhp_strings[i]); +} + +static fhp_env_t fhp_default_env; + +fhp_env_t * +fhp_get_default_env(void) { + static fhp_env_t *r = NULL; + + if (!r) { + r = &fhp_default_env; + fhp_env_init(r); + } + + return r; +} diff --git a/error.c b/error.c new file mode 100644 index 0000000..e88ab4f --- /dev/null +++ b/error.c @@ -0,0 +1,50 @@ +#include +#include "fhp/fhp.h" + +// +// error functions +// + +static const char * +fhp_errors[] = { + "OK", + "callback error", + "bad state", + "invalid character", + "invalid character in HTTP method", + "invalid character in HTTP URL", + "invalid percent-encoded character in HTTP URL", + "invalid character in HTTP version", + "invalid character after carriage return", + "invalid character in HTTP header name", + "invalid error code", + "buffer too small", + "invalid body type", + "too many transfer encodings", + "invalid character in transfer encoding name", + "invalid character in transfer encoding", + "bad transfer encoding state", + "transfer encoding parser already done", +}; + +fhp_err_t +fhp_strerror( + fhp_err_t err, + char * const buf, + size_t len +) { + // check error code + if (err >= FHP_ERR_LAST) + return FHP_ERR_INVALID_ERROR; + + // check buffer size + size_t err_len = strlen(fhp_errors[err]) + 1; + if (len < err_len) + return FHP_ERR_BUFFER_TOO_SMALL; + + // copy string + memcpy(buf, fhp_errors[err], err_len); + + // return success + return FHP_OK; +} diff --git a/fhp.c b/fhp.c index fd1f746..917141d 100644 --- a/fhp.c +++ b/fhp.c @@ -175,190 +175,9 @@ case '\t': // -// hash functions (djb2) -// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/) +// strings // -uint32_t -fhp_hash_init(void) { - return 5381; -} - -uint32_t -fhp_hash_push(uint32_t hash, uint8_t * const buf, size_t len) { - for (size_t i = 0; i < len; i++) - hash = ((hash << 5) + hash) + buf[len]; - - return hash; -} - -uint32_t -fhp_hash_string(char * const str) { - uint32_t r = fhp_hash_init(); - return fhp_hash_push(r, (uint8_t*) str, strlen(str)); -} - -uint32_t -fhp_lc_hash_push( - uint32_t hash, - uint8_t * const buf, - size_t len -) { - for (size_t i = 0; i < len; i++) { - uint8_t c = buf[len]; - - if (c >= 'A' && c <= 'Z') - c = (c - 'A') + 'a'; - - hash = ((hash << 5) + hash) + c; - } - - return hash; -} - -uint32_t -fhp_lc_hash_string(char * const str) { - uint32_t r = fhp_hash_init(); - return fhp_lc_hash_push(r, (uint8_t*) str, strlen(str)); -} - -// -// error functions -// - -static const char * -fhp_errors[] = { - "OK", - "callback error", - "bad state", - "invalid character", - "invalid character in HTTP method", - "invalid character in HTTP URL", - "invalid percent-encoded character in HTTP URL", - "invalid character in HTTP version", - "invalid character after carriage return", - "invalid character in HTTP header name", - "invalid error code", - "buffer too small", - "invalid body type", - "too many transfer encodings", - "invalid character in transfer encoding name", - "invalid character in transfer encoding", - "bad transfer encoding state", - "transfer encoding parser already done", -}; - -fhp_err_t -fhp_strerror( - fhp_err_t err, - char * const buf, - size_t len -) { - // check error code - if (err >= FHP_ERR_LAST) - return FHP_ERR_INVALID_ERROR; - - // check buffer size - size_t err_len = strlen(fhp_errors[err]) + 1; - if (len < err_len) - return FHP_ERR_BUFFER_TOO_SMALL; - - // copy string - memcpy(buf, fhp_errors[err], err_len); - - // return success - return FHP_OK; -} - -// -// token functions -// - -static const char * -fhp_tokens[] = { - "METHOD_START", - "METHOD_FRAGMENT", - "METHOD_END", - - "METHOD_GET", - "METHOD_POST", - "METHOD_HEAD", - "METHOD_PUT", - "METHOD_DELETE", - "METHOD_OPTIONS", - "METHOD_OTHER", - - "URL_START", - "URL_FRAGMENT", - "URL_END", - - "VERSION_START", - "VERSION_FRAGMENT", - "VERSION_END", - - "VERSION_HTTP_10", - "VERSION_HTTP_11", - "VERSION_OTHER", - - "HEADER_NAME_START", - "HEADER_NAME_FRAGMENT", - "HEADER_NAME_END", - - "HEADER_VALUE_START", - "HEADER_VALUE_FRAGMENT", - "HEADER_VALUE_END", - - "LAST" -}; - -fhp_err_t -fhp_strtoken( - fhp_token_t token, - char * const buf, - size_t len -) { - // check token code - if (token >= FHP_TOKEN_LAST) - return FHP_ERR_INVALID_ERROR; - - // check buffer size - size_t str_len = strlen(fhp_tokens[token]) + 1; - if (len < str_len) - return FHP_ERR_BUFFER_TOO_SMALL; - - // copy string - memcpy(buf, fhp_tokens[token], str_len); - - // return success - return FHP_OK; -} - -// -// string functions -// - -static char * const -fhp_strings[] = { - "GET", - "POST", - "HEAD", - "PUT", - "DELETE", - "OPTIONS", - "HTTP/1.0", - "HTTP/1.1", - "content-length", - "transfer-encoding", - "gzip", - "x-gzip", - "deflate", - "x-deflate", - "compress", - "x-compress", - "chunked", - NULL -}; - typedef enum { FHP_STR_GET, FHP_STR_POST, @@ -380,26 +199,6 @@ typedef enum { FHP_STR_LAST } fhp_str_t; -void -fhp_env_init(fhp_env_t * const env) { - for (size_t i = 0; i < FHP_STR_LAST; i++) - env->hashes[i] = fhp_lc_hash_string(fhp_strings[i]); -} - -static fhp_env_t fhp_default_env; - -fhp_env_t * -fhp_get_default_env(void) { - static fhp_env_t *r = NULL; - - if (!r) { - r = &fhp_default_env; - fhp_env_init(r); - } - - return r; -} - // // transfer encoding buffer functions // diff --git a/hash.c b/hash.c new file mode 100644 index 0000000..deae792 --- /dev/null +++ b/hash.c @@ -0,0 +1,50 @@ +#include +#include "fhp/fhp.h" + +// +// hash functions (djb2) +// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/) +// + +uint32_t +fhp_hash_init(void) { + return 5381; +} + +uint32_t +fhp_hash_push(uint32_t hash, uint8_t * const buf, size_t len) { + for (size_t i = 0; i < len; i++) + hash = ((hash << 5) + hash) + buf[len]; + + return hash; +} + +uint32_t +fhp_hash_string(char * const str) { + uint32_t r = fhp_hash_init(); + return fhp_hash_push(r, (uint8_t*) str, strlen(str)); +} + +uint32_t +fhp_lc_hash_push( + uint32_t hash, + uint8_t * const buf, + size_t len +) { + for (size_t i = 0; i < len; i++) { + uint8_t c = buf[len]; + + if (c >= 'A' && c <= 'Z') + c = (c - 'A') + 'a'; + + hash = ((hash << 5) + hash) + c; + } + + return hash; +} + +uint32_t +fhp_lc_hash_string(char * const str) { + uint32_t r = fhp_hash_init(); + return fhp_lc_hash_push(r, (uint8_t*) str, strlen(str)); +} diff --git a/token.c b/token.c new file mode 100644 index 0000000..d32c952 --- /dev/null +++ b/token.c @@ -0,0 +1,65 @@ +#include +#include "fhp/fhp.h" + +// +// token functions +// + +static const char * +fhp_tokens[] = { + "METHOD_START", + "METHOD_FRAGMENT", + "METHOD_END", + + "METHOD_GET", + "METHOD_POST", + "METHOD_HEAD", + "METHOD_PUT", + "METHOD_DELETE", + "METHOD_OPTIONS", + "METHOD_OTHER", + + "URL_START", + "URL_FRAGMENT", + "URL_END", + + "VERSION_START", + "VERSION_FRAGMENT", + "VERSION_END", + + "VERSION_HTTP_10", + "VERSION_HTTP_11", + "VERSION_OTHER", + + "HEADER_NAME_START", + "HEADER_NAME_FRAGMENT", + "HEADER_NAME_END", + + "HEADER_VALUE_START", + "HEADER_VALUE_FRAGMENT", + "HEADER_VALUE_END", + + "LAST" +}; + +fhp_err_t +fhp_strtoken( + fhp_token_t token, + char * const buf, + size_t len +) { + // check token code + if (token >= FHP_TOKEN_LAST) + return FHP_ERR_INVALID_ERROR; + + // check buffer size + size_t str_len = strlen(fhp_tokens[token]) + 1; + if (len < str_len) + return FHP_ERR_BUFFER_TOO_SMALL; + + // copy string + memcpy(buf, fhp_tokens[token], str_len); + + // return success + return FHP_OK; +} -- cgit v1.2.3