diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | env.c | 44 | ||||
-rw-r--r-- | error.c | 50 | ||||
-rw-r--r-- | fhp.c | 203 | ||||
-rw-r--r-- | hash.c | 50 | ||||
-rw-r--r-- | token.c | 65 |
6 files changed, 211 insertions, 203 deletions
@@ -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 @@ -0,0 +1,44 @@ +#include <string.h> +#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; +} @@ -0,0 +1,50 @@ +#include <string.h> +#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; +} @@ -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 // @@ -0,0 +1,50 @@ +#include <string.h> +#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)); +} @@ -0,0 +1,65 @@ +#include <string.h> +#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; +} |