diff options
| author | lijia <[email protected]> | 2024-04-12 15:01:57 +0800 |
|---|---|---|
| committer | lijia <[email protected]> | 2024-04-12 15:01:57 +0800 |
| commit | 9ea86638e1676a6fc8c8892beb3fb6afd431b5cc (patch) | |
| tree | 577f3c13e07dba159f8c77800b8bf9220c380643 /src | |
| parent | 9403db98e5e0b435ee9da8e049be0ed2d1727cc9 (diff) | |
Remove the dir of http_message_get_xxx API, because msg type already contains this information
Diffstat (limited to 'src')
| -rw-r--r-- | src/http_decoder.c | 491 | ||||
| -rw-r--r-- | src/http_decoder_half.c | 319 | ||||
| -rw-r--r-- | src/http_decoder_half.h | 5 | ||||
| -rw-r--r-- | src/http_decoder_table.c | 23 | ||||
| -rw-r--r-- | src/http_decoder_table.h | 5 |
5 files changed, 521 insertions, 322 deletions
diff --git a/src/http_decoder.c b/src/http_decoder.c index 43eb8bf..b9d43d6 100644 --- a/src/http_decoder.c +++ b/src/http_decoder.c @@ -1,7 +1,7 @@ /* ********************************************************************************************** * File: http_decoder.c -* Description: +* Description: * Authors: Liu WenTan <[email protected]> * Date: 2024-01-10 * Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved. @@ -36,9 +36,10 @@ const char *g_hd_cfg_path = "./etc/http/http_decoder.toml"; const char *http_decoder_topic = "HTTP_DECODER_MESSAGE"; const char *fs_file_name = "http_decoder.fs"; -struct http_decoder_config { +struct http_decoder_config +{ int decompress_switch; - int stat_interval_pkts; //call fieldstat_incrby every stat_interval_pkts + int stat_interval_pkts; // call fieldstat_incrby every stat_interval_pkts int stat_output_interval; size_t result_queue_len; // per session result queue length size_t mempool_size; // per session mempool size @@ -47,24 +48,28 @@ struct http_decoder_config { /** * NOTE: http_message don't have the ownership of data */ -struct http_message { - enum http_message_type type; +struct http_message +{ + enum http_message_type type; struct http_decoder_result_queue *ref_queue; size_t queue_index; }; -struct http_decoder { - struct http_decoder_half *c2s_half; - struct http_decoder_half *s2c_half; +struct http_decoder +{ + struct http_decoder_half *c2s_half; + struct http_decoder_half *s2c_half; }; -struct http_decoder_exdata { +struct http_decoder_exdata +{ struct http_decoder_result_queue *queue; struct http_decoder *decoder; nmx_pool_t *mempool; }; -struct http_decoder_stat { +struct http_decoder_stat +{ long long incoming_bytes; long long incoming_pkts; long long incoming_trans; @@ -72,9 +77,10 @@ struct http_decoder_stat { int counter; }; -struct http_decoder_context { +struct http_decoder_context +{ int plugin_id; - int topic_id; + int topic_id; int ex_data_idx; int fs_incoming_bytes_id; int fs_incoming_pkts_id; @@ -103,9 +109,10 @@ http_message_new(enum http_message_type type, static void http_message_free(void *http_msg, void *cb_arg) { - if (NULL == http_msg) { - return; - } + if (NULL == http_msg) + { + return; + } FREE(http_msg); } @@ -123,15 +130,18 @@ static void http_event_handler(enum http_event event, struct http_decoder_half_data *half_data = NULL; int ret = 0; - switch (event) { + switch (event) + { case HTTP_EVENT_REQ_INIT: half_data = http_decoder_result_queue_peek_req(queue); - if (half_data != NULL) { + 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) { + if (half_data != NULL) + { half_data = http_decoder_result_queue_pop_req(queue); http_decoder_half_data_free(mempool, half_data); half_data = NULL; @@ -139,7 +149,8 @@ static void http_event_handler(enum http_event event, half_data = http_decoder_half_data_new(mempool); ret = http_decoder_result_queue_push_req(queue, half_data); - if (ret < 0) { + if (ret < 0) + { fprintf(stderr, "http_decoder_result_queue_push req failed."); http_decoder_half_data_free(mempool, half_data); half_data = NULL; @@ -153,16 +164,17 @@ static void http_event_handler(enum http_event event, break; case HTTP_EVENT_REQ_HDR_END: { - int build_url_final = http_decoder_join_url_finally(ev_ctx, http_decoder_result_queue_peek_req(queue),mempool); + int build_url_final = http_decoder_join_url_finally(ev_ctx, http_decoder_result_queue_peek_req(queue), mempool); ret = http_decoder_half_data_has_parsed_header(*data); - if (0 == ret && 0 == build_url_final) { + if (0 == ret && 0 == build_url_final) + { break; } queue_idx = http_decoder_result_queue_req_index(queue); msg = http_message_new(HTTP_MESSAGE_REQ_HEADER, queue, queue_idx); session_mq_publish_message(ev_ctx->ref_session, ev_ctx->topic_id, msg); } - break; + break; case HTTP_EVENT_REQ_BODY_BEGIN: break; case HTTP_EVENT_REQ_BODY_DATA: @@ -174,20 +186,23 @@ static void http_event_handler(enum http_event event, break; case HTTP_EVENT_REQ_END: http_decoder_result_queue_inc_req_index(queue); - half_data = http_decoder_result_queue_pop_req(queue); - if (half_data != NULL) { + half_data = http_decoder_result_queue_pop_req(queue); + if (half_data != NULL) + { http_decoder_half_data_free(mempool, half_data); half_data = NULL; } break; case HTTP_EVENT_RES_INIT: half_data = http_decoder_result_queue_peek_res(queue); - if (half_data != NULL) { + 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) { + if (half_data != NULL) + { half_data = http_decoder_result_queue_pop_res(queue); http_decoder_half_data_free(mempool, half_data); half_data = NULL; @@ -195,7 +210,8 @@ static void http_event_handler(enum http_event event, half_data = http_decoder_half_data_new(mempool); ret = http_decoder_result_queue_push_res(queue, half_data); - if (ret < 0) { + if (ret < 0) + { fprintf(stderr, "http_decoder_result_queue_push res failed."); http_decoder_half_data_free(mempool, half_data); half_data = NULL; @@ -203,7 +219,7 @@ static void http_event_handler(enum http_event event, *data = half_data; break; case HTTP_EVENT_RES_LINE: - queue_idx = http_decoder_result_queue_res_index(queue); + queue_idx = http_decoder_result_queue_res_index(queue); msg = http_message_new(HTTP_MESSAGE_RES_LINE, queue, queue_idx); session_mq_publish_message(ev_ctx->ref_session, ev_ctx->topic_id, msg); break; @@ -211,27 +227,29 @@ static void http_event_handler(enum http_event event, break; case HTTP_EVENT_RES_HDR_END: ret = http_decoder_half_data_has_parsed_header(*data); - if (0 == ret) { + if (0 == ret) + { break; } queue_idx = http_decoder_result_queue_res_index(queue); msg = http_message_new(HTTP_MESSAGE_RES_HEADER, queue, queue_idx); - session_mq_publish_message(ev_ctx->ref_session, ev_ctx->topic_id, msg); + session_mq_publish_message(ev_ctx->ref_session, ev_ctx->topic_id, msg); break; case HTTP_EVENT_RES_BODY_BEGIN: break; case HTTP_EVENT_RES_BODY_DATA: queue_idx = http_decoder_result_queue_res_index(queue); msg = http_message_new(HTTP_MESSAGE_RES_BODY, queue, queue_idx); - session_mq_publish_message(ev_ctx->ref_session, ev_ctx->topic_id, msg); + session_mq_publish_message(ev_ctx->ref_session, ev_ctx->topic_id, msg); break; case HTTP_EVENT_RES_BODY_END: break; case HTTP_EVENT_RES_END: http_decoder_result_queue_inc_res_index(queue); - half_data = http_decoder_result_queue_pop_res(queue); - if (half_data != NULL) { + half_data = http_decoder_result_queue_pop_res(queue); + if (half_data != NULL) + { http_decoder_half_data_free(mempool, half_data); half_data = NULL; } @@ -246,30 +264,33 @@ static struct http_decoder * http_decoder_new(nmx_pool_t *mempool, http_event_cb *ev_cb, int decompress_switch) { - struct http_decoder *decoder = MEMPOOL_CALLOC(mempool, struct http_decoder, 1); - assert(decoder); + struct http_decoder *decoder = MEMPOOL_CALLOC(mempool, struct http_decoder, 1); + assert(decoder); - decoder->c2s_half = http_decoder_half_new(mempool, ev_cb, HTTP_REQUEST, + decoder->c2s_half = http_decoder_half_new(mempool, ev_cb, HTTP_REQUEST, decompress_switch); - decoder->s2c_half = http_decoder_half_new(mempool, ev_cb, HTTP_RESPONSE, + decoder->s2c_half = http_decoder_half_new(mempool, ev_cb, HTTP_RESPONSE, decompress_switch); - return decoder; + return decoder; } static void http_decoder_free(nmx_pool_t *mempool, struct http_decoder *decoder) { - if (NULL == decoder) { + if (NULL == decoder) + { return; } - if (decoder->c2s_half != NULL) { + if (decoder->c2s_half != NULL) + { http_decoder_half_free(mempool, decoder->c2s_half); decoder->c2s_half = NULL; } - if (decoder->s2c_half != NULL) { + if (decoder->s2c_half != NULL) + { http_decoder_half_free(mempool, decoder->s2c_half); decoder->s2c_half = NULL; } @@ -293,16 +314,19 @@ http_decoder_exdata_new(size_t mempool_size, size_t queue_size, static void http_decoder_exdata_free(struct http_decoder_exdata *ex_data) { - if (NULL == ex_data) { + if (NULL == ex_data) + { return; } - if (ex_data->decoder != NULL) { + if (ex_data->decoder != NULL) + { http_decoder_free(ex_data->mempool, ex_data->decoder); ex_data->decoder = NULL; } - if (ex_data->queue != NULL) { + if (ex_data->queue != NULL) + { http_decoder_result_queue_free(ex_data->mempool, ex_data->queue); ex_data->queue = NULL; } @@ -314,19 +338,20 @@ static void http_decoder_exdata_free(struct http_decoder_exdata *ex_data) static int http_protocol_identify(const char *data, size_t data_len) { - llhttp_t parser; - llhttp_settings_t settings; - enum llhttp_errno error; + llhttp_t parser; + llhttp_settings_t settings; + enum llhttp_errno error; - llhttp_settings_init(&settings); - llhttp_init(&parser, HTTP_BOTH, &settings); + llhttp_settings_init(&settings); + llhttp_init(&parser, HTTP_BOTH, &settings); - error = llhttp_execute(&parser, data, data_len); - if (error != HPE_OK) { - return -1; - } + error = llhttp_execute(&parser, data, data_len); + if (error != HPE_OK) + { + return -1; + } - return 0; + return 0; } static int @@ -334,46 +359,53 @@ http_decoder_stat_init(struct http_decoder_context *ctx, int thread_num) { ctx->fse = fieldstat_easy_new(thread_num, "http_decoder_statistics", NULL, 0); - if (NULL == ctx->fse) { + if (NULL == ctx->fse) + { fprintf(stderr, "fieldstat_easy_new failed."); return -1; } ctx->fs_incoming_bytes_id = fieldstat_easy_register_counter(ctx->fse, "incoming_bytes"); - if (ctx->fs_incoming_bytes_id < 0) { + if (ctx->fs_incoming_bytes_id < 0) + { fprintf(stderr, "fieldstat_easy_register_counter incoming_bytes failed."); return -1; } ctx->fs_incoming_trans_id = fieldstat_easy_register_counter(ctx->fse, "incoming_trans"); - if (ctx->fs_incoming_trans_id < 0) { + if (ctx->fs_incoming_trans_id < 0) + { fprintf(stderr, "fieldstat_easy_register_counter incoming_trans failed."); return -1; } ctx->fs_incoming_pkts_id = fieldstat_easy_register_counter(ctx->fse, "incoming_pkts"); - if (ctx->fs_incoming_pkts_id < 0) { + if (ctx->fs_incoming_pkts_id < 0) + { fprintf(stderr, "fieldstat_easy_register_counter incoming_pkts failed."); return -1; } ctx->fs_err_pkts_id = fieldstat_easy_register_counter(ctx->fse, "err_pkts"); - if (ctx->fs_err_pkts_id < 0) { + if (ctx->fs_err_pkts_id < 0) + { fprintf(stderr, "fieldstat_easy_register_counter err_pkts failed."); return -1; } int stat_output_interval = DEFAULT_STAT_OUTPUT_INTERVAL; - if (ctx->hd_cfg.stat_output_interval > 0) { + if (ctx->hd_cfg.stat_output_interval > 0) + { stat_output_interval = ctx->hd_cfg.stat_output_interval; } int ret = fieldstat_easy_enable_auto_output(ctx->fse, fs_file_name, stat_output_interval); - if (ret < 0) { + if (ret < 0) + { fprintf(stderr, "fieldstat_easy_enable_auto_output failed."); return -1; } @@ -386,16 +418,19 @@ http_decoder_stat_init(struct http_decoder_context *ctx, int thread_num) static void http_decoder_stat_output(struct http_decoder_context *ctx, int thread_id) { - if (NULL == ctx || thread_id < 0) { + if (NULL == ctx || thread_id < 0) + { return; } int stat_interval_pkts = DEFAULT_STAT_INTERVAL_PKTS; - if (ctx->hd_cfg.stat_interval_pkts > 0) { + if (ctx->hd_cfg.stat_interval_pkts > 0) + { stat_interval_pkts = ctx->hd_cfg.stat_interval_pkts; } - if (_th_stat.counter >= stat_interval_pkts) { + if (_th_stat.counter >= stat_interval_pkts) + { fieldstat_easy_counter_incrby(ctx->fse, thread_id, ctx->fs_incoming_bytes_id, NULL, 0, _th_stat.incoming_bytes); @@ -403,7 +438,7 @@ http_decoder_stat_output(struct http_decoder_context *ctx, int thread_id) fieldstat_easy_counter_incrby(ctx->fse, thread_id, ctx->fs_incoming_pkts_id, NULL, 0, _th_stat.incoming_pkts); - + fieldstat_easy_counter_incrby(ctx->fse, thread_id, ctx->fs_incoming_trans_id, NULL, 0, _th_stat.incoming_trans); @@ -428,15 +463,18 @@ int http_decoder_entry(struct session *sess, int events, uint64_t inner_flag = 0; int ret = session_is_inner_most(sess, &inner_flag); - if (0 == ret) { + if (0 == ret) + { return 0; } struct http_decoder_exdata *ex_data = session_get_ex_data(sess, ctx->ex_data_idx); - if (events & SESS_EV_CLOSING) { - if (ex_data != NULL) { + if (events & SESS_EV_CLOSING) + { + if (ex_data != NULL) + { http_decoder_exdata_free(ex_data); session_set_ex_data(sess, ctx->ex_data_idx, NULL); } @@ -445,18 +483,21 @@ int http_decoder_entry(struct session *sess, int events, } const char *payload = session_get0_current_payload(sess, &payload_len); - - if (events & SESS_EV_OPENING) { + + if (events & SESS_EV_OPENING) + { assert(ex_data == NULL); - //If not http, ignore this session - if (payload_len > 0) { + // 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; - + ? HTTP_IDENTIFY_LEN + : payload_len; + ret = http_protocol_identify(payload, http_identify_len); - if (ret < 0) { + if (ret < 0) + { // ignore this session's event struct session_event *s_event = session_get_intrinsic_event(sess, ctx->plugin_id); @@ -473,28 +514,34 @@ int http_decoder_entry(struct session *sess, int events, session_set_ex_data(sess, ctx->ex_data_idx, ex_data); } - if (0 == payload_len || NULL == ex_data) { + if (0 == payload_len || NULL == ex_data) + { return 0; } int dir = packet_get_direction(pkt); - if (dir < 0) { + if (dir < 0) + { return -1; } int thread_id = session_get_current_thread_id(sess); struct http_decoder_half *cur_half = NULL; - if (dir == PACKET_DIRECTION_C2S) { + if (dir == PACKET_DIRECTION_C2S) + { cur_half = ex_data->decoder->c2s_half; - } else { + } + else + { cur_half = ex_data->decoder->s2c_half; } http_decoder_half_reinit(cur_half, ctx->topic_id, ex_data->queue, ex_data->mempool, sess); ret = http_decoder_half_parse(cur_half, payload, payload_len); - if (ret < 0) { + if (ret < 0) + { _th_stat.err_pkts += 1; } @@ -510,16 +557,19 @@ int http_decoder_entry(struct session *sess, int events, static void _http_decoder_context_free(struct http_decoder_context *ctx) { - if (NULL == ctx) { + if (NULL == ctx) + { return; } - if (ctx->fse != NULL) { + if (ctx->fse != NULL) + { fieldstat_easy_free(ctx->fse); ctx->fse = NULL; } - if (ctx->topic_id >= 0) { + if (ctx->topic_id >= 0) + { session_mq_destroy_topic(ctx->st, ctx->topic_id); ctx->topic_id = -1; } @@ -530,7 +580,8 @@ static void _http_decoder_context_free(struct http_decoder_context *ctx) static void http_decoder_ex_data_free(struct session *s, int idx, void *ex_data, void *arg) { - if (NULL == ex_data) { + if (NULL == ex_data) + { return; } @@ -542,7 +593,8 @@ static int load_http_decoder_config(const char *cfg_path, struct http_decoder_config *hd_cfg) { FILE *fp = fopen(cfg_path, "r"); - if (NULL == fp) { + if (NULL == fp) + { fprintf(stderr, "[%s:%d]Can't open config file:%s", __FUNCTION__, __LINE__, cfg_path); return -1; @@ -555,7 +607,8 @@ static int load_http_decoder_config(const char *cfg_path, fclose(fp); toml_table_t *basic_sec_tbl = toml_table_in(root, "basic"); - if (NULL == basic_sec_tbl) { + if (NULL == basic_sec_tbl) + { fprintf(stderr, "[%s:%d]config file:%s has no key: [basic]", __FUNCTION__, __LINE__, cfg_path); ret = -1; @@ -563,35 +616,48 @@ static int load_http_decoder_config(const char *cfg_path, } toml_datum_t int_val = toml_int_in(basic_sec_tbl, "decompress"); - if (int_val.ok != 0) { + if (int_val.ok != 0) + { hd_cfg->decompress_switch = int_val.u.b; } int_val = toml_int_in(basic_sec_tbl, "mempool_size"); - if (int_val.ok != 0) { + if (int_val.ok != 0) + { hd_cfg->mempool_size = int_val.u.i; - } else { + } + else + { hd_cfg->mempool_size = DEFAULT_MEMPOOL_SIZE; } int_val = toml_int_in(basic_sec_tbl, "result_queue_len"); - if (int_val.ok != 0) { + if (int_val.ok != 0) + { hd_cfg->result_queue_len = int_val.u.i; - } else { + } + else + { hd_cfg->result_queue_len = HD_RESULT_QUEUE_LEN; } int_val = toml_int_in(basic_sec_tbl, "stat_interval_pkts"); - if (int_val.ok != 0) { + if (int_val.ok != 0) + { hd_cfg->stat_interval_pkts = int_val.u.i; - } else { + } + else + { hd_cfg->stat_interval_pkts = DEFAULT_STAT_INTERVAL_PKTS; } int_val = toml_int_in(basic_sec_tbl, "stat_output_interval"); - if (int_val.ok != 0) { + if (int_val.ok != 0) + { hd_cfg->stat_output_interval = int_val.u.i; - } else { + } + else + { hd_cfg->stat_output_interval = DEFAULT_STAT_OUTPUT_INTERVAL; } @@ -609,25 +675,28 @@ void *http_decoder_init(struct stellar *st) struct http_decoder_context *ctx = CALLOC(struct http_decoder_context, 1); int ret = load_http_decoder_config(g_hd_cfg_path, &ctx->hd_cfg); - if (ret < 0) { + if (ret < 0) + { goto failed; } ctx->st = st; ctx->ex_data_idx = stellar_session_get_ex_new_index(st, "HTTP_DECODER", - http_decoder_ex_data_free, + http_decoder_ex_data_free, NULL); - plugin_id = stellar_plugin_register(st, SESS_EV_TCP|SESS_EV_CLOSING, - http_decoder_entry, ctx); - if (plugin_id < 0) { + plugin_id = stellar_plugin_register(st, SESS_EV_TCP | SESS_EV_CLOSING, + http_decoder_entry, ctx); + if (plugin_id < 0) + { goto failed; } ctx->plugin_id = plugin_id; topic_id = session_mq_get_topic_id(st, http_decoder_topic); - if (topic_id < 0) { + if (topic_id < 0) + { topic_id = session_mq_create_topic(st, http_decoder_topic, http_message_free, NULL); } @@ -636,10 +705,11 @@ void *http_decoder_init(struct stellar *st) thread_num = stellar_get_worker_thread_num(st); - if (http_decoder_stat_init(ctx, thread_num) < 0) { + if (http_decoder_stat_init(ctx, thread_num) < 0) + { goto failed; } - + 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); @@ -652,7 +722,8 @@ failed: void http_decoder_exit(void *decoder_ctx) { - if (NULL == decoder_ctx) { + if (NULL == decoder_ctx) + { return; } @@ -664,20 +735,22 @@ void http_decoder_exit(void *decoder_ctx) enum http_message_type http_message_type(struct http_message *msg) { - if (NULL == msg) { - return HTTP_MESSAGE_MAX; - } + if (NULL == msg) + { + return HTTP_MESSAGE_MAX; + } - return msg->type; + return msg->type; } 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) { - return -1; - } + if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_LINE || + NULL == line) + { + return -1; + } assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); @@ -691,10 +764,11 @@ int http_message_get_request_line(struct http_message *msg, 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) { - return -1; - } + if (NULL == msg || msg->type != HTTP_MESSAGE_RES_LINE || + NULL == line) + { + return -1; + } assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); @@ -702,143 +776,182 @@ int http_message_get_response_line(struct http_message *msg, struct http_decoder_half_data *res_data = msg->ref_queue->array[msg->queue_index].res_data; - return http_decoder_half_data_get_response_line(res_data, line); + return http_decoder_half_data_get_response_line(res_data, line); } -int http_message_get_request_header(struct http_message *msg, struct hstring *key, - struct http_header *hdr_array, size_t array_size) +static int http_msg_get_request_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_result) { - if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER || - NULL == key || NULL == hdr_array || 0 == array_size) { - return -1; - } - - assert(msg->ref_queue); - assert(msg->queue_index < HD_RESULT_QUEUE_LEN); - struct http_decoder_half_data *req_data = msg->ref_queue->array[msg->queue_index].req_data; - - return http_decoder_half_data_get_header(req_data, key, hdr_array, array_size); + return http_decoder_half_data_get_header(req_data, key, hdr_result); } -int http_message_get_response_header(struct http_message *msg, struct hstring *key, - struct http_header *hdr_array, size_t array_size) +static int http_msg_get_response_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_result) { - if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER || NULL == key || - NULL == hdr_array || 0 == array_size) { - return -1; - } - - assert(msg->ref_queue); - assert(msg->queue_index < HD_RESULT_QUEUE_LEN); - struct http_decoder_half_data *res_data = msg->ref_queue->array[msg->queue_index].res_data; - - return http_decoder_half_data_get_header(res_data, key, hdr_array, array_size); + return http_decoder_half_data_get_header(res_data, key, hdr_result); } -int http_message_request_header_next(struct http_message *msg, - struct http_header *hdr) +int http_message_get_header(struct http_message *msg, struct hstring *key, + struct http_header *hdr_result) { - if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER - || NULL == hdr) { - return -1; - } - + if (NULL == msg || NULL == key || NULL == hdr_result) + { + return -1; + } assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); + if (HTTP_MESSAGE_REQ_HEADER == msg->type) + { + return http_msg_get_request_header(msg, key, hdr_result); + } + else if (HTTP_MESSAGE_RES_HEADER == msg->type) + { + return http_msg_get_response_header(msg, key, hdr_result); + } + return -1; +} + +static int http_msg_request_header_next(struct http_message *msg, + struct http_header *hdr) +{ struct http_decoder_half_data *req_data = msg->ref_queue->array[msg->queue_index].req_data; - return http_decoder_half_data_iter_header(req_data, hdr); } -int http_message_response_header_next(struct http_message *msg, - struct http_header *hdr) +static int http_msg_response_header_next(struct http_message *msg, + struct http_header *hdr) { - if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER || - NULL == hdr) { - return -1; - } - - assert(msg->ref_queue); - assert(msg->queue_index < HD_RESULT_QUEUE_LEN); - struct http_decoder_half_data *res_data = msg->ref_queue->array[msg->queue_index].res_data; - return http_decoder_half_data_iter_header(res_data, hdr); } -int http_message_get_request_raw_body(struct http_message *msg, - struct hstring *body) +int http_message_header_next(struct http_message *msg, + struct http_header *header) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY) || - NULL == body) { + if (NULL == msg || NULL == header) + { return -1; } - assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); + if (HTTP_MESSAGE_REQ_HEADER == msg->type) + { + return http_msg_request_header_next(msg, header); + } + else if (HTTP_MESSAGE_RES_HEADER == msg->type) + { + return http_msg_response_header_next(msg, header); + } - struct http_decoder_half_data *req_data = - msg->ref_queue->array[msg->queue_index].req_data; - - return http_decoder_half_data_get_raw_body(req_data, body); + return -1; } -int http_message_get_response_raw_body(struct http_message *msg, - struct hstring *body) +int http_message_reset_header_iter(struct http_message *msg) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY) || - NULL == body) { + if (NULL == msg) + { return -1; } - assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); + if (HTTP_MESSAGE_REQ_HEADER == msg->type) + { + struct http_decoder_half_data *req_data = + msg->ref_queue->array[msg->queue_index].req_data; + return http_decoder_half_data_reset_header_iter(req_data); + } + else if (HTTP_MESSAGE_RES_HEADER == msg->type) + { + struct http_decoder_half_data *res_data = + msg->ref_queue->array[msg->queue_index].res_data; + return http_decoder_half_data_reset_header_iter(res_data); + } + return -1; +} + +static int http_msg_get_request_raw_body(struct http_message *msg, + struct hstring *body) +{ + struct http_decoder_half_data *req_data = + msg->ref_queue->array[msg->queue_index].req_data; + return http_decoder_half_data_get_raw_body(req_data, body); +} + +static int http_msg_get_response_raw_body(struct http_message *msg, + struct hstring *body) +{ struct http_decoder_half_data *res_data = msg->ref_queue->array[msg->queue_index].res_data; - return http_decoder_half_data_get_raw_body(res_data, body); } -int http_message_get_request_decompress_body(struct http_message *msg, - struct hstring *body) +int http_message_get_raw_body(struct http_message *msg, + struct hstring *body) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_REQ_BODY) || - NULL == body) { + if (NULL == msg || NULL == body) + { return -1; } - assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); + if (HTTP_MESSAGE_REQ_BODY == msg->type) + { + return http_msg_get_request_raw_body(msg, body); + } + else if (HTTP_MESSAGE_RES_BODY == msg->type) + { + return http_msg_get_response_raw_body(msg, body); + } + + return -1; +} + +int http_msg_get_request_decompress_body(struct http_message *msg, + struct hstring *body) +{ struct http_decoder_half_data *req_data = msg->ref_queue->array[msg->queue_index].req_data; - return http_decoder_half_data_get_decompress_body(req_data, body); } -int http_message_get_response_decompress_body(struct http_message *msg, - struct hstring *body) +int http_msg_get_response_decompress_body(struct http_message *msg, + struct hstring *body) +{ + struct http_decoder_half_data *res_data = + msg->ref_queue->array[msg->queue_index].res_data; + return http_decoder_half_data_get_decompress_body(res_data, body); +} + +int http_message_get_decompress_body(struct http_message *msg, + struct hstring *body) { - if (NULL == msg || (msg->type != HTTP_MESSAGE_RES_BODY) || - NULL == body) { + if (NULL == msg || NULL == body) + { return -1; } assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); - struct http_decoder_half_data *res_data = - msg->ref_queue->array[msg->queue_index].res_data; + if (HTTP_MESSAGE_REQ_BODY == msg->type) + { + return http_msg_get_request_decompress_body(msg, body); + } + else if (HTTP_MESSAGE_RES_BODY == msg->type) + { + return http_msg_get_response_decompress_body(msg, body); + } - return http_decoder_half_data_get_decompress_body(res_data, body); + return -1; } int http_message_get_url(struct http_message *msg, struct hstring *url) diff --git a/src/http_decoder_half.c b/src/http_decoder_half.c index 5772a54..d5f8336 100644 --- a/src/http_decoder_half.c +++ b/src/http_decoder_half.c @@ -1,7 +1,7 @@ /* ********************************************************************************************** * File: http_decoder_half.c -* Description: +* Description: * Authors: Liu WenTan <[email protected]> * Date: 2024-01-10 * Copyright: (c) Since 2022 Geedge Networks, Ltd. All rights reserved. @@ -23,7 +23,8 @@ #include "http_decoder_table.h" #include "http_content_decompress.h" -struct http_decoder_half_data { +struct http_decoder_half_data +{ struct http_decoder_table *table; int major_version; @@ -40,13 +41,14 @@ struct http_decoder_half_data { long long transaction_index; }; -struct http_decoder_half { +struct http_decoder_half +{ llhttp_t parser; llhttp_settings_t settings; enum llhttp_errno error; int decompress_switch; - + enum http_event event; http_event_cb *http_ev_cb; struct http_event_context *http_ev_ctx; @@ -82,25 +84,29 @@ http_decoder_half_data_decompress(struct http_decoder_half_data *data) { assert(data); - if (data->content_encoding == HTTP_CONTENT_ENCODING_NONE) { + if (data->content_encoding == HTTP_CONTENT_ENCODING_NONE) + { return; } struct hstring raw_body = {0}; http_decoder_table_get_body(data->table, &raw_body); - if (raw_body.str == NULL || raw_body.str_len == 0) { + if (raw_body.str == NULL || raw_body.str_len == 0) + { return; } - if (NULL == data->decompress) { + if (NULL == data->decompress) + { data->decompress = http_content_decompress_create(data->content_encoding); } - + assert(data->decompress); if (http_content_decompress_write(data->decompress, raw_body.str, raw_body.str_len, &data->ref_decompress_body, - &data->decompress_body_len) == -1) { + &data->decompress_body_len) == -1) + { // log error http_content_decompress_destroy(data->decompress); data->decompress = NULL; @@ -116,9 +122,12 @@ static int on_message_begin(llhttp_t *http) container_of(http, struct http_decoder_half, parser); assert(half); - if (half->parser.type == HTTP_REQUEST) { + if (half->parser.type == HTTP_REQUEST) + { half->event = HTTP_EVENT_REQ_INIT; - } else { + } + else + { half->event = HTTP_EVENT_RES_INIT; } @@ -140,33 +149,45 @@ static int on_message_complete(llhttp_t *http) container_of(http, struct http_decoder_half, parser); assert(half); - if (half->parser.type == HTTP_REQUEST) { - if (half->event == HTTP_EVENT_REQ_BODY_DATA) { + if (half->parser.type == HTTP_REQUEST) + { + if (half->event == HTTP_EVENT_REQ_BODY_DATA) + { half->event = HTTP_EVENT_REQ_BODY_END; - if (half->http_ev_cb != NULL) { + if (half->http_ev_cb != NULL) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } - } else { - if (half->event == HTTP_EVENT_RES_BODY_DATA) { + } + else + { + if (half->event == HTTP_EVENT_RES_BODY_DATA) + { half->event = HTTP_EVENT_RES_BODY_END; - if (half->http_ev_cb != NULL) { + if (half->http_ev_cb != NULL) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } } - //trigger req_end/res_end - if (half->parser.type == HTTP_REQUEST) { + // trigger req_end/res_end + if (half->parser.type == HTTP_REQUEST) + { half->event = HTTP_EVENT_REQ_END; - if (half->http_ev_cb != NULL) { + if (half->http_ev_cb != NULL) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } - } else { + } + else + { half->event = HTTP_EVENT_RES_END; - if (half->http_ev_cb != NULL) { + if (half->http_ev_cb != NULL) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } @@ -204,7 +225,8 @@ static int on_method_complete(llhttp_t *http) assert(half); if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_METHOD) == - STRING_STATE_REFER) { + STRING_STATE_REFER) + { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_METHOD); } @@ -232,8 +254,8 @@ static void http_decoder_cached_portion_url(struct http_decoder_half *half, cons struct http_decoder_half_data *ref_data = half->ref_data; int uri_skip_len = 0; - if ((uri_result->str_len) > 7 && (strncasecmp("http://", uri_result->str, 7) == 0)) // absolute URI - { + if ((uri_result->str_len) > 7 && (strncasecmp("http://", uri_result->str, 7) == 0)) // absolute URI + { uri_skip_len = strlen("http://"); ref_data->joint_url_complete = 1; } @@ -244,7 +266,7 @@ static void http_decoder_cached_portion_url(struct http_decoder_half *half, cons ref_data->joint_url.str_len = uri_result->str_len - uri_skip_len; ref_data->joint_url.str = MEMPOOL_CALLOC(half->http_ev_ctx->ref_mempool, char, ref_data->joint_url.str_len); - memcpy(ref_data->joint_url.str, uri_result->str+uri_skip_len, ref_data->joint_url.str_len); + memcpy(ref_data->joint_url.str, uri_result->str + uri_skip_len, ref_data->joint_url.str_len); } /* Information-only callbacks, return value is ignored */ @@ -257,7 +279,8 @@ static int on_uri_complete(llhttp_t *http) assert(half); if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_URI) == - STRING_STATE_REFER) { + STRING_STATE_REFER) + { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_URI); } @@ -295,7 +318,8 @@ static int on_version_complete(llhttp_t *http) assert(half); if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_VERSION) == - STRING_STATE_REFER) { + STRING_STATE_REFER) + { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_VERSION); } @@ -304,13 +328,15 @@ static int on_version_complete(llhttp_t *http) half->ref_data->major_version = llhttp_get_http_major(&half->parser); half->ref_data->minor_version = llhttp_get_http_minor(&half->parser); - if (half->parser.type == HTTP_REQUEST) { + if (half->parser.type == HTTP_REQUEST) + { half->event = HTTP_EVENT_REQ_LINE; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } - + return 0; } @@ -338,16 +364,19 @@ static int on_status_complete(llhttp_t *http) assert(half); if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_STATUS) == - STRING_STATE_REFER) { + 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); - if (half->parser.type == HTTP_RESPONSE) { + if (half->parser.type == HTTP_RESPONSE) + { half->event = HTTP_EVENT_RES_LINE; - if (half->http_ev_cb != NULL) { + if (half->http_ev_cb != NULL) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } @@ -407,22 +436,26 @@ static int on_header_value_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_HDRKEY) == - STRING_STATE_CACHE) { + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_HDRKEY) == + STRING_STATE_CACHE) + { http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_HDRKEY); } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_HDRVAL); - if (half->ref_data->content_encoding == HTTP_CONTENT_ENCODING_NONE) { + if (half->ref_data->content_encoding == HTTP_CONTENT_ENCODING_NONE) + { struct http_header http_hdr = {0}; struct hstring key = {.str = (char *)"Content-Encoding", .str_len = 16}; if (http_decoder_table_get_header(half->ref_data->table, &key, - &http_hdr, 1) == 1) { + &http_hdr) == 0) + { char encoding_str[MAX_ENCODING_STR_LEN + 1] = {0}; size_t str_len = http_hdr.val.str_len; - if (str_len > MAX_ENCODING_STR_LEN) { + if (str_len > MAX_ENCODING_STR_LEN) + { str_len = MAX_ENCODING_STR_LEN; } memcpy(encoding_str, http_hdr.val.str, str_len); @@ -474,16 +507,20 @@ static int on_headers_complete(llhttp_t *http) http_decoder_table_set_header_complete(half->ref_data->table); - if (half->parser.type == HTTP_REQUEST) { + if (half->parser.type == HTTP_REQUEST) + { half->event = HTTP_EVENT_REQ_HDR_END; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } - if (half->parser.type == HTTP_RESPONSE) { + if (half->parser.type == HTTP_RESPONSE) + { half->event = HTTP_EVENT_RES_HDR_END; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } @@ -501,27 +538,36 @@ static int on_body(llhttp_t *http, const char *at, size_t length) assert(half); // trigger body_begin event - if (half->parser.type == HTTP_REQUEST) { - if (half->event == HTTP_EVENT_REQ_HDR_END) { + if (half->parser.type == HTTP_REQUEST) + { + if (half->event == HTTP_EVENT_REQ_HDR_END) + { half->event = HTTP_EVENT_REQ_BODY_BEGIN; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } - } else { - if (half->event == HTTP_EVENT_RES_HDR_END) { + } + else + { + if (half->event == HTTP_EVENT_RES_HDR_END) + { half->event = HTTP_EVENT_RES_BODY_BEGIN; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } } - if (half->ref_data != NULL) { + if (half->ref_data != NULL) + { if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) == - STRING_STATE_COMMIT) { + STRING_STATE_COMMIT) + { http_decoder_table_reset(half->ref_data->table, HTTP_ITEM_BODY); } @@ -531,18 +577,24 @@ static int on_body(llhttp_t *http, const char *at, size_t length) } if (1 == half->decompress_switch && - half->ref_data->content_encoding != HTTP_CONTENT_ENCODING_NONE) { + half->ref_data->content_encoding != HTTP_CONTENT_ENCODING_NONE) + { http_decoder_half_data_decompress(half->ref_data); } - if (half->parser.type == HTTP_REQUEST) { + if (half->parser.type == HTTP_REQUEST) + { half->event = HTTP_EVENT_REQ_BODY_DATA; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } - } else { + } + else + { half->event = HTTP_EVENT_RES_BODY_DATA; - if (half->http_ev_cb) { + if (half->http_ev_cb) + { half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); } } @@ -554,13 +606,14 @@ static void http_decoder_half_init(struct http_decoder_half *half, http_event_cb *http_ev_cb, int type) { - if (NULL == half) { + if (NULL == half) + { return; } llhttp_settings_init(&half->settings); llhttp_init(&half->parser, type, &half->settings); - + half->settings.on_message_begin = on_message_begin; half->settings.on_message_complete = on_message_complete; half->settings.on_reset = on_reset; @@ -612,11 +665,13 @@ http_decoder_half_new(nmx_pool_t *mempool, http_event_cb *ev_cb, int http_type, void http_decoder_half_free(nmx_pool_t *mempool, struct http_decoder_half *half) { - if (NULL == half) { + if (NULL == half) + { return; } - if (half->http_ev_ctx != NULL) { + if (half->http_ev_ctx != NULL) + { MEMPOOL_FREE(mempool, half->http_ev_ctx); half->http_ev_ctx = NULL; } @@ -630,7 +685,8 @@ void http_decoder_half_reinit(struct http_decoder_half *half, int topic_id, { assert(half != NULL); - if (half->ref_data != NULL) { + if (half->ref_data != NULL) + { http_decoder_table_reinit(half->ref_data->table); } @@ -642,7 +698,8 @@ void http_decoder_half_reinit(struct http_decoder_half *half, int topic_id, static void publish_message_for_parsed_header(struct http_decoder_half *half) { - if (0 == http_decoder_table_has_parsed_header(half->ref_data->table)) { + if (0 == http_decoder_table_has_parsed_header(half->ref_data->table)) + { return; } @@ -651,14 +708,17 @@ static void publish_message_for_parsed_header(struct http_decoder_half *half) size_t queue_idx = 0; struct http_decoder_result_queue *queue = half->http_ev_ctx->ref_queue; - if (half->parser.type == HTTP_REQUEST) { + if (half->parser.type == HTTP_REQUEST) + { queue_idx = http_decoder_result_queue_req_index(queue); msg = http_message_new(HTTP_MESSAGE_REQ_HEADER, queue, queue_idx); session_mq_publish_message(half->http_ev_ctx->ref_session, half->http_ev_ctx->topic_id, msg); - } else { + } + else + { // http response queue_idx = http_decoder_result_queue_res_index(queue); @@ -672,7 +732,8 @@ static void publish_message_for_parsed_header(struct http_decoder_half *half) int http_decoder_half_parse(struct http_decoder_half *half, const char *data, size_t data_len) { - if (NULL == half || NULL == data || 0 == data_len) { + if (NULL == half || NULL == data || 0 == data_len) + { return -1; } @@ -682,7 +743,8 @@ int http_decoder_half_parse(struct http_decoder_half *half, const char *data, uint8_t type = 0; struct http_decoder_half_data *half_data = NULL; - switch (half->error) { + switch (half->error) + { case HPE_OK: break; case HPE_PAUSED: @@ -699,37 +761,42 @@ int http_decoder_half_parse(struct http_decoder_half *half, const char *data, break; } - if (ret < 0) { + if (ret < 0) + { // fprintf(stdout, // "llhttp_execute parse error: %s err_reason:%s\n", // llhttp_errno_name(half->error), half->parser.reason); return half->error; } - if (half->ref_data != NULL) { - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_URI) - == STRING_STATE_REFER) { + if (half->ref_data != NULL) + { + 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); } - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_STATUS) - == STRING_STATE_REFER) { + 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); } - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_METHOD) - == STRING_STATE_REFER) { + 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); } - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_VERSION) - == STRING_STATE_REFER) { + 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); } - if (http_decoder_table_header_complete(half->ref_data->table)) { + if (http_decoder_table_header_complete(half->ref_data->table)) + { http_decoder_table_reset_header_complete(half->ref_data->table); - } else { + } + else + { publish_message_for_parsed_header(half); } @@ -738,31 +805,32 @@ int http_decoder_half_parse(struct http_decoder_half *half, const char *data, enum string_state hdr_val_state = http_decoder_table_state(half->ref_data->table, HTTP_ITEM_HDRVAL); - /* Truncated in http header key + /* Truncated in http header key For example http header k-v => User-Agent: Chrome - case1: - packet1: User- hdr_key_state == STRING_STATE_REFER + case1: + packet1: User- hdr_key_state == STRING_STATE_REFER packet2: Agent: Chrome case2: packet1: User-Agent: hdr_key_state == STRING_STATE_COMMIT - hdr_val_state == STRING_STATE_INIT + hdr_val_state == STRING_STATE_INIT packet2: Chrome - */ + */ if (hdr_key_state == STRING_STATE_REFER || - (hdr_key_state == STRING_STATE_COMMIT && hdr_val_state == STRING_STATE_INIT)) { + (hdr_key_state == STRING_STATE_COMMIT && hdr_val_state == STRING_STATE_INIT)) + { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_HDRKEY); } - /* Truncated in http header value + /* Truncated in http header value For example http header k-v => User-Agent: Chrome packet1: User-Agent: Ch hdr_key_state == STRING_STATE_COMMIT - hdr_val_state == STRING_STATE_REFER + hdr_val_state == STRING_STATE_REFER packet2: rome */ - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_HDRVAL) - == STRING_STATE_REFER) { + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_HDRVAL) == STRING_STATE_REFER) + { /* Header key should have been committed If it's not cached, cache it for next packet to use */ @@ -770,8 +838,8 @@ int http_decoder_half_parse(struct http_decoder_half *half, const char *data, http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_HDRVAL); } - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) - == STRING_STATE_REFER) { + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) == STRING_STATE_REFER) + { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_BODY); } } @@ -781,7 +849,8 @@ int http_decoder_half_parse(struct http_decoder_half *half, const char *data, long long http_decoder_half_trans_count(struct http_decoder_half *half) { - if (NULL == half) { + if (NULL == half) + { return 0; } @@ -815,23 +884,26 @@ http_decoder_half_data_new(nmx_pool_t *mempool) void http_decoder_half_data_free(nmx_pool_t *mempool, struct http_decoder_half_data *data) { - if (NULL == data) { + if (NULL == data) + { return; } - if (data->table != NULL) { + if (data->table != NULL) + { http_decoder_table_free(data->table); data->table = NULL; } - if (data->decompress != NULL) { + if (data->decompress != NULL) + { http_content_decompress_destroy(data->decompress); data->decompress = NULL; } - if(data->joint_url.str) + if (data->joint_url.str) { - MEMPOOL_FREE(mempool, data->joint_url.str); + MEMPOOL_FREE(mempool, data->joint_url.str); data->joint_url.str = NULL; data->joint_url_complete = 0; } @@ -842,7 +914,8 @@ void http_decoder_half_data_free(nmx_pool_t *mempool, int http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, struct http_request_line *line) { - if (NULL == data || NULL == line) { + if (NULL == data || NULL == line) + { return -1; } @@ -859,7 +932,8 @@ int http_decoder_half_data_get_request_line(struct http_decoder_half_data *data, int http_decoder_half_data_get_response_line(struct http_decoder_half_data *data, struct http_response_line *line) { - if (NULL == data || NULL == line) { + if (NULL == data || NULL == line) + { return -1; } @@ -875,58 +949,66 @@ int http_decoder_half_data_get_response_line(struct http_decoder_half_data *data 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) + struct http_header *hdr_result) { - if (NULL == data || NULL == key || - NULL == hdr_array || 0 == array_size) { + if (NULL == data || NULL == key || NULL == hdr_result) + { return -1; } - - return http_decoder_table_get_header(data->table, key, hdr_array, array_size); + return http_decoder_table_get_header(data->table, key, hdr_result); } int http_decoder_half_data_iter_header(struct http_decoder_half_data *data, struct http_header *header) { - if (NULL == data || NULL == header) { + if (NULL == data || NULL == header) + { return -1; } - return http_decoder_table_iter_header(data->table, header); } +int http_decoder_half_data_reset_header_iter(struct http_decoder_half_data *req_data) +{ + if (NULL == req_data) + { + return -1; + } + return http_decoder_table_reset_header_iter(req_data->table); +} + int http_decoder_half_data_has_parsed_header(struct http_decoder_half_data *data) { - if (NULL == data) { + if (NULL == data) + { return 0; } - return http_decoder_table_has_parsed_header(data->table); } int http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, struct hstring *body) { - if (NULL == data || NULL == body) { + if (NULL == data || NULL == body) + { return -1; } - 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) { - if (NULL == data || NULL == body) { + if (NULL == data || NULL == body) + { return -1; } - if (HTTP_CONTENT_ENCODING_NONE == data->content_encoding) { + if (HTTP_CONTENT_ENCODING_NONE == data->content_encoding) + { return http_decoder_table_get_body(data->table, body); } - body->str = data->ref_decompress_body; body->str_len = data->decompress_body_len; return 0; @@ -934,7 +1016,8 @@ int http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *da void http_decoder_half_data_dump(struct http_decoder_half *half) { - if (NULL == half || NULL == half->ref_data) { + if (NULL == half || NULL == half->ref_data) + { return; } @@ -976,7 +1059,7 @@ static void using_session_addr_as_host(struct session *ref_session, void http_decoder_join_url(struct http_decoder_half_data *hfdata, nmx_pool_t *mempool, const struct http_header *host_hdr) { int append_slash_len = 0; - if('/' != hfdata->joint_url.str[0]) + if ('/' != hfdata->joint_url.str[0]) { append_slash_len = 1; } @@ -986,7 +1069,7 @@ void http_decoder_join_url(struct http_decoder_half_data *hfdata, nmx_pool_t *me char *ptr = url_cache_str; memcpy(ptr, host_hdr->val.str, host_hdr->val.str_len); ptr += host_hdr->val.str_len; - if(append_slash_len) + if (append_slash_len) { *ptr = '/'; ptr++; @@ -1030,8 +1113,8 @@ void http_decoder_get_host_feed_url(struct http_decoder_half *half) } int host_header_cnt = http_decoder_half_data_get_header(half->ref_data, &host_key, - &host_result, 1); - if (host_header_cnt <= 0) + &host_result); + if (host_header_cnt < 0) { return; } diff --git a/src/http_decoder_half.h b/src/http_decoder_half.h index ebe7e00..a2ff5f8 100644 --- a/src/http_decoder_half.h +++ b/src/http_decoder_half.h @@ -87,12 +87,11 @@ 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 *hdr_array, - size_t array_size); + struct hstring *key, struct http_header *hdr_res); int http_decoder_half_data_iter_header(struct http_decoder_half_data *data, struct http_header *header); - +int http_decoder_half_data_reset_header_iter(struct http_decoder_half_data *req_data); int http_decoder_half_data_has_parsed_header(struct http_decoder_half_data *data); int http_decoder_half_data_get_raw_body(struct http_decoder_half_data *data, diff --git a/src/http_decoder_table.c b/src/http_decoder_table.c index ede7989..e999bd7 100644 --- a/src/http_decoder_table.c +++ b/src/http_decoder_table.c @@ -450,14 +450,13 @@ int http_decoder_table_get_body(struct http_decoder_table *table, struct hstring } int http_decoder_table_get_header(struct http_decoder_table *table, struct hstring *key, - struct http_header *hdr_array, size_t array_size) + struct http_header *hdr_result) { - if (NULL == table || NULL == key->str || 0 == key->str_len) { - return 0; + if (NULL == table || NULL == key->str || 0 == key->str_len || NULL == hdr_result) { + return -1; } - int header_cnt = 0; - for (size_t i = 0; i < table->header_cnt && header_cnt < array_size; i++) { + for (size_t i = 0; i < table->header_cnt; i++) { struct http_decoder_header *tmp_header = &table->headers[i]; if (tmp_header->key.commit.str_len != key->str_len) { continue; @@ -470,14 +469,14 @@ int http_decoder_table_get_header(struct http_decoder_table *table, struct hstri if (tmp_key.str_len == key->str_len && (0 == strncasecmp(tmp_key.str, key->str, key->str_len))) { - http_decoder_string_get(&tmp_header->key, &hdr_array[header_cnt].key); - http_decoder_string_get(&tmp_header->val, &hdr_array[header_cnt].val); - header_cnt++; + http_decoder_string_get(&tmp_header->key, &hdr_result->key); + http_decoder_string_get(&tmp_header->val, &hdr_result->val); + return 0; } } } - return header_cnt; + return -1; } int http_decoder_table_iter_header(struct http_decoder_table *table, @@ -513,6 +512,12 @@ int http_decoder_table_iter_header(struct http_decoder_table *table, return 0; } +int http_decoder_table_reset_header_iter(struct http_decoder_table *table) +{ + table->header_iter = 0; + return 0; +} + int http_decoder_table_has_parsed_header(struct http_decoder_table *table) { if (NULL == table || (table->header_iter == table->header_index)) { diff --git a/src/http_decoder_table.h b/src/http_decoder_table.h index fe443bb..906a28d 100644 --- a/src/http_decoder_table.h +++ b/src/http_decoder_table.h @@ -65,12 +65,11 @@ int http_decoder_table_get_body(struct http_decoder_table *table, struct hstring int http_decoder_table_get_header(struct http_decoder_table *table, struct hstring *key, - struct http_header *hdr_array, - size_t array_size); + struct http_header *hdr_res); int http_decoder_table_iter_header(struct http_decoder_table *table, struct http_header *hdr); - +int http_decoder_table_reset_header_iter(struct http_decoder_table *table); /** * @brief Is there a parsed header * |
