From 285caa39cfdb7be92992ef9258e28a5babb8aa02 Mon Sep 17 00:00:00 2001 From: liuwentan Date: Tue, 16 Jan 2024 15:19:45 +0800 Subject: [HTTP_DECODER]bugfix for result queue overflow --- src/http_decoder/CMakeLists.txt | 3 +- src/http_decoder/http_content_decompress.c | 45 +- src/http_decoder/http_content_decompress.h | 9 +- src/http_decoder/http_decoder.c | 519 ++++++++------------- src/http_decoder/http_decoder.h | 54 +-- src/http_decoder/http_decoder_half.c | 149 +++--- src/http_decoder/http_decoder_half.h | 55 +-- src/http_decoder/http_decoder_result_queue.c | 155 ++++++ src/http_decoder/http_decoder_result_queue.h | 67 +++ src/http_decoder/http_decoder_string.c | 28 +- src/http_decoder/http_decoder_string.h | 24 +- src/http_decoder/http_decoder_table.c | 104 ++--- src/http_decoder/http_decoder_table.h | 50 +- src/http_decoder/http_decoder_utils.c | 5 +- src/http_decoder/http_decoder_utils.h | 3 +- test/http_decoder/http_decoder_gtest.cpp | 4 +- .../test_result_json/http_multi_parse_error.json | 95 +++- 17 files changed, 736 insertions(+), 633 deletions(-) create mode 100644 src/http_decoder/http_decoder_result_queue.c create mode 100644 src/http_decoder/http_decoder_result_queue.h diff --git a/src/http_decoder/CMakeLists.txt b/src/http_decoder/CMakeLists.txt index 7ba1de5..a17fcd8 100644 --- a/src/http_decoder/CMakeLists.txt +++ b/src/http_decoder/CMakeLists.txt @@ -3,7 +3,8 @@ add_definitions(-fPIC) include_directories(/opt/MESA/include/) set(HTTP_SRC http_decoder.c http_decoder_utils.c http_decoder_half.c - http_decoder_table.c http_decoder_string.c http_content_decompress.c) + http_decoder_table.c http_decoder_string.c http_content_decompress.c + http_decoder_result_queue.c) add_library(http_decoder SHARED ${HTTP_SRC}) #set_target_properties(http_decoder_shared PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") diff --git a/src/http_decoder/http_content_decompress.c b/src/http_decoder/http_content_decompress.c index 4afc2cb..f57d4cf 100644 --- a/src/http_decoder/http_content_decompress.c +++ b/src/http_decoder/http_content_decompress.c @@ -120,8 +120,7 @@ error: return NULL; } -void -http_content_decompress_destroy(struct http_content_decompress *decompress) +void http_content_decompress_destroy(struct http_content_decompress *decompress) { if (NULL == decompress) { return; @@ -143,7 +142,8 @@ http_content_decompress_destroy(struct http_content_decompress *decompress) static int http_content_decompress_write_zlib(struct http_content_decompress *decompress, - const char *indata, size_t indata_len, char **outdata, size_t *outdata_len) + const char *indata, size_t indata_len, + char **outdata, size_t *outdata_len) { z_stream *z_stream_ptr = decompress->z_stream_ptr; z_stream_ptr->avail_in = (unsigned int)indata_len; @@ -167,15 +167,15 @@ http_content_decompress_write_zlib(struct http_content_decompress *decompress, size_t have = decompress->buffer_size - z_stream_ptr->avail_out; if (have > 0) { if (NULL == *outdata) { - http_decoder_log(DEBUG, "%s alloc outbuffer %zu bytes", - http_content_encoding_int2str(decompress->encoding), - have); + // http_decoder_log(DEBUG, "%s alloc outbuffer %zu bytes", + // http_content_encoding_int2str(decompress->encoding), + // have); *outdata = safe_dup(decompress->buffer, have); *outdata_len = have; } else { - http_decoder_log(DEBUG, "%s realloc outbuffer %zu bytes", - http_content_encoding_int2str(decompress->encoding), - *outdata_len + have + 1); + // http_decoder_log(DEBUG, "%s realloc outbuffer %zu bytes", + // http_content_encoding_int2str(decompress->encoding), + // *outdata_len + have + 1); *outdata = REALLOC(char, *outdata, *outdata_len + have + 1); memcpy(*outdata + *outdata_len, decompress->buffer, have); *outdata_len = *outdata_len + have; @@ -188,8 +188,9 @@ http_content_decompress_write_zlib(struct http_content_decompress *decompress, } static int -http_content_decompress_write_br(struct http_content_decompress *decompress, - const char *indata, size_t indata_len, char **outdata, size_t *outdata_len) +http_content_decompress_write_br(struct http_content_decompress *decompress, + const char *indata, size_t indata_len, + char **outdata, size_t *outdata_len) { size_t available_out; size_t available_in = indata_len; @@ -209,15 +210,15 @@ http_content_decompress_write_br(struct http_content_decompress *decompress, size_t have = decompress->buffer_size - available_out; if (have > 0) { if (NULL == *outdata) { - http_decoder_log(DEBUG, "%s alloc outbuffer %zu bytes", - http_content_encoding_int2str(decompress->encoding), - have); + // http_decoder_log(DEBUG, "%s alloc outbuffer %zu bytes", + // http_content_encoding_int2str(decompress->encoding), + // have); *outdata = safe_dup(decompress->buffer, have); *outdata_len = have; } else { - http_decoder_log(DEBUG, "%s realloc outbuffer %zu bytes", - http_content_encoding_int2str(decompress->encoding), - *outdata_len + have + 1); + // http_decoder_log(DEBUG, "%s realloc outbuffer %zu bytes", + // http_content_encoding_int2str(decompress->encoding), + // *outdata_len + have + 1); *outdata = REALLOC(char, *outdata, *outdata_len + have + 1); memcpy(*outdata + *outdata_len, decompress->buffer, have); *outdata_len = *outdata_len + have; @@ -243,9 +244,9 @@ http_content_decompress_write_br(struct http_content_decompress *decompress, } } -int -http_content_decompress_write(struct http_content_decompress *decompress, - const char *indata, size_t indata_len, char **outdata, size_t *outdata_len) +int http_content_decompress_write(struct http_content_decompress *decompress, + const char *indata, size_t indata_len, + char **outdata, size_t *outdata_len) { assert(decompress); assert(indata); @@ -256,8 +257,8 @@ http_content_decompress_write(struct http_content_decompress *decompress, *outdata = NULL; *outdata_len = 0; - if (decompress->encoding == HTTP_CONTENT_ENCODING_GZIP - || decompress->encoding == HTTP_CONTENT_ENCODING_DEFLATE) { + if (decompress->encoding == HTTP_CONTENT_ENCODING_GZIP || + decompress->encoding == HTTP_CONTENT_ENCODING_DEFLATE) { return http_content_decompress_write_zlib(decompress, indata, indata_len, outdata, outdata_len); } diff --git a/src/http_decoder/http_content_decompress.h b/src/http_decoder/http_content_decompress.h index a754e59..22bd5fa 100644 --- a/src/http_decoder/http_content_decompress.h +++ b/src/http_decoder/http_content_decompress.h @@ -40,14 +40,13 @@ http_content_encoding_int2str(enum http_content_encoding content_encoding); struct http_content_decompress * http_content_decompress_create(enum http_content_encoding encoding); -void -http_content_decompress_destroy(struct http_content_decompress *decompress); +void http_content_decompress_destroy(struct http_content_decompress *decompress); // return 0 : success // return -1 : error -int -http_content_decompress_write(struct http_content_decompress *decompress, - const char *indata, size_t indata_len, char **outdata, size_t *outdata_len); +int http_content_decompress_write(struct http_content_decompress *decompress, + const char *indata, size_t indata_len, + char **outdata, size_t *outdata_len); #ifdef __cplusplus } diff --git a/src/http_decoder/http_decoder.c b/src/http_decoder/http_decoder.c index e2c2d2f..4be1df6 100644 --- a/src/http_decoder/http_decoder.c +++ b/src/http_decoder/http_decoder.c @@ -20,6 +20,7 @@ #include "http_decoder.h" #include "http_decoder_half.h" #include "http_decoder_table.h" +#include "http_decoder_result_queue.h" #include "llhttp.h" #include "fieldstat/fieldstat_easy.h" @@ -30,20 +31,6 @@ const char *http_decoder_topic = "HTTP_DECODER_MESSAGE"; const char *fs_file_name = "http_decoder.fs"; -struct http_decoder_result { - struct http_decoder_half_data *req_data; - struct http_decoder_half_data *res_data; -}; - -struct http_decoder_result_queue { - struct session *ref_session; - size_t req_index; - size_t res_index; - size_t del_index; - size_t queue_size; - struct http_decoder_result **array; -}; - /** * NOTE: http_message don't have the ownership of data */ @@ -57,10 +44,15 @@ struct http_decoder { struct http_decoder_half *s2c_half; }; +struct http_decoder_exdata { + struct http_decoder_result_queue *queue; + struct http_decoder *decoder; +}; + struct http_event_context { int topic_id; struct session *ref_session; - struct http_decoder_result_queue *ref_queue; + struct http_decoder_exdata *ref_exdata; }; struct http_decoder_context { @@ -72,132 +64,43 @@ struct http_decoder_context { int fs_incoming_trans_id; struct fieldstat_easy *fse; struct stellar *st; - struct http_decoder *decoder; struct http_event_context *http_ev_ctx; }; -static struct http_decoder_result * -http_decoder_result_new() -{ - struct http_decoder_result *result = - CALLOC(struct http_decoder_result, 1); - assert(result); - - result->req_data = NULL; - result->res_data = NULL; - - return result; -} - -static void -http_decoder_result_free(struct http_decoder_result *result) -{ - if (NULL == result) { - return; - } - - if (result->req_data != NULL) { - http_decoder_half_data_free(result->req_data); - result->req_data = NULL; - } - - if (result->res_data != NULL) { - http_decoder_half_data_free(result->res_data); - result->res_data = NULL; - } - - FREE(result); -} - -// Create a new http result and add it to the queue -static void -http_decoder_result_queue_push(struct http_decoder_result_queue *queue, - size_t index) -{ - assert(queue); - assert(index < queue->queue_size); - - if (queue->array[index] == NULL) { - queue->array[index] = http_decoder_result_new(); - assert(queue->array[index]); - } -} - -// Remove the http result from the queue but not destroy it -static void -http_decoder_result_queue_pop(struct http_decoder_result_queue *queue, - size_t index) -{ - assert(queue); - assert(index < queue->queue_size); - - if (queue->array[index] != NULL) { - if (queue->array[index]->req_data != NULL) { - http_decoder_half_data_free(queue->array[index]->req_data); - queue->array[index]->req_data = NULL; - } - - if (queue->array[index]->res_data != NULL) { - http_decoder_half_data_free(queue->array[index]->res_data); - queue->array[index]->res_data = NULL; - } - } -} - -static void -http_decoder_result_queue_inc_req_index(struct http_decoder_result_queue *queue) -{ - assert(queue); - - queue->req_index++; - queue->req_index = queue->req_index % queue->queue_size; -} - -static void -http_decoder_result_queue_inc_res_index(struct http_decoder_result_queue *queue) -{ - assert(queue); - - queue->res_index++; - queue->res_index = queue->res_index % queue->queue_size; -} - -static void -http_decoder_result_queue_inc_del_index(struct http_decoder_result_queue *queue) -{ - assert(queue); - - queue->del_index++; - queue->del_index = queue->del_index % queue->queue_size; -} - -static void -http_decoder_result_queue_gc(struct http_decoder_result_queue *queue, - size_t index) -{ - assert(queue); - assert(index < queue->queue_size); - - if (index == queue->del_index) { - http_decoder_result_queue_pop(queue, index); - http_decoder_result_queue_inc_del_index(queue); - } -} - -static void -http_event_handler(enum http_event event, struct http_decoder_half_data **data, - void *http_ev_ctx) +static void http_event_handler(enum http_event event, + struct http_decoder_half_data **data, + void *http_ev_ctx) { struct http_event_context *ctx = (struct http_event_context *)http_ev_ctx; assert(ctx); - struct http_decoder_result_queue *queue = ctx->ref_queue; + struct http_decoder_result_queue *queue = ctx->ref_exdata->queue; struct http_message *msg = NULL; + struct http_decoder_half_data *half_data = NULL; + int ret = 0; switch (event) { case HTTP_EVENT_REQ_INIT: - queue->array[queue->req_index]->req_data = http_decoder_half_data_new(); - *data = queue->array[queue->req_index]->req_data; + half_data = http_decoder_result_queue_peek_req(queue); + if (half_data != NULL) { + http_decoder_result_queue_inc_req_index(queue); + } + + half_data = http_decoder_result_queue_peek_req(queue); + if (half_data != NULL) { + half_data = http_decoder_result_queue_pop_req(queue); + http_decoder_half_data_free(half_data); + half_data = NULL; + } + + half_data = http_decoder_half_data_new(); + ret = http_decoder_result_queue_push_req(queue, half_data); + if (ret < 0) { + fprintf(stderr, "http_decoder_result_queue_push failed."); + http_decoder_half_data_free(half_data); + half_data = NULL; + } + *data = half_data; break; case HTTP_EVENT_REQ_LINE: msg = CALLOC(struct http_message, 1); @@ -223,11 +126,33 @@ http_event_handler(enum http_event event, struct http_decoder_half_data **data, break; case HTTP_EVENT_REQ_END: http_decoder_result_queue_inc_req_index(queue); - http_decoder_result_queue_gc(queue, queue->req_index); + half_data = http_decoder_result_queue_pop_req(queue); + if (half_data != NULL) { + http_decoder_half_data_free(half_data); + half_data = NULL; + } break; case HTTP_EVENT_RES_INIT: - queue->array[queue->res_index]->res_data = http_decoder_half_data_new(); - *data = queue->array[queue->res_index]->res_data; + half_data = http_decoder_result_queue_peek_res(queue); + if (half_data != NULL) { + http_decoder_result_queue_inc_res_index(queue); + } + + half_data = http_decoder_result_queue_peek_res(queue); + if (half_data != NULL) { + half_data = http_decoder_result_queue_pop_res(queue); + http_decoder_half_data_free(half_data); + half_data = NULL; + } + + half_data = http_decoder_half_data_new(); + ret = http_decoder_result_queue_push_res(queue, half_data); + if (ret < 0) { + fprintf(stderr, "http_decoder_result_queue_push failed."); + http_decoder_half_data_free(half_data); + half_data = NULL; + } + *data = half_data; break; case HTTP_EVENT_RES_LINE: msg = CALLOC(struct http_message, 1); @@ -255,7 +180,11 @@ http_event_handler(enum http_event event, struct http_decoder_half_data **data, break; case HTTP_EVENT_RES_END: http_decoder_result_queue_inc_res_index(queue); - http_decoder_result_queue_gc(queue, queue->res_index); + half_data = http_decoder_result_queue_pop_res(queue); + if (half_data != NULL) { + http_decoder_half_data_free(half_data); + half_data = NULL; + } break; default: assert(0); @@ -275,8 +204,7 @@ http_decoder_new(http_event_cb *ev_cb, int is_cache_body) return decoder; } -static void -http_decoder_free(struct http_decoder *decoder) +static void http_decoder_free(struct http_decoder *decoder) { if (NULL == decoder) { return; @@ -295,51 +223,36 @@ http_decoder_free(struct http_decoder *decoder) FREE(decoder); } -static struct http_decoder_result_queue * -http_decoder_result_queue_new(size_t queue_size) +static struct http_decoder_exdata *http_decoder_exdata_new(size_t queue_size) { - struct http_decoder_result_queue *queue = - CALLOC(struct http_decoder_result_queue, 1); - assert(queue); + struct http_decoder_exdata *ex_data = CALLOC(struct http_decoder_exdata, 1); - queue->del_index = 0; - queue->req_index = 0; - queue->res_index = 0; - queue->queue_size = queue_size; + ex_data->decoder = http_decoder_new(http_event_handler, 0); + ex_data->queue = http_decoder_result_queue_new(HD_RESULT_QUEUE_SIZE); - queue->array = CALLOC(struct http_decoder_result *, queue->queue_size); - assert(queue->array); - - for (size_t i = 0; i < queue->queue_size; i++) { - queue->array[i] = CALLOC(struct http_decoder_result, 1); - } - - return queue; + return ex_data; } -static void -http_decoder_result_queue_free(struct http_decoder_result_queue *queue) +static void http_decoder_exdata_free(struct http_decoder_exdata *ex_data) { - if (NULL == queue) { - return; + if (NULL == ex_data) { + return; } - if (queue->array != NULL) { - for (size_t i = 0; i < queue->queue_size; i++) { - if (queue->array[i] != NULL) { - http_decoder_result_free(queue->array[i]); - queue->array[i] = NULL; - } - } - - FREE(queue->array); + if (ex_data->decoder != NULL) { + http_decoder_free(ex_data->decoder); + ex_data->decoder = NULL; + } + + if (ex_data->queue != NULL) { + http_decoder_result_queue_free(ex_data->queue); + ex_data->queue = NULL; } - FREE(queue); + FREE(ex_data); } -static int -http_protocol_identify(const char *data, size_t data_len) +static int http_protocol_identify(const char *data, size_t data_len) { llhttp_t parser; llhttp_settings_t settings; @@ -357,7 +270,7 @@ http_protocol_identify(const char *data, size_t data_len) } int http_decoder_entry(struct session *sess, int events, - const struct packet *pkt, void *cb_arg) + const struct packet *pkt, void *cb_arg) { struct http_decoder_context *ctx = (struct http_decoder_context *)cb_arg; size_t payload_len = 0; @@ -368,50 +281,47 @@ int http_decoder_entry(struct session *sess, int events, return 0; } - struct http_decoder_result_queue *queue = - session_get_ex_data(sess, ctx->ex_data_idx);; + struct http_decoder_exdata *ex_data = session_get_ex_data(sess, ctx->ex_data_idx); if (events & SESS_EV_CLOSING) { - if (queue != NULL) { - http_decoder_result_queue_free(queue); - session_set_ex_data(sess, ctx->ex_data_idx, NULL); + if (ex_data != NULL) { + http_decoder_exdata_free(ex_data); + session_set_ex_data(sess, ctx->ex_data_idx, NULL); } return 0; } const char *payload = session_get0_current_payload(sess, &payload_len); - // printf("session:%s\n", session_get0_readable_addr(sess)); - //printf("%s\n", payload); + // const char *addr_readable = session_get0_readable_addr(sess); + // enum session_addr_type addr_type; + // struct session_addr *addr = session_get0_addr(sess, &addr_type); + if (events & SESS_EV_OPENING) { - if (queue != NULL) { - fprintf(stderr, - "http_decoder_result_queue should be null for new session\n"); - return -1; - } + assert(ex_data == NULL); //If not http, ignore this session if (payload_len > 0) { - size_t http_identify_len = - payload_len > HTTP_IDENTIFY_LEN ? HTTP_IDENTIFY_LEN : payload_len; + size_t http_identify_len = payload_len > HTTP_IDENTIFY_LEN + ? HTTP_IDENTIFY_LEN + : payload_len; + ret = http_protocol_identify(payload, http_identify_len); if (ret < 0) { // ignore this session's event struct session_event *s_event = session_get_intrinsic_event(sess, ctx->plugin_id); - session_event_assign(s_event, ctx->st, sess, 0, - http_decoder_entry, ctx); + session_event_assign(s_event, ctx->st, sess, 0, http_decoder_entry, ctx); return 0; } } - queue = http_decoder_result_queue_new(HD_RESULT_QUEUE_SIZE); - queue->ref_session = sess; - session_set_ex_data(sess, ctx->ex_data_idx, queue); + ex_data = http_decoder_exdata_new(HD_RESULT_QUEUE_SIZE); + session_set_ex_data(sess, ctx->ex_data_idx, ex_data); } - if (0 == payload_len || NULL == queue) { + if (0 == payload_len || NULL == ex_data) { return 0; } @@ -420,45 +330,33 @@ int http_decoder_entry(struct session *sess, int events, return -1; } - if (NULL == ctx->decoder) { - ctx->decoder = http_decoder_new(http_event_handler, 0); - } - + // printf("session:%u dir:%d readable:%s\n", addr->ipv4.saddr, dir, addr_readable); struct http_decoder_half *cur_half = NULL; if (dir == PACKET_DIRECTION_C2S) { - cur_half = ctx->decoder->c2s_half; + cur_half = ex_data->decoder->c2s_half; } else { - cur_half = ctx->decoder->s2c_half; + cur_half = ex_data->decoder->s2c_half; } ctx->http_ev_ctx->topic_id = ctx->topic_id; - ctx->http_ev_ctx->ref_queue = queue; + ctx->http_ev_ctx->ref_exdata = ex_data; ctx->http_ev_ctx->ref_session = sess; http_decoder_half_parse(cur_half, ctx->http_ev_ctx, payload, payload_len); - // long long trans_cnt = http_decoder_half_trans_count(cur_half); + long long trans_cnt = http_decoder_half_trans_count(cur_half); - // fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_bytes_id, - // NULL, 0, payload_len); - // fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_pkts_id, - // NULL, 0, 1); - // fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_trans_id, - // NULL, 0, trans_cnt); + fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_bytes_id, + NULL, 0, payload_len); + fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_pkts_id, + NULL, 0, 1); + fieldstat_easy_counter_incrby(ctx->fse, 0, ctx->fs_incoming_trans_id, + NULL, 0, trans_cnt); return 0; } -static void -http_decoder_ex_data_free(struct session *s, int idx, void *ex_ptr, void *arg) -{ - if (ex_ptr != NULL) { - FREE(ex_ptr); - } -} - -static void -http_message_free(void *msg, void *cb_arg) +static void http_message_free(void *msg, void *cb_arg) { if (NULL == msg) { return; @@ -470,8 +368,7 @@ http_message_free(void *msg, void *cb_arg) FREE(message); } -static void -_http_decoder_context_free(struct http_decoder_context *ctx) +static void _http_decoder_context_free(struct http_decoder_context *ctx) { if (NULL == ctx) { return; @@ -491,15 +388,21 @@ _http_decoder_context_free(struct http_decoder_context *ctx) ctx->topic_id = -1; } - if (ctx->decoder != NULL) { - http_decoder_free(ctx->decoder); - ctx->decoder = NULL; + FREE(ctx); +} + +static void http_decoder_ex_data_free(struct session *s, int idx, + void *ex_data, void *arg) +{ + if (NULL == ex_data) { + return; } - FREE(ctx); + struct http_decoder_exdata *exdata = (struct http_decoder_exdata *)ex_data; + http_decoder_exdata_free(exdata); } -#define FS_OUTPUT_INTERVAL_S 2 +#define FS_OUTPUT_INTERVAL_S 1 void *http_decoder_init(struct stellar *st) { struct http_decoder_context *ctx = CALLOC(struct http_decoder_context, 1); @@ -523,49 +426,49 @@ void *http_decoder_init(struct stellar *st) } ctx->topic_id = topic_id; - // ctx->fse = fieldstat_easy_new(1, "http_decoder_statistics", NULL, 0); - // if (NULL == ctx->fse) { - // fprintf(stderr, "fieldstat_easy_new failed."); - // goto failed; - // } - - // ctx->fs_incoming_bytes_id = - // fieldstat_easy_register_counter(ctx->fse, "incoming_bytes"); - // if (ctx->fs_incoming_bytes_id < 0) { - // fprintf(stderr, "fieldstat_easy_register_counter incoming_bytes failed."); - // goto failed; - // } - - // ctx->fs_incoming_trans_id = - // fieldstat_easy_register_counter(ctx->fse, "incoming_trans"); - // if (ctx->fs_incoming_trans_id < 0) { - // fprintf(stderr, "fieldstat_easy_register_counter incoming_trans failed."); - // goto failed; - // } - - // ctx->fs_incoming_pkts_id = - // fieldstat_easy_register_counter(ctx->fse, "incoming_pkts"); - // if (ctx->fs_incoming_pkts_id < 0) { - // fprintf(stderr, "fieldstat_easy_register_counter incoming_pkts failed."); - // goto failed; - // } - - // int ret = fieldstat_easy_enable_auto_output(ctx->fse, fs_file_name, - // FS_OUTPUT_INTERVAL_S); - // if (ret < 0) { - // fprintf(stderr, "fieldstat_easy_enable_auto_output failed."); - // goto failed; - // } - // sleep(3); + ctx->fse = fieldstat_easy_new(1, "http_decoder_statistics", NULL, 0); + if (NULL == ctx->fse) { + fprintf(stderr, "fieldstat_easy_new failed."); + goto failed; + } + + ctx->fs_incoming_bytes_id = + fieldstat_easy_register_counter(ctx->fse, "incoming_bytes"); + if (ctx->fs_incoming_bytes_id < 0) { + fprintf(stderr, "fieldstat_easy_register_counter incoming_bytes failed."); + goto failed; + } + + ctx->fs_incoming_trans_id = + fieldstat_easy_register_counter(ctx->fse, "incoming_trans"); + if (ctx->fs_incoming_trans_id < 0) { + fprintf(stderr, "fieldstat_easy_register_counter incoming_trans failed."); + goto failed; + } + + ctx->fs_incoming_pkts_id = + fieldstat_easy_register_counter(ctx->fse, "incoming_pkts"); + if (ctx->fs_incoming_pkts_id < 0) { + fprintf(stderr, "fieldstat_easy_register_counter incoming_pkts failed."); + goto failed; + } + + int ret = fieldstat_easy_enable_auto_output(ctx->fse, fs_file_name, + FS_OUTPUT_INTERVAL_S); + if (ret < 0) { + fprintf(stderr, "fieldstat_easy_enable_auto_output failed."); + goto failed; + } + sleep(1); printf("http_decoder_init: ex_data_idx:%d, plugin_id:%d, topic_id:%d\n", ctx->ex_data_idx, ctx->plugin_id, ctx->topic_id); return ctx; -// failed: -// _http_decoder_context_free(ctx); -// return NULL; +failed: + _http_decoder_context_free(ctx); + return NULL; } void http_decoder_exit(void *decoder_ctx) @@ -580,8 +483,7 @@ void http_decoder_exit(void *decoder_ctx) _http_decoder_context_free(ctx); } -enum http_message_type -http_message_type(struct http_message *msg) +enum http_message_type http_message_type(struct http_message *msg) { if (NULL == msg) { return HTTP_MESSAGE_MAX; @@ -590,121 +492,110 @@ http_message_type(struct http_message *msg) return msg->type; } -int -http_message_get_request_line(struct http_message *msg, - struct http_request_line *line) +int http_message_get_request_line(struct http_message *msg, + struct http_request_line *line) { - if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_LINE || NULL == line) { + if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_LINE || + NULL == line) { return -1; } return http_decoder_half_data_get_request_line(msg->data, line); } -int -http_message_get_response_line(struct http_message *msg, - struct http_response_line *line) +int http_message_get_response_line(struct http_message *msg, + struct http_response_line *line) { - if (NULL == msg || msg->type != HTTP_MESSAGE_RES_LINE || NULL == line) { + if (NULL == msg || msg->type != HTTP_MESSAGE_RES_LINE || + NULL == line) { return -1; } return http_decoder_half_data_get_response_line(msg->data, line); } -int -http_message_get_request_header(struct http_message *msg, struct hstring *key, - struct http_header *header_array, size_t array_size) +int http_message_get_request_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_array, size_t array_size) { - if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER - || NULL == key || NULL == header_array || 0 == array_size) { + if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER || + NULL == key || NULL == hdr_array || 0 == array_size) { return -1; } - return http_decoder_half_data_get_header(msg->data, key, header_array, - array_size); + return http_decoder_half_data_get_header(msg->data, key, hdr_array, array_size); } -int -http_message_get_response_header(struct http_message *msg, struct hstring *key, - struct http_header *header_array, size_t array_size) +int http_message_get_response_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_array, size_t array_size) { - if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER - || NULL == key || NULL == header_array - || 0 == array_size) { + if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER || NULL == key || + NULL == hdr_array || 0 == array_size) { return -1; } - return http_decoder_half_data_get_header(msg->data, key, header_array, - array_size); + return http_decoder_half_data_get_header(msg->data, key, hdr_array, array_size); } -int -http_message_request_header_next(struct http_message *msg, - struct http_header *header) +int http_message_request_header_next(struct http_message *msg, + struct http_header *hdr) { if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER - || NULL == header) { + || NULL == hdr) { return -1; } - return http_decoder_half_data_iter_header(msg->data, header); + return http_decoder_half_data_iter_header(msg->data, hdr); } -int -http_message_response_header_next(struct http_message *msg, - struct http_header *header) +int http_message_response_header_next(struct http_message *msg, + struct http_header *hdr) { - if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER - || NULL == header) { + if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER || + NULL == hdr) { return -1; } - return http_decoder_half_data_iter_header(msg->data, header); + return http_decoder_half_data_iter_header(msg->data, hdr); } -int -http_message_get_request_raw_body(struct http_message *msg, - struct hstring *body) +int http_message_get_request_raw_body(struct http_message *msg, + struct hstring *body) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY) - || NULL == body) { + if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY) || + NULL == body) { return -1; } return http_decoder_half_data_get_raw_body(msg->data, body); } -int -http_message_get_response_raw_body(struct http_message *msg, - struct hstring *body) +int http_message_get_response_raw_body(struct http_message *msg, + struct hstring *body) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY) - || NULL == body) { + if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY) || + NULL == body) { return -1; } return http_decoder_half_data_get_raw_body(msg->data, body); } -int -http_message_get_request_decompress_body(struct http_message *msg, - struct hstring *body) +int http_message_get_request_decompress_body(struct http_message *msg, + struct hstring *body) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY) - || NULL == body) { + if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY) || + NULL == body) { return -1; } return http_decoder_half_data_get_decompress_body(msg->data, body); } -int -http_message_get_response_decompress_body(struct http_message *msg, - struct hstring *body) +int http_message_get_response_decompress_body(struct http_message *msg, + struct hstring *body) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY) - || NULL == body) { + if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY) || + NULL == body) { return -1; } diff --git a/src/http_decoder/http_decoder.h b/src/http_decoder/http_decoder.h index b06876f..d99830b 100644 --- a/src/http_decoder/http_decoder.h +++ b/src/http_decoder/http_decoder.h @@ -61,53 +61,41 @@ struct http_response_line { struct http_message; -enum http_message_type -http_message_type(struct http_message *msg); +enum http_message_type http_message_type(struct http_message *msg); -int -http_message_get_request_line(struct http_message *msg, - struct http_request_line *line); +int http_message_get_request_line(struct http_message *msg, + struct http_request_line *line); -int -http_message_get_response_line(struct http_message *msg, - struct http_response_line *line); +int http_message_get_response_line(struct http_message *msg, + struct http_response_line *line); /* same key may has multiple kv */ -int -http_message_get_request_header(struct http_message *msg, struct hstring *key, - struct http_header *header_array, size_t array_size); +int http_message_get_request_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_array, size_t array_size); -int -http_message_get_response_header(struct http_message *msg, struct hstring *key, - struct http_header *header_array, size_t array_size); +int http_message_get_response_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_array, size_t array_size); -int -http_message_request_header_next(struct http_message *msg, - struct http_header *header); +int http_message_request_header_next(struct http_message *msg, + struct http_header *header); -int -http_message_response_header_next(struct http_message *msg, - struct http_header *header); +int http_message_response_header_next(struct http_message *msg, + struct http_header *header); -int -http_message_get_request_raw_body(struct http_message *msg, - struct hstring *body); +int http_message_get_request_raw_body(struct http_message *msg, + struct hstring *body); -int -http_message_get_response_raw_body(struct http_message *msg, - struct hstring *body); +int http_message_get_response_raw_body(struct http_message *msg, + struct hstring *body); /** * @brief If the body hasn't been compressed, return raw body */ -int -http_message_get_request_decompress_body(struct http_message *msg, - struct hstring *body); - -int -http_message_get_response_decompress_body(struct http_message *msg, - struct hstring *body); +int http_message_get_request_decompress_body(struct http_message *msg, + struct hstring *body); +int http_message_get_response_decompress_body(struct http_message *msg, + struct hstring *body); #ifdef __cplusplus } diff --git a/src/http_decoder/http_decoder_half.c b/src/http_decoder/http_decoder_half.c index af77f0a..2c1a133 100644 --- a/src/http_decoder/http_decoder_half.c +++ b/src/http_decoder/http_decoder_half.c @@ -2,13 +2,12 @@ ********************************************************************************************** * File: http_decoder_half.c * Description: -* Authors: LuWenPeng -* Date: 2022-10-31 +* Authors: Liuwentan +* Date: 2024-01-10 * Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved. *********************************************************************************************** */ - #include #include @@ -19,7 +18,6 @@ #include "http_decoder_table.h" #include "http_content_decompress.h" - struct http_decoder_half_data { struct http_decoder_table *table; @@ -46,7 +44,7 @@ struct http_decoder_half { long long trans_counter; }; -//#define HTTP_DECODER_DEBUG +// #define HTTP_DECODER_DEBUG #ifdef HTTP_DECODER_DEBUG static void printf_debug_info(const char *desc, const char *at, size_t length) { @@ -95,8 +93,7 @@ http_decoder_half_data_decompress(struct http_decoder_half_data *data) } /* Possible return values 0, -1, `HPE_PAUSED` */ -static int -on_message_begin(llhttp_t *http) +static int on_message_begin(llhttp_t *http) { printf_debug_info("on_message_begin", NULL, 0); @@ -120,8 +117,7 @@ on_message_begin(llhttp_t *http) return 0; } -static int -on_message_complete(llhttp_t *http) +static int on_message_complete(llhttp_t *http) { printf_debug_info("on_message_complete", NULL, 0); @@ -205,16 +201,14 @@ on_message_complete(llhttp_t *http) return 0; } -static int -on_reset(llhttp_t *http) +static int on_reset(llhttp_t *http) { printf_debug_info("on_reset", NULL, 0); return 0; } -static int -on_method(llhttp_t *http, const char *at, size_t length) +static int on_method(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_method", at, length); @@ -228,8 +222,7 @@ on_method(llhttp_t *http, const char *at, size_t length) } /* Information-only callbacks, return value is ignored */ -static int -on_method_complete(llhttp_t *http) +static int on_method_complete(llhttp_t *http) { printf_debug_info("on_method_complete", NULL, 0); @@ -237,14 +230,17 @@ on_method_complete(llhttp_t *http) container_of(http, struct http_decoder_half, parser); assert(half); + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_METHOD) == + STRING_STATE_REFER) { + http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_METHOD); + } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_METHOD); return 0; } /* Possible return values 0, -1, HPE_USER */ -static int -on_uri(llhttp_t *http, const char *at, size_t length) +static int on_uri(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_uri", at, length); @@ -258,8 +254,7 @@ on_uri(llhttp_t *http, const char *at, size_t length) } /* Information-only callbacks, return value is ignored */ -static int -on_uri_complete(llhttp_t *http) +static int on_uri_complete(llhttp_t *http) { printf_debug_info("on_uri_complete", NULL, 0); @@ -267,14 +262,17 @@ on_uri_complete(llhttp_t *http) container_of(http, struct http_decoder_half, parser); assert(half); + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_URI) == + STRING_STATE_REFER) { + http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_URI); + } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_URI); return 0; } /* Possible return values 0, -1, HPE_USER */ -static int -on_version(llhttp_t *http, const char *at, size_t length) +static int on_version(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_version", at, length); @@ -288,8 +286,7 @@ on_version(llhttp_t *http, const char *at, size_t length) } /* Information-only callbacks, return value is ignored */ -static int -on_version_complete(llhttp_t *http) +static int on_version_complete(llhttp_t *http) { printf_debug_info("on_version_complete", NULL, 0); @@ -297,6 +294,10 @@ on_version_complete(llhttp_t *http) container_of(http, struct http_decoder_half, parser); assert(half); + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_VERSION) == + STRING_STATE_REFER) { + http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_VERSION); + } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_VERSION); half->ref_data->major_version = llhttp_get_http_major(&half->parser); @@ -313,8 +314,7 @@ on_version_complete(llhttp_t *http) } /* Possible return values 0, -1, HPE_USER */ -static int -on_status(llhttp_t *http, const char *at, size_t length) +static int on_status(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_status", at, length); @@ -328,8 +328,7 @@ on_status(llhttp_t *http, const char *at, size_t length) } /* Information-only callbacks, return value is ignored */ -static int -on_status_complete(llhttp_t *http) +static int on_status_complete(llhttp_t *http) { printf_debug_info("on_status_complete", NULL, 0); @@ -337,7 +336,12 @@ on_status_complete(llhttp_t *http) container_of(http, struct http_decoder_half, parser); assert(half); + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_STATUS) == + STRING_STATE_REFER) { + http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_STATUS); + } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_STATUS); + half->ref_data->status_code = llhttp_get_status_code(&half->parser); half->event = HTTP_EVENT_RES_LINE; @@ -349,8 +353,7 @@ on_status_complete(llhttp_t *http) } /* Possible return values 0, -1, HPE_USER */ -static int -on_header_field(llhttp_t *http, const char *at, size_t length) +static int on_header_field(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_header_field", at, length); @@ -364,8 +367,7 @@ on_header_field(llhttp_t *http, const char *at, size_t length) } /* Information-only callbacks, return value is ignored */ -static int -on_header_field_complete(llhttp_t *http) +static int on_header_field_complete(llhttp_t *http) { printf_debug_info("on_header_field_complete", NULL, 0); @@ -377,15 +379,13 @@ on_header_field_complete(llhttp_t *http) STRING_STATE_REFER) { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_HDRKEY); } - http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_HDRKEY); return 0; } /* Possible return values 0, -1, HPE_USER */ -static int -on_header_value(llhttp_t *http, const char *at, size_t length) +static int on_header_value(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_header_value", at, length); @@ -399,8 +399,7 @@ on_header_value(llhttp_t *http, const char *at, size_t length) } /* Information-only callbacks, return value is ignored */ -static int -on_header_value_complete(llhttp_t *http) +static int on_header_value_complete(llhttp_t *http) { printf_debug_info("on_header_value_complete", NULL, 0); @@ -434,8 +433,7 @@ on_header_value_complete(llhttp_t *http) * in parser->content_length. * Possible return values 0, -1, `HPE_PAUSED` */ -static int -on_chunk_header(llhttp_t *http) +static int on_chunk_header(llhttp_t *http) { printf_debug_info("on_chunk_header", NULL, 0); @@ -446,8 +444,7 @@ on_chunk_header(llhttp_t *http) * in parser->content_length. * Possible return values 0, -1, `HPE_PAUSED` */ -static int -on_chunk_header_complete(llhttp_t *http) +static int on_chunk_header_complete(llhttp_t *http) { printf_debug_info("on_chunk_header_complete", NULL, 0); @@ -460,8 +457,7 @@ on_chunk_header_complete(llhttp_t *http) * 2 - Assume absence of body (as above) and make `llhttp_execute()` return `HPE_PAUSED_UPGRADE` * -1 - Error `HPE_PAUSED` */ -static int -on_headers_complete(llhttp_t *http) +static int on_headers_complete(llhttp_t *http) { printf_debug_info("on_headers_complete", NULL, 0); @@ -487,8 +483,7 @@ on_headers_complete(llhttp_t *http) } /* Possible return values 0, -1, HPE_USER */ -static int -on_body(llhttp_t *http, const char *at, size_t length) +static int on_body(llhttp_t *http, const char *at, size_t length) { printf_debug_info("on_body", at, length); @@ -517,8 +512,8 @@ on_body(llhttp_t *http, const char *at, size_t length) // if enable cache_body, trigger body_data event on_message_complete // if disable cache_body, trigger body_data event on_body - if (half->is_cache_body - || (half->ref_data->content_encoding != HTTP_CONTENT_ENCODING_NONE)) { + if (half->is_cache_body || + (half->ref_data->content_encoding != HTTP_CONTENT_ENCODING_NONE)) { http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_BODY, at, length); http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_BODY); @@ -552,9 +547,9 @@ on_body(llhttp_t *http, const char *at, size_t length) return 0; } -static void -http_decoder_half_init(struct http_decoder_half *half, - http_event_cb *http_ev_cb, int is_cache_body, int type) +static void http_decoder_half_init(struct http_decoder_half *half, + http_event_cb *http_ev_cb, + int is_cache_body, int type) { if (NULL == half) { return; @@ -609,8 +604,7 @@ http_decoder_half_new(http_event_cb *ev_cb, int is_cache_body, int type) return half; } -void -http_decoder_half_free(struct http_decoder_half *half) +void http_decoder_half_free(struct http_decoder_half *half) { if (NULL == half) { return; @@ -619,9 +613,8 @@ http_decoder_half_free(struct http_decoder_half *half) FREE(half); } -int -http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, - const char *data, size_t data_len) +int http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, + const char *data, size_t data_len) { if (NULL == half || NULL == data || 0 == data_len) { return -1; @@ -632,6 +625,7 @@ http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, int ret = 0; uint8_t type = 0; + struct http_decoder_half_data *half_data = NULL; switch (half->error) { case HPE_OK: @@ -651,10 +645,10 @@ http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, } if (ret < 0) { - // fprintf(stderr, + // fprintf(stdout, // "llhttp_execute parse error: %s err_reason:%s\n", // llhttp_errno_name(half->error), half->parser.reason); - return -1; + return half->error; } if (half->ref_data != NULL) { @@ -709,8 +703,7 @@ long long http_decoder_half_trans_count(struct http_decoder_half *half) return trans_cnt; } -struct http_decoder_half_data * -http_decoder_half_data_new() +struct http_decoder_half_data *http_decoder_half_data_new() { struct http_decoder_half_data *data = CALLOC(struct http_decoder_half_data, 1); @@ -730,8 +723,7 @@ http_decoder_half_data_new() return data; } -void -http_decoder_half_data_free(struct http_decoder_half_data *data) +void http_decoder_half_data_free(struct http_decoder_half_data *data) { if (NULL == data) { return; @@ -749,9 +741,8 @@ http_decoder_half_data_free(struct http_decoder_half_data *data) FREE(data); } -int -http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, - struct http_request_line *line) +int http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, + struct http_request_line *line) { if (NULL == data || NULL == line) { return -1; @@ -767,9 +758,8 @@ http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, return 0; } -int -http_decoder_half_data_get_response_line(struct http_decoder_half_data *data, - struct http_response_line *line) +int http_decoder_half_data_get_response_line(struct http_decoder_half_data *data, + struct http_response_line *line) { if (NULL == data || NULL == line) { return -1; @@ -785,21 +775,19 @@ http_decoder_half_data_get_response_line(struct http_decoder_half_data *data, return 0; } -int -http_decoder_half_data_get_header(struct http_decoder_half_data *data, - struct hstring *key, struct http_header *header_array, size_t array_size) +int http_decoder_half_data_get_header(struct http_decoder_half_data *data, + struct hstring *key, struct http_header *hdr_array, + size_t array_size) { if (NULL == data) { return -1; } - return http_decoder_table_get_header(data->table, key, header_array, - array_size); + return http_decoder_table_get_header(data->table, key, hdr_array, array_size); } -int -http_decoder_half_data_iter_header(struct http_decoder_half_data *data, - struct http_header *header) +int http_decoder_half_data_iter_header(struct http_decoder_half_data *data, + struct http_header *header) { if (NULL == data || NULL == header) { return -1; @@ -808,9 +796,8 @@ http_decoder_half_data_iter_header(struct http_decoder_half_data *data, return http_decoder_table_iter_header(data->table, header); } -int -http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, - struct hstring *body) +int http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, + struct hstring *body) { if (NULL == data || NULL == body) { return -1; @@ -819,9 +806,8 @@ http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, return http_decoder_table_get_body(data->table, body); } -int -http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *data, - struct hstring *body) +int http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *data, + struct hstring *body) { if (NULL == data || NULL == body) { return -1; @@ -837,8 +823,7 @@ http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *data, return 0; } -void -http_decoder_half_data_dump(struct http_decoder_half *half) +void http_decoder_half_data_dump(struct http_decoder_half *half) { if (NULL == half || NULL == half->ref_data) { return; diff --git a/src/http_decoder/http_decoder_half.h b/src/http_decoder/http_decoder_half.h index 222d040..e566409 100644 --- a/src/http_decoder/http_decoder_half.h +++ b/src/http_decoder/http_decoder_half.h @@ -21,7 +21,7 @@ extern "C" #include "http_decoder.h" #include "http_content_decompress.h" - +#include "http_decoder_result_queue.h" // only one http event is fired at a time enum http_event { @@ -47,55 +47,44 @@ enum http_event { struct http_decoder_half; struct http_decoder_half_data; -typedef void http_event_cb(enum http_event event, - struct http_decoder_half_data **data, void *cb_args); +typedef void http_event_cb(enum http_event event, struct http_decoder_half_data **data, + void *cb_args); struct http_decoder_half * http_decoder_half_new(http_event_cb *event_cb, int is_cache_body, int type); -void -http_decoder_half_free(struct http_decoder_half *half); +void http_decoder_half_free(struct http_decoder_half *half); -int -http_decoder_half_parse(struct http_decoder_half *half, void *ev_ctx, - const char *data, size_t len); +int http_decoder_half_parse(struct http_decoder_half *half, void *ev_ctx, + const char *data, size_t len); long long http_decoder_half_trans_count(struct http_decoder_half *half); //http decoder half data API -struct http_decoder_half_data * -http_decoder_half_data_new(); +struct http_decoder_half_data *http_decoder_half_data_new(); -void -http_decoder_half_data_free(struct http_decoder_half_data *data); +void http_decoder_half_data_free(struct http_decoder_half_data *data); -int -http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, - struct http_request_line *line); +int http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, + struct http_request_line *line); -int -http_decoder_half_data_get_response_line(struct http_decoder_half_data *data, - struct http_response_line *line); +int http_decoder_half_data_get_response_line(struct http_decoder_half_data *data, + struct http_response_line *line); -int -http_decoder_half_data_get_header(struct http_decoder_half_data *data, - struct hstring *key, struct http_header *header_array, - size_t array_size); +int http_decoder_half_data_get_header(struct http_decoder_half_data *data, + struct hstring *key, struct http_header *hdr_array, + size_t array_size); -int -http_decoder_half_data_iter_header(struct http_decoder_half_data *data, - struct http_header *header); +int http_decoder_half_data_iter_header(struct http_decoder_half_data *data, + struct http_header *header); -int -http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, - struct hstring *body); +int http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, + struct hstring *body); -int -http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *data, - struct hstring *body); +int http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *data, + struct hstring *body); -void -http_decoder_half_data_dump(struct http_decoder_half *half); +void http_decoder_half_data_dump(struct http_decoder_half *half); #ifdef __cplusplus } diff --git a/src/http_decoder/http_decoder_result_queue.c b/src/http_decoder/http_decoder_result_queue.c new file mode 100644 index 0000000..eb7b2d1 --- /dev/null +++ b/src/http_decoder/http_decoder_result_queue.c @@ -0,0 +1,155 @@ +/* +********************************************************************************************** +* File: http_decoder_result_queue.c +* Description: +* Authors: Liuwentan +* Date: 2024-01-15 +* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved. +*********************************************************************************************** +*/ + +#include + +#include "stellar/utils.h" +#include "http_decoder_half.h" +#include "http_decoder_result_queue.h" + +struct http_decoder_result_queue * +http_decoder_result_queue_new(size_t queue_size) +{ + struct http_decoder_result_queue *queue = + CALLOC(struct http_decoder_result_queue, 1); + assert(queue); + + queue->req_index = 0; + queue->res_index = 0; + queue->queue_size = queue_size; + + queue->array = CALLOC(struct http_decoder_result, queue->queue_size); + assert(queue->array); + + return queue; +} + +void http_decoder_result_queue_free(struct http_decoder_result_queue *queue) +{ + if (NULL == queue) { + return; + } + + if (queue->array != NULL) { + for (size_t i = 0; i < queue->queue_size; i++) { + if (queue->array[i].req_data != NULL) { + http_decoder_half_data_free(queue->array[i].req_data); + queue->array[i].req_data = NULL; + } + + if (queue->array[i].res_data != NULL) { + http_decoder_half_data_free(queue->array[i].res_data); + queue->array[i].res_data = NULL; + } + } + + FREE(queue->array); + } + + FREE(queue); +} + +void http_decoder_result_queue_inc_req_index(struct http_decoder_result_queue *queue) +{ + assert(queue); + + queue->req_index++; + queue->req_index = queue->req_index % queue->queue_size; +} + +void http_decoder_result_queue_inc_res_index(struct http_decoder_result_queue *queue) +{ + assert(queue); + + queue->res_index++; + queue->res_index = queue->res_index % queue->queue_size; +} + +int http_decoder_result_queue_push_req(struct http_decoder_result_queue *queue, + struct http_decoder_half_data *req_data) +{ + if (NULL == queue || NULL == req_data) { + return -1; + } + + assert(queue->array[queue->req_index].req_data == NULL); + if (queue->array[queue->req_index].req_data != NULL) { + return -1; + } + + queue->array[queue->req_index].req_data = req_data; + return 0; +} + +int http_decoder_result_queue_push_res(struct http_decoder_result_queue *queue, + struct http_decoder_half_data *res_data) +{ + if (NULL == queue || NULL == res_data) { + return -1; + } + + assert(queue->array[queue->res_index].res_data == NULL); + if (queue->array[queue->res_index].res_data != NULL) { + return -1; + } + + queue->array[queue->res_index].res_data = res_data; + return 0; +} + +struct http_decoder_half_data * +http_decoder_result_queue_pop_req(struct http_decoder_result_queue *queue) +{ + if (NULL == queue) { + return NULL; + } + + struct http_decoder_half_data *req_data = + queue->array[queue->req_index].req_data; + queue->array[queue->req_index].req_data = NULL; + + return req_data; +} + +struct http_decoder_half_data * +http_decoder_result_queue_pop_res(struct http_decoder_result_queue *queue) +{ + if (NULL == queue) { + return NULL; + } + + struct http_decoder_half_data *res_data = + queue->array[queue->res_index].res_data; + queue->array[queue->res_index].res_data = NULL; + + return res_data; +} + +struct http_decoder_half_data * +http_decoder_result_queue_peek_req(struct http_decoder_result_queue *queue) +{ + if (NULL == queue) { + return NULL; + } + + assert(queue->req_index < queue->queue_size); + return queue->array[queue->req_index].req_data; +} + +struct http_decoder_half_data * +http_decoder_result_queue_peek_res(struct http_decoder_result_queue *queue) +{ + if (NULL == queue) { + return NULL; + } + + assert(queue->res_index < queue->queue_size); + return queue->array[queue->res_index].res_data; +} \ No newline at end of file diff --git a/src/http_decoder/http_decoder_result_queue.h b/src/http_decoder/http_decoder_result_queue.h new file mode 100644 index 0000000..eef68ba --- /dev/null +++ b/src/http_decoder/http_decoder_result_queue.h @@ -0,0 +1,67 @@ +/* +********************************************************************************************** +* File: http_decoder_result_queue.h +* Description: +* Authors: Liuwentan +* Date: 2024-01-15 +* Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved. +*********************************************************************************************** +*/ + + +#ifndef _HTTP_DECODER_RESULT_QUEUE_H_ +#define _HTTP_DECODER_RESULT_QUEUE_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +#include "http_decoder_half.h" + +struct http_decoder_result { + struct http_decoder_half_data *req_data; + struct http_decoder_half_data *res_data; +}; + +struct http_decoder_result_queue { + size_t req_index; + size_t res_index; + size_t queue_size; + struct http_decoder_result *array; +}; + +struct http_decoder_result_queue * +http_decoder_result_queue_new(size_t queue_size); + +void http_decoder_result_queue_free(struct http_decoder_result_queue *queue); + +void http_decoder_result_queue_inc_req_index(struct http_decoder_result_queue *queue); + +void http_decoder_result_queue_inc_res_index(struct http_decoder_result_queue *queue); + +struct http_decoder_half_data * +http_decoder_result_queue_pop_req(struct http_decoder_result_queue *queue); + +struct http_decoder_half_data * +http_decoder_result_queue_pop_res(struct http_decoder_result_queue *queue); + +int http_decoder_result_queue_push_req(struct http_decoder_result_queue *queue, + struct http_decoder_half_data *req_data); + +int http_decoder_result_queue_push_res(struct http_decoder_result_queue *queue, + struct http_decoder_half_data *res_data); + +struct http_decoder_half_data * +http_decoder_result_queue_peek_req(struct http_decoder_result_queue *queue); + +struct http_decoder_half_data * +http_decoder_result_queue_peek_res(struct http_decoder_result_queue *queue); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/http_decoder/http_decoder_string.c b/src/http_decoder/http_decoder_string.c index d6c53af..cfaeb4c 100644 --- a/src/http_decoder/http_decoder_string.c +++ b/src/http_decoder/http_decoder_string.c @@ -8,7 +8,6 @@ *********************************************************************************************** */ - #include #include #include @@ -18,9 +17,7 @@ #include "http_decoder_utils.h" #include "http_decoder_string.h" - -static const char * -string_state_to_desc(enum string_state state) +static const char *string_state_to_desc(enum string_state state) { switch (state) { case STRING_STATE_INIT: @@ -41,9 +38,8 @@ string_state_to_desc(enum string_state state) } } -void -http_decoder_string_refer(struct http_decoder_string *rstr, - const char *at, size_t length) +void http_decoder_string_refer(struct http_decoder_string *rstr, + const char *at, size_t length) { if (NULL == rstr) { return; @@ -63,8 +59,7 @@ http_decoder_string_refer(struct http_decoder_string *rstr, rstr->state = STRING_STATE_REFER; } -void -http_decoder_string_cache(struct http_decoder_string *rstr) +void http_decoder_string_cache(struct http_decoder_string *rstr) { if (NULL == rstr) { return; @@ -99,8 +94,7 @@ http_decoder_string_cache(struct http_decoder_string *rstr) rstr->state = STRING_STATE_CACHE; } -void -http_decoder_string_commit(struct http_decoder_string *rstr) +void http_decoder_string_commit(struct http_decoder_string *rstr) { if (NULL == rstr) { return; @@ -135,8 +129,7 @@ http_decoder_string_commit(struct http_decoder_string *rstr) rstr->state = STRING_STATE_COMMIT; } -void -http_decoder_string_reset(struct http_decoder_string *rstr) +void http_decoder_string_reset(struct http_decoder_string *rstr) { assert(rstr); @@ -156,14 +149,12 @@ http_decoder_string_reset(struct http_decoder_string *rstr) rstr->state = STRING_STATE_INIT; } -enum string_state -http_decoder_string_state(struct http_decoder_string *rstr) +enum string_state http_decoder_string_state(struct http_decoder_string *rstr) { return rstr->state; } -int -http_decoder_string_get(struct http_decoder_string *rstr, struct hstring *out) +int http_decoder_string_get(struct http_decoder_string *rstr, struct hstring *out) { if (NULL == rstr || NULL == out) { return -1; @@ -180,8 +171,7 @@ http_decoder_string_get(struct http_decoder_string *rstr, struct hstring *out) return 0; } -void -http_decoder_string_dump(struct http_decoder_string *rstr, const char *desc) +void http_decoder_string_dump(struct http_decoder_string *rstr, const char *desc) { if (NULL == rstr) { return; diff --git a/src/http_decoder/http_decoder_string.h b/src/http_decoder/http_decoder_string.h index 6f3ff0b..804a7ff 100644 --- a/src/http_decoder/http_decoder_string.h +++ b/src/http_decoder/http_decoder_string.h @@ -8,7 +8,6 @@ *********************************************************************************************** */ - #ifndef _HTTP_DECODER_STRING_H_ #define _HTTP_DECODER_STRING_H_ @@ -68,27 +67,20 @@ struct http_decoder_string { enum string_state state; }; -void -http_decoder_string_refer(struct http_decoder_string *rstr, - const char *at, size_t length); +void http_decoder_string_refer(struct http_decoder_string *rstr, + const char *at, size_t length); -void -http_decoder_string_cache(struct http_decoder_string *rstr); +void http_decoder_string_cache(struct http_decoder_string *rstr); -void -http_decoder_string_commit(struct http_decoder_string *rstr); +void http_decoder_string_commit(struct http_decoder_string *rstr); -void -http_decoder_string_reset(struct http_decoder_string *rstr); +void http_decoder_string_reset(struct http_decoder_string *rstr); -enum string_state -http_decoder_string_state(struct http_decoder_string *rstr); +enum string_state http_decoder_string_state(struct http_decoder_string *rstr); -int -http_decoder_string_get(struct http_decoder_string *rstr, struct hstring *out); +int http_decoder_string_get(struct http_decoder_string *rstr, struct hstring *out); -void -http_decoder_string_dump(struct http_decoder_string *rstr, const char *desc); +void http_decoder_string_dump(struct http_decoder_string *rstr, const char *desc); #ifdef __cplusplus } diff --git a/src/http_decoder/http_decoder_table.c b/src/http_decoder/http_decoder_table.c index 8151e5c..ced30d0 100644 --- a/src/http_decoder/http_decoder_table.c +++ b/src/http_decoder/http_decoder_table.c @@ -39,8 +39,7 @@ struct http_decoder_table { struct http_decoder_header *headers; }; -struct http_decoder_table * -http_decoder_table_new() +struct http_decoder_table *http_decoder_table_new() { struct http_decoder_table *table = CALLOC(struct http_decoder_table, 1); assert(table); @@ -52,8 +51,7 @@ http_decoder_table_new() return table; } -void -http_decoder_table_free(struct http_decoder_table *table) +void http_decoder_table_free(struct http_decoder_table *table) { if (NULL == table) { return; @@ -121,10 +119,12 @@ http_decoder_table_state(struct http_decoder_table *table, enum http_item type) state = http_decoder_string_state(&table->version); break; case HTTP_ITEM_HDRKEY: + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; state = http_decoder_string_state(&header->key); break; case HTTP_ITEM_HDRVAL: + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; state = http_decoder_string_state(&header->val); break; @@ -139,9 +139,8 @@ 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) { return; @@ -164,19 +163,12 @@ http_decoder_table_refer(struct http_decoder_table *table, enum http_item type, http_decoder_string_refer(&table->version, at, len); break; case HTTP_ITEM_HDRKEY: - if (table->header_index >= table->header_size) { - table->headers = REALLOC(struct http_decoder_header, table->headers, - table->header_size * 2); - table->header_size *= 2; - for (size_t i = table->header_index; i < table->header_size; i++) { - header = &table->headers[i]; - memset(header, 0, sizeof(struct http_decoder_header)); - } - } + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; http_decoder_string_refer(&header->key, at, len); break; case HTTP_ITEM_HDRVAL: + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; http_decoder_string_refer(&header->val, at, len); break; @@ -189,8 +181,7 @@ http_decoder_table_refer(struct http_decoder_table *table, enum http_item type, } } -void -http_decoder_table_cache(struct http_decoder_table *table, enum http_item type) +void http_decoder_table_cache(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return; @@ -213,10 +204,12 @@ http_decoder_table_cache(struct http_decoder_table *table, enum http_item type) http_decoder_string_cache(&table->version); break; case HTTP_ITEM_HDRKEY: + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; http_decoder_string_cache(&header->key); break; case HTTP_ITEM_HDRVAL: + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; http_decoder_string_cache(&header->val); break; @@ -229,8 +222,7 @@ http_decoder_table_cache(struct http_decoder_table *table, enum http_item type) } } -void -http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) +void http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return; @@ -253,6 +245,7 @@ http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) http_decoder_string_commit(&table->version); break; case HTTP_ITEM_HDRKEY: + assert(table->header_index < table->header_size); header = &table->headers[table->header_index]; http_decoder_string_commit(&header->key); break; @@ -260,6 +253,15 @@ http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) header = &table->headers[table->header_index]; http_decoder_string_commit(&header->val); // inc index + if ((table->header_index + 1) >= table->header_size) { + table->headers = REALLOC(struct http_decoder_header, table->headers, + table->header_size * 2); + table->header_size *= 2; + for (size_t i = table->header_index + 1; i < table->header_size; i++) { + header = &table->headers[i]; + memset(header, 0, sizeof(struct http_decoder_header)); + } + } table->header_index++; break; case HTTP_ITEM_BODY: @@ -271,8 +273,7 @@ http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) } } -void -http_decoder_table_reset(struct http_decoder_table *table, enum http_item type) +void http_decoder_table_reset(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return; @@ -311,8 +312,7 @@ http_decoder_table_reset(struct http_decoder_table *table, enum http_item type) } } -void -http_decoder_table_dump(struct http_decoder_table *table) +void http_decoder_table_dump(struct http_decoder_table *table) { if (NULL == table) { return; @@ -335,9 +335,7 @@ http_decoder_table_dump(struct http_decoder_table *table) } } -int -http_decoder_table_get_uri(struct http_decoder_table *table, - struct hstring *out) +int http_decoder_table_get_uri(struct http_decoder_table *table, struct hstring *out) { if (NULL == table || NULL == out) { return -1; @@ -346,9 +344,7 @@ http_decoder_table_get_uri(struct http_decoder_table *table, return http_decoder_string_get(&table->uri, out); } -int -http_decoder_table_get_method(struct http_decoder_table *table, - struct hstring *out) +int http_decoder_table_get_method(struct http_decoder_table *table, struct hstring *out) { if (NULL == table || NULL == out) { return -1; @@ -357,9 +353,7 @@ http_decoder_table_get_method(struct http_decoder_table *table, return http_decoder_string_get(&table->method, out); } -int -http_decoder_table_get_status(struct http_decoder_table *table, - struct hstring *out) +int http_decoder_table_get_status(struct http_decoder_table *table, struct hstring *out) { if (NULL == table || NULL == out) { return -1; @@ -368,9 +362,7 @@ http_decoder_table_get_status(struct http_decoder_table *table, return http_decoder_string_get(&table->status, out); } -int -http_decoder_table_get_version(struct http_decoder_table *table, - struct hstring *out) +int http_decoder_table_get_version(struct http_decoder_table *table, struct hstring *out) { if (NULL == table || NULL == out) { return -1; @@ -379,9 +371,7 @@ http_decoder_table_get_version(struct http_decoder_table *table, return http_decoder_string_get(&table->version, out); } -int -http_decoder_table_get_body(struct http_decoder_table *table, - struct hstring *out) +int http_decoder_table_get_body(struct http_decoder_table *table, struct hstring *out) { if (NULL == table || NULL == out) { return -1; @@ -390,10 +380,8 @@ http_decoder_table_get_body(struct http_decoder_table *table, return http_decoder_string_get(&table->body, out); } -int -http_decoder_table_get_header(struct http_decoder_table *table, - struct hstring *key, struct http_header *header_array, - size_t array_size) +int http_decoder_table_get_header(struct http_decoder_table *table, struct hstring *key, + struct http_header *hdr_array, size_t array_size) { if (NULL == table || NULL == key->str || 0 == key->str_len) { return 0; @@ -413,8 +401,7 @@ http_decoder_table_get_header(struct http_decoder_table *table, if (tmp_key.str_len == key->str_len && (0 == strncasecmp(tmp_key.str, key->str, key->str_len))) { - http_decoder_string_get(&tmp_header->val, - &header_array[header_cnt++].val); + http_decoder_string_get(&tmp_header->val, &hdr_array[header_cnt++].val); } } } @@ -422,11 +409,10 @@ http_decoder_table_get_header(struct http_decoder_table *table, return header_cnt; } -int -http_decoder_table_iter_header(struct http_decoder_table *table, - struct http_header *header) +int http_decoder_table_iter_header(struct http_decoder_table *table, + struct http_header *hdr) { - if (NULL == table || NULL == header) { + if (NULL == table || NULL == hdr) { return -1; } @@ -434,22 +420,22 @@ http_decoder_table_iter_header(struct http_decoder_table *table, return 0; } - struct http_decoder_header *tmp_header = - &table->headers[table->header_iter++]; + struct http_decoder_header *tmp_header = &table->headers[table->header_iter++]; 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_get(&tmp_header->key, &header->key); - http_decoder_string_get(&tmp_header->val, &header->val); + if (http_decoder_string_state(&tmp_header->key) == 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); return 1; } } - header->key.str = NULL; - header->key.str_len = 0; + hdr->key.str = NULL; + hdr->key.str_len = 0; - header->val.str = NULL; - header->val.str_len = 0; + hdr->val.str = NULL; + hdr->val.str_len = 0; return 0; } \ No newline at end of file diff --git a/src/http_decoder/http_decoder_table.h b/src/http_decoder/http_decoder_table.h index 0e04f0e..19a4811 100644 --- a/src/http_decoder/http_decoder_table.h +++ b/src/http_decoder/http_decoder_table.h @@ -34,58 +34,38 @@ enum http_item { struct http_decoder_table; -struct http_decoder_table * -http_decoder_table_new(); +struct http_decoder_table *http_decoder_table_new(); void http_decoder_table_free(struct http_decoder_table *table); enum string_state http_decoder_table_state(struct http_decoder_table *table, enum http_item type); -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); -void -http_decoder_table_cache(struct http_decoder_table *table, enum http_item type); +void http_decoder_table_cache(struct http_decoder_table *table, enum http_item type); -void -http_decoder_table_commit(struct http_decoder_table *table, enum http_item type); +void http_decoder_table_commit(struct http_decoder_table *table, enum http_item type); -void -http_decoder_table_reset(struct http_decoder_table *table, enum http_item type); +void http_decoder_table_reset(struct http_decoder_table *table, enum http_item type); -void -http_decoder_table_dump(struct http_decoder_table *table); +void http_decoder_table_dump(struct http_decoder_table *table); -int -http_decoder_table_get_uri(struct http_decoder_table *table, - struct hstring *out); +int http_decoder_table_get_uri(struct http_decoder_table *table, struct hstring *out); -int -http_decoder_table_get_method(struct http_decoder_table *table, - struct hstring *out); +int http_decoder_table_get_method(struct http_decoder_table *table, struct hstring *out); -int -http_decoder_table_get_status(struct http_decoder_table *table, - struct hstring *out); +int http_decoder_table_get_status(struct http_decoder_table *table, struct hstring *out); -int -http_decoder_table_get_version(struct http_decoder_table *table, - struct hstring *out); +int http_decoder_table_get_version(struct http_decoder_table *table, struct hstring *out); -int -http_decoder_table_get_body(struct http_decoder_table *table, - struct hstring *out); +int http_decoder_table_get_body(struct http_decoder_table *table, struct hstring *out); -int -http_decoder_table_get_header(struct http_decoder_table *table, - struct hstring *key, struct http_header *header_array, - size_t array_size); +int http_decoder_table_get_header(struct http_decoder_table *table, struct hstring *key, + struct http_header *hdr_array, size_t array_size); -int -http_decoder_table_iter_header(struct http_decoder_table *table, - struct http_header *header); +int http_decoder_table_iter_header(struct http_decoder_table *table, struct http_header *hdr); #ifdef __cplusplus } diff --git a/src/http_decoder/http_decoder_utils.c b/src/http_decoder/http_decoder_utils.c index 33b2d8e..a5dfbe1 100644 --- a/src/http_decoder/http_decoder_utils.c +++ b/src/http_decoder/http_decoder_utils.c @@ -8,14 +8,11 @@ *********************************************************************************************** */ - #include #include "stellar/utils.h" - -char * -safe_dup(const char *str, size_t len) +char *safe_dup(const char *str, size_t len) { if (str == NULL || len == 0) { return NULL; diff --git a/src/http_decoder/http_decoder_utils.h b/src/http_decoder/http_decoder_utils.h index 97f1fd2..9c031a3 100644 --- a/src/http_decoder/http_decoder_utils.h +++ b/src/http_decoder/http_decoder_utils.h @@ -21,8 +21,7 @@ extern "C" #include -char * -safe_dup(const char *str, size_t len); +char *safe_dup(const char *str, size_t len); /****************************************************************************** * Logger diff --git a/test/http_decoder/http_decoder_gtest.cpp b/test/http_decoder/http_decoder_gtest.cpp index 72c6769..725f42d 100644 --- a/test/http_decoder/http_decoder_gtest.cpp +++ b/test/http_decoder/http_decoder_gtest.cpp @@ -119,7 +119,7 @@ http_field_to_json(cJSON *object, const char *key, char *val, size_t val_len) memcpy(tmp, val, val_len); cJSON_AddStringToObject(object, key, tmp); FREE(tmp); - + return 0; } @@ -154,7 +154,7 @@ http_header_to_json(cJSON *ctx, struct http_header *header) { char key[MAX_KEY_STR_LEN] = {0}; - snprintf(key, header->key.str_len + 1, "%s", header->key.str); + memcpy(key, header->key.str, header->key.str_len); if (cJSON_HasObjectItem(ctx, key) == FALSE) { http_field_to_json(ctx, key, header->val.str, header->val.str_len); diff --git a/test/http_decoder/test_result_json/http_multi_parse_error.json b/test/http_decoder/test_result_json/http_multi_parse_error.json index 9a6f450..c61f70a 100644 --- a/test/http_decoder/test_result_json/http_multi_parse_error.json +++ b/test/http_decoder/test_result_json/http_multi_parse_error.json @@ -27,6 +27,34 @@ "Date": "Sat Sep 7 01:09:08 2019", "name": "HTTP_DECODER_RESULT_2" }, + { + "Tuple4": "192.168.131.33.47166>192.168.204.67.4445", + "method": "POST", + "uri": "http://:4445/RPC2", + "req_version": "1.1", + "major_version": 1, + "minor_version": 1, + "User-Agent": "ulxmlrpcpp/1.7.5", + "Connection": "Close", + "Content-Type": "text/xml", + "Date": "Sat Sep 7 10:05:05 2019", + "Content-Length": "468", + "name": "HTTP_DECODER_RESULT_3" + }, + { + "Tuple4": "192.168.131.33.47166>192.168.204.67.4445", + "res_version": "1.1", + "res_status": "OK", + "major_version": 1, + "minor_version": 1, + "status_code": 200, + "Connection": "Close", + "Content-Type": "text/xml", + "Transfer-Encoding": "chunked", + "X-Powered-By": "ulxmlrpcpp/1.7.4", + "Date": "Sat Sep 7 01:09:16 2019", + "name": "HTTP_DECODER_RESULT_4" + }, { "Tuple4": "192.168.131.33.47172>192.168.204.67.4445", "method": "POST", @@ -39,7 +67,7 @@ "Content-Type": "text/xml", "Date": "Sat Sep 7 10:05:13 2019", "Content-Length": "468", - "name": "HTTP_DECODER_RESULT_3" + "name": "HTTP_DECODER_RESULT_5" }, { "Tuple4": "192.168.131.33.47172>192.168.204.67.4445", @@ -53,7 +81,34 @@ "Transfer-Encoding": "chunked", "X-Powered-By": "ulxmlrpcpp/1.7.4", "Date": "Sat Sep 7 01:09:24 2019", - "name": "HTTP_DECODER_RESULT_4" + "name": "HTTP_DECODER_RESULT_6" + }, + { + "Tuple4": "192.168.131.33.47194>192.168.204.67.4445", + "method": "POST", + "uri": "http://:4445/RPC2", + "req_version": "1.1", + "major_version": 1, + "minor_version": 1, + "User-Agent": "ulxmlrpcpp/1.7.5", + "Connection": "Close", + "Content-Type": "text/xml", + "Date": "Sat Sep 7 10:05:22 2019", + "Content-Length": "306", + "name": "HTTP_DECODER_RESULT_7" + }, + { + "Tuple4": "192.168.131.33.47194>192.168.204.67.4445", + "res_version": "1.1", + "res_status": "OK", + "major_version": 1, + "minor_version": 1, + "status_code": 200, + "Connection": "Close", + "Transfer-Encoding": "chunked", + "X-Powered-By": "ulxmlrpcpp/1.7.4", + "Date": "Sat Sep 7 01:09:32 2019", + "name": "HTTP_DECODER_RESULT_8" }, { "Tuple4": "192.168.131.33.47196>192.168.204.67.4445", @@ -67,7 +122,7 @@ "Content-Type": "text/xml", "Date": "Sat Sep 7 10:05:30 2019", "Content-Length": "225", - "name": "HTTP_DECODER_RESULT_5" + "name": "HTTP_DECODER_RESULT_9" }, { "Tuple4": "192.168.131.33.47196>192.168.204.67.4445", @@ -81,7 +136,35 @@ "Transfer-Encoding": "chunked", "X-Powered-By": "ulxmlrpcpp/1.7.4", "Date": "Sat Sep 7 01:09:42 2019", - "name": "HTTP_DECODER_RESULT_6" + "name": "HTTP_DECODER_RESULT_10" + }, + { + "Tuple4": "192.168.131.33.47200>192.168.204.67.4445", + "method": "POST", + "uri": "http://:4445/RPC2", + "req_version": "1.1", + "major_version": 1, + "minor_version": 1, + "User-Agent": "ulxmlrpcpp/1.7.5", + "Connection": "Close", + "Content-Type": "text/xml", + "Date": "Sat Sep 7 10:05:39 2019", + "Content-Length": "461", + "name": "HTTP_DECODER_RESULT_11" + }, + { + "Tuple4": "192.168.131.33.47200>192.168.204.67.4445", + "res_version": "1.1", + "res_status": "OK", + "major_version": 1, + "minor_version": 1, + "status_code": 200, + "Connection": "Close", + "Content-Type": "text/xml", + "Transfer-Encoding": "chunked", + "X-Powered-By": "ulxmlrpcpp/1.7.4", + "Date": "Sat Sep 7 01:09:50 2019", + "name": "HTTP_DECODER_RESULT_12" }, { "Tuple4": "192.168.131.33.47214>192.168.204.67.4445", @@ -95,7 +178,7 @@ "Content-Type": "text/xml", "Date": "Sat Sep 7 10:05:47 2019", "Content-Length": "461", - "name": "HTTP_DECODER_RESULT_7" + "name": "HTTP_DECODER_RESULT_13" }, { "Tuple4": "192.168.131.33.47214>192.168.204.67.4445", @@ -109,6 +192,6 @@ "Transfer-Encoding": "chunked", "X-Powered-By": "ulxmlrpcpp/1.7.4", "Date": "Sat Sep 7 01:09:58 2019", - "name": "HTTP_DECODER_RESULT_8" + "name": "HTTP_DECODER_RESULT_14" } ] \ No newline at end of file -- cgit v1.2.3