Adjust benchmark directory,enable HTTP test,rename variables,format codes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "http_decoder_inc.h"
|
||||
#include "http_decoder_private.h"
|
||||
|
||||
#define INIT_HEADER_CNT 16
|
||||
#define MAX_URI_CACHE_SIZE 2048
|
||||
@@ -11,12 +11,14 @@
|
||||
#define MAX_HEADER_KEY_CACHE_SIZE 4096
|
||||
#define MAX_HEADER_VALUE_CACHE_SIZE 4096
|
||||
|
||||
struct http_decoder_header {
|
||||
struct http_decoder_header
|
||||
{
|
||||
struct http_decoder_string key;
|
||||
struct http_decoder_string val;
|
||||
};
|
||||
|
||||
struct http_decoder_table {
|
||||
struct http_decoder_table
|
||||
{
|
||||
struct http_decoder_string uri;
|
||||
struct http_decoder_string status;
|
||||
struct http_decoder_string method;
|
||||
@@ -26,15 +28,16 @@ struct http_decoder_table {
|
||||
nmx_pool_t *ref_mempool;
|
||||
int header_complete; // flag for all headers parsed completely
|
||||
size_t header_cnt;
|
||||
size_t header_index; //current parsing header
|
||||
size_t header_iter; //plugins iterate cursor
|
||||
size_t commit_header_index; //pushed to plugins, whether has called http_message_header_next()
|
||||
size_t header_index; // current parsing header
|
||||
size_t header_iter; // plugins iterate cursor
|
||||
size_t commit_header_index; // pushed to plugins, whether has called http_message_header_next()
|
||||
struct http_decoder_header *headers;
|
||||
};
|
||||
|
||||
static void http_decoder_table_init(struct http_decoder_table *table)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -46,12 +49,13 @@ static void http_decoder_table_init(struct http_decoder_table *table)
|
||||
http_decoder_string_init(&table->method, MAX_METHOD_CACHE_SIZE);
|
||||
http_decoder_string_init(&table->version, MAX_METHOD_CACHE_SIZE);
|
||||
|
||||
for (size_t i = 0; i < table->header_cnt; i++) {
|
||||
for (size_t i = 0; i < table->header_cnt; i++)
|
||||
{
|
||||
header = &table->headers[i];
|
||||
http_decoder_string_init(&header->key, MAX_HEADER_KEY_CACHE_SIZE);
|
||||
http_decoder_string_init(&header->val, MAX_HEADER_VALUE_CACHE_SIZE);
|
||||
}
|
||||
|
||||
|
||||
http_decoder_string_init(&table->body, 0);
|
||||
}
|
||||
|
||||
@@ -73,32 +77,42 @@ struct http_decoder_table *http_decoder_table_new(nmx_pool_t *mempool)
|
||||
|
||||
void http_decoder_table_free(struct http_decoder_table *table)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (table->uri.cache.iov_base != NULL) {
|
||||
if (table->uri.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->uri.cache.iov_base);
|
||||
}
|
||||
if (table->status.cache.iov_base != NULL) {
|
||||
if (table->status.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->status.cache.iov_base);
|
||||
}
|
||||
if (table->method.cache.iov_base != NULL) {
|
||||
if (table->method.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->method.cache.iov_base);
|
||||
}
|
||||
if (table->version.cache.iov_base != NULL) {
|
||||
if (table->version.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->version.cache.iov_base);
|
||||
}
|
||||
if (table->body.cache.iov_base != NULL) {
|
||||
if (table->body.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->body.cache.iov_base);
|
||||
}
|
||||
|
||||
if (table->headers != NULL) {
|
||||
for (size_t i = 0; i < table->header_cnt; i++) {
|
||||
if (table->headers[i].key.cache.iov_base != NULL) {
|
||||
if (table->headers != NULL)
|
||||
{
|
||||
for (size_t i = 0; i < table->header_cnt; i++)
|
||||
{
|
||||
if (table->headers[i].key.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->headers[i].key.cache.iov_base);
|
||||
}
|
||||
|
||||
if (table->headers[i].val.cache.iov_base != NULL) {
|
||||
if (table->headers[i].val.cache.iov_base != NULL)
|
||||
{
|
||||
FREE(table->headers[i].val.cache.iov_base);
|
||||
}
|
||||
}
|
||||
@@ -109,17 +123,18 @@ void http_decoder_table_free(struct http_decoder_table *table)
|
||||
MEMPOOL_FREE(table->ref_mempool, table);
|
||||
}
|
||||
|
||||
enum string_state
|
||||
http_decoder_table_state(struct http_decoder_table *table, enum http_item type)
|
||||
enum string_state http_decoder_table_state(struct http_decoder_table *table, enum http_item type)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return STRING_STATE_INIT;
|
||||
}
|
||||
struct http_decoder_header *header = NULL;
|
||||
enum string_state state = STRING_STATE_INIT;
|
||||
assert(table);
|
||||
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case HTTP_ITEM_URI:
|
||||
state = http_decoder_string_state(&table->uri);
|
||||
break;
|
||||
@@ -153,17 +168,18 @@ http_decoder_table_state(struct http_decoder_table *table, enum http_item type)
|
||||
return state;
|
||||
}
|
||||
|
||||
void http_decoder_table_refer(struct http_decoder_table *table, enum http_item type,
|
||||
const char *at, size_t len)
|
||||
void http_decoder_table_refer(struct http_decoder_table *table, enum http_item type, const char *at, size_t len)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct http_decoder_header *header = NULL;
|
||||
assert(table);
|
||||
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case HTTP_ITEM_URI:
|
||||
http_decoder_string_refer(&table->uri, at, len);
|
||||
break;
|
||||
@@ -197,14 +213,16 @@ void http_decoder_table_refer(struct http_decoder_table *table, enum http_item t
|
||||
|
||||
void http_decoder_table_cache(struct http_decoder_table *table, enum http_item type)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct http_decoder_header *header = NULL;
|
||||
assert(table);
|
||||
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case HTTP_ITEM_URI:
|
||||
http_decoder_string_cache(&table->uri);
|
||||
break;
|
||||
@@ -238,7 +256,8 @@ void http_decoder_table_cache(struct http_decoder_table *table, enum http_item t
|
||||
|
||||
void http_decoder_table_commit(struct http_decoder_table *table, enum http_item type)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -246,7 +265,8 @@ void http_decoder_table_commit(struct http_decoder_table *table, enum http_item
|
||||
struct http_decoder_header *header = NULL;
|
||||
assert(table);
|
||||
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case HTTP_ITEM_URI:
|
||||
http_decoder_string_commit(&table->uri);
|
||||
break;
|
||||
@@ -268,20 +288,23 @@ void http_decoder_table_commit(struct http_decoder_table *table, enum http_item
|
||||
header = &table->headers[table->header_index];
|
||||
http_decoder_string_commit(&header->val);
|
||||
// inc index
|
||||
if ((table->header_index + 1) >= table->header_cnt) {
|
||||
if ((table->header_index + 1) >= table->header_cnt)
|
||||
{
|
||||
struct http_decoder_header *old_headers = table->headers;
|
||||
table->headers =
|
||||
MEMPOOL_CALLOC(table->ref_mempool, struct http_decoder_header,
|
||||
table->header_cnt * 2);
|
||||
table->header_cnt *= 2;
|
||||
|
||||
for (i = 0; i <= table->header_index; i++) {
|
||||
for (i = 0; i <= table->header_index; i++)
|
||||
{
|
||||
table->headers[i] = old_headers[i];
|
||||
}
|
||||
|
||||
MEMPOOL_FREE(table->ref_mempool, old_headers);
|
||||
|
||||
for (i = table->header_index + 1; i < table->header_cnt; i++) {
|
||||
for (i = table->header_index + 1; i < table->header_cnt; i++)
|
||||
{
|
||||
header = &table->headers[i];
|
||||
memset(header, 0, sizeof(struct http_decoder_header));
|
||||
http_decoder_string_init(&header->key, MAX_HEADER_KEY_CACHE_SIZE);
|
||||
@@ -301,14 +324,16 @@ void http_decoder_table_commit(struct http_decoder_table *table, enum http_item
|
||||
|
||||
void http_decoder_table_reset(struct http_decoder_table *table, enum http_item type)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct http_decoder_header *header = NULL;
|
||||
assert(table);
|
||||
|
||||
switch (type) {
|
||||
switch (type)
|
||||
{
|
||||
case HTTP_ITEM_URI:
|
||||
http_decoder_string_reset(&table->uri);
|
||||
break;
|
||||
@@ -342,14 +367,15 @@ void http_decoder_table_reinit(struct http_decoder_table *table)
|
||||
{
|
||||
assert(table);
|
||||
struct http_decoder_header *header = NULL;
|
||||
|
||||
|
||||
http_decoder_string_reinit(&table->uri);
|
||||
http_decoder_string_reinit(&table->status);
|
||||
http_decoder_string_reinit(&table->method);
|
||||
http_decoder_string_reinit(&table->version);
|
||||
// for (size_t i = 0; i < table->header_iter; i++) {
|
||||
for (size_t i = 0; i < table->commit_header_index; i++) {
|
||||
//todo, reset header_index, avoid realloc headers as much as possible
|
||||
for (size_t i = 0; i < table->commit_header_index; i++)
|
||||
{
|
||||
// todo, reset header_index, avoid realloc headers as much as possible
|
||||
header = &table->headers[i];
|
||||
http_decoder_string_reinit(&header->key);
|
||||
http_decoder_string_reinit(&header->val);
|
||||
@@ -360,7 +386,8 @@ void http_decoder_table_reinit(struct http_decoder_table *table)
|
||||
|
||||
void http_decoder_table_dump(struct http_decoder_table *table)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -370,9 +397,11 @@ void http_decoder_table_dump(struct http_decoder_table *table)
|
||||
http_decoder_string_dump(&table->version, "version");
|
||||
http_decoder_string_dump(&table->body, "body");
|
||||
|
||||
for (size_t i = 0; i < table->header_cnt; i++) {
|
||||
for (size_t i = 0; i < table->header_cnt; i++)
|
||||
{
|
||||
struct http_decoder_header *header = &table->headers[i];
|
||||
if (NULL == header) {
|
||||
if (NULL == header)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -383,7 +412,8 @@ void http_decoder_table_dump(struct http_decoder_table *table)
|
||||
|
||||
int http_decoder_table_get_uri(const struct http_decoder_table *table, hstring *out)
|
||||
{
|
||||
if (NULL == table || NULL == out) {
|
||||
if (NULL == table || NULL == out)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return http_decoder_string_get(&table->uri, out);
|
||||
@@ -391,7 +421,8 @@ int http_decoder_table_get_uri(const struct http_decoder_table *table, hstring *
|
||||
|
||||
int http_decoder_table_get_method(const struct http_decoder_table *table, hstring *out)
|
||||
{
|
||||
if (NULL == table || NULL == out) {
|
||||
if (NULL == table || NULL == out)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return http_decoder_string_get(&table->method, out);
|
||||
@@ -399,7 +430,8 @@ int http_decoder_table_get_method(const struct http_decoder_table *table, hstrin
|
||||
|
||||
int http_decoder_table_get_status(const struct http_decoder_table *table, hstring *out)
|
||||
{
|
||||
if (NULL == table || NULL == out) {
|
||||
if (NULL == table || NULL == out)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return http_decoder_string_get(&table->status, out);
|
||||
@@ -407,7 +439,8 @@ int http_decoder_table_get_status(const struct http_decoder_table *table, hstrin
|
||||
|
||||
int http_decoder_table_get_version(const struct http_decoder_table *table, hstring *out)
|
||||
{
|
||||
if (NULL == table || NULL == out) {
|
||||
if (NULL == table || NULL == out)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return http_decoder_string_get(&table->version, out);
|
||||
@@ -415,7 +448,8 @@ int http_decoder_table_get_version(const struct http_decoder_table *table, hstri
|
||||
|
||||
int http_decoder_table_get_body(const struct http_decoder_table *table, hstring *out)
|
||||
{
|
||||
if (NULL == table || NULL == out) {
|
||||
if (NULL == table || NULL == out)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return http_decoder_string_get(&table->body, out);
|
||||
@@ -424,19 +458,23 @@ int http_decoder_table_get_body(const struct http_decoder_table *table, hstring
|
||||
int http_decoder_table_get_header(const struct http_decoder_table *table, const hstring *key,
|
||||
struct http_header *hdr_result)
|
||||
{
|
||||
for (size_t i = 0; i < table->header_cnt; i++) {
|
||||
for (size_t i = 0; i < table->header_cnt; i++)
|
||||
{
|
||||
const struct http_decoder_header *tmp_header = &table->headers[i];
|
||||
if (tmp_header->key.commit.iov_len != key->iov_len) {
|
||||
if (tmp_header->key.commit.iov_len != key->iov_len)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT &&
|
||||
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) {
|
||||
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT)
|
||||
{
|
||||
hstring tmp_key;
|
||||
http_decoder_string_get(&tmp_header->key, &tmp_key);
|
||||
|
||||
if (tmp_key.iov_len == key->iov_len &&
|
||||
(0 == strncasecmp((char *)tmp_key.iov_base, (char *)key->iov_base, key->iov_len))) {
|
||||
(0 == strncasecmp((char *)tmp_key.iov_base, (char *)key->iov_base, key->iov_len)))
|
||||
{
|
||||
http_decoder_string_get(&tmp_header->key, &hdr_result->key);
|
||||
http_decoder_string_get(&tmp_header->val, &hdr_result->val);
|
||||
return 0;
|
||||
@@ -446,20 +484,23 @@ int http_decoder_table_get_header(const struct http_decoder_table *table, const
|
||||
return -1;
|
||||
}
|
||||
|
||||
int http_decoder_table_iter_header(struct http_decoder_table *table,
|
||||
struct http_header *hdr)
|
||||
int http_decoder_table_iter_header(struct http_decoder_table *table, struct http_header *hdr)
|
||||
{
|
||||
if (NULL == table || NULL == hdr) {
|
||||
if (NULL == table || NULL == hdr)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (table->header_iter >= table->header_cnt) {
|
||||
if (table->header_iter >= table->header_cnt)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct http_decoder_header *tmp_header = &table->headers[table->header_iter];
|
||||
if (tmp_header != NULL) {
|
||||
if (tmp_header != NULL)
|
||||
{
|
||||
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT &&
|
||||
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) {
|
||||
http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT)
|
||||
{
|
||||
|
||||
http_decoder_string_get(&tmp_header->key, &hdr->key);
|
||||
http_decoder_string_get(&tmp_header->val, &hdr->val);
|
||||
@@ -485,14 +526,15 @@ int http_decoder_table_reset_header_iter(struct http_decoder_table *table)
|
||||
int http_decoder_table_has_parsed_header(struct http_decoder_table *table)
|
||||
{
|
||||
// if (NULL == table || (table->header_iter == table->header_index)) {
|
||||
if (NULL == table || (table->commit_header_index == table->header_index)) {
|
||||
if (NULL == table || (table->commit_header_index == table->header_index))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct http_decoder_header *tmp_header = &table->headers[table->header_iter];
|
||||
|
||||
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT
|
||||
&& http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) {
|
||||
if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT && http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -501,7 +543,8 @@ int http_decoder_table_has_parsed_header(struct http_decoder_table *table)
|
||||
|
||||
int http_decoder_table_header_complete(struct http_decoder_table *table)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return table->header_complete;
|
||||
@@ -509,7 +552,8 @@ int http_decoder_table_header_complete(struct http_decoder_table *table)
|
||||
|
||||
void http_decoder_table_set_header_complete(struct http_decoder_table *table)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
table->header_complete = 1;
|
||||
@@ -517,7 +561,8 @@ void http_decoder_table_set_header_complete(struct http_decoder_table *table)
|
||||
|
||||
void http_decoder_table_reset_header_complete(struct http_decoder_table *table)
|
||||
{
|
||||
if (NULL == table) {
|
||||
if (NULL == table)
|
||||
{
|
||||
return;
|
||||
}
|
||||
table->header_complete = 0;
|
||||
|
||||
Reference in New Issue
Block a user