From 19653e153b70e72e9a75e74655c8e56025ce4d61 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 27 Aug 2016 14:39:23 -0400 Subject: hash http method and version and add tokens for fast parsing --- include/fhp/fhp.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/fhp/fhp.h b/include/fhp/fhp.h index 563d2ad..2e1dc95 100644 --- a/include/fhp/fhp.h +++ b/include/fhp/fhp.h @@ -5,6 +5,21 @@ #include // for size_t #include // for size_t +// +// hash functions (djb2) +// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/) +// + +uint32_t fhp_hash_init(void); +uint32_t fhp_hash_push(uint32_t, uint8_t * const, size_t); +uint32_t fhp_hash_string(char * const); +uint32_t fhp_lc_hash_push(uint32_t, uint8_t * const, size_t); +uint32_t fhp_lc_hash_string(char * const); + +// +// error functions +// + typedef enum { FHP_OK, FHP_ERR_CB, @@ -24,11 +39,23 @@ typedef enum { fhp_err_t fhp_strerror(fhp_err_t, char * const, size_t); +// +// token functions +// + typedef enum { FHP_TOKEN_METHOD_START, FHP_TOKEN_METHOD_FRAGMENT, FHP_TOKEN_METHOD_END, + FHP_TOKEN_METHOD_GET, + FHP_TOKEN_METHOD_POST, + FHP_TOKEN_METHOD_HEAD, + FHP_TOKEN_METHOD_PUT, + FHP_TOKEN_METHOD_DELETE, + FHP_TOKEN_METHOD_OPTIONS, + FHP_TOKEN_METHOD_OTHER, + FHP_TOKEN_URL_START, FHP_TOKEN_URL_FRAGMENT, FHP_TOKEN_URL_END, @@ -37,6 +64,10 @@ typedef enum { FHP_TOKEN_VERSION_FRAGMENT, FHP_TOKEN_VERSION_END, + FHP_TOKEN_VERSION_HTTP_10, + FHP_TOKEN_VERSION_HTTP_11, + FHP_TOKEN_VERSION_OTHER, + FHP_TOKEN_HEADER_NAME_START, FHP_TOKEN_HEADER_NAME_FRAGMENT, FHP_TOKEN_HEADER_NAME_END, @@ -51,6 +82,23 @@ typedef enum { fhp_err_t fhp_strtoken(fhp_token_t, char * const, size_t); +// +// env functions +// + +#define FHP_ENV_NUM_HASHES 6 + +typedef struct { + uint32_t hashes[FHP_ENV_NUM_HASHES]; +} fhp_env_t; + +void fhp_env_init(fhp_env_t * const env); +fhp_env_t *fhp_get_default_env(void); + +// +// context functions +// + typedef struct fhp_t_ fhp_t; typedef bool (*fhp_cb_t)( @@ -88,15 +136,18 @@ typedef enum { #define FHP_BUF_SIZE 1024 struct fhp_t_ { + // env pointer + fhp_env_t *env; + + // opaque user data + void *user_data; + // current parser state fhp_state_t state; // user callback fhp_cb_t cb; - // opaque user data - void *user_data; - // last error fhp_err_t err; @@ -107,17 +158,27 @@ struct fhp_t_ { uint8_t buf[FHP_BUF_SIZE]; size_t buf_len; + // buffer hashing state + bool is_hashing; + uint32_t buf_hash; + + // cached http method and version + fhp_token_t http_method, http_version; + // state for url hex decoder uint32_t hex; }; fhp_err_t -fhp_init(fhp_t * const, fhp_cb_t, void * const); +fhp_init(fhp_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_env_t * +fhp_get_env(fhp_t * const); + void * -fhp_user_data(fhp_t * const); +fhp_get_user_data(fhp_t * const); #endif /* FHP_H */ -- cgit v1.2.3