summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorliuwentan <[email protected]>2024-01-02 08:40:06 +0000
committerliuwentan <[email protected]>2024-01-02 08:40:06 +0000
commit4ed6aa6c98cfc1f4d9a61be38c226044a8f7ca57 (patch)
treec285ee8f4c9421e6e601ea045ddac6587722ecd7
parentc19e0ea5d9a59aaabf580568a1fb421eb4421051 (diff)
[PATCH] simplify http_decoder external API
-rw-r--r--src/http_decoder/http_decoder.c39
-rw-r--r--src/http_decoder/http_decoder.h2
-rw-r--r--src/http_decoder/http_decoder_half.c27
-rw-r--r--src/http_decoder/http_decoder_half.h3
-rw-r--r--test/http_decoder/CMakeLists.txt12
-rw-r--r--test/http_decoder/http_decoder_gtest.cpp24
-rw-r--r--test/http_decoder/http_pcap/http_req_1byte_sliding_window.pcap (renamed from test/http_decoder/http_pcap/http_req_window_1.pcap)bin31724 -> 31724 bytes
-rw-r--r--test/http_decoder/http_pcap/http_res_1byte_sliding_window.pcap (renamed from test/http_decoder/http_pcap/http_res_window_1.pcap)bin55449 -> 55449 bytes
-rw-r--r--test/http_decoder/test_result_json/http_req_1byte_sliding_window.json (renamed from test/http_decoder/test_result_json/http_req_window_1.json)0
-rw-r--r--test/http_decoder/test_result_json/http_res_1byte_sliding_window.json (renamed from test/http_decoder/test_result_json/http_res_window_1.json)0
10 files changed, 43 insertions, 64 deletions
diff --git a/src/http_decoder/http_decoder.c b/src/http_decoder/http_decoder.c
index f29a6cd..32cfc46 100644
--- a/src/http_decoder/http_decoder.c
+++ b/src/http_decoder/http_decoder.c
@@ -22,10 +22,7 @@
#define HTTP_IDENTIFY_LEN 16
#define HTTP_DECODER_RESULT_QUEUE_SIZE 16
-
-#define HD_IS_CACHE_LINE 1
#define HD_IS_CACHE_BODY 1
-#define HD_IS_CACHE_HEADER 1
const char *http_decoder_topic = "HTTP_DECODER_MESSAGE";
@@ -185,7 +182,7 @@ static void http_event_handler(enum http_event event,
break;
case HTTP_EVENT_REQ_HDR_END:
msg = CALLOC(struct http_message, 1);
- msg->type = HTTP_MESSAGE_REQ_HEADER_END;
+ msg->type = HTTP_MESSAGE_REQ_HEADER;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
@@ -217,7 +214,7 @@ static void http_event_handler(enum http_event event,
break;
case HTTP_EVENT_RES_HDR_END:
msg = CALLOC(struct http_message, 1);
- msg->type = HTTP_MESSAGE_RES_HEADER_END;
+ msg->type = HTTP_MESSAGE_RES_HEADER;
msg->data = *data;
session_mq_publish_message(ctx->ref_session, ctx->topic_id, msg);
break;
@@ -241,14 +238,13 @@ static void http_event_handler(enum http_event event,
}
}
-static struct http_decoder *http_decoder_new(http_event_cb *ev_cb, int is_cache_line,
- int is_cache_header, int is_cache_body)
+static struct http_decoder *http_decoder_new(http_event_cb *ev_cb, int is_cache_body)
{
struct http_decoder *decoder = CALLOC(struct http_decoder, 1);
assert(decoder);
- decoder->c2s_half = http_decoder_half_new(ev_cb, is_cache_line, is_cache_header, is_cache_body);
- decoder->s2c_half = http_decoder_half_new(ev_cb, is_cache_line, is_cache_header, is_cache_body);
+ decoder->c2s_half = http_decoder_half_new(ev_cb, is_cache_body);
+ decoder->s2c_half = http_decoder_half_new(ev_cb, is_cache_body);
return decoder;
}
@@ -376,7 +372,7 @@ int http_decoder_entry(struct session *sess, int events, const struct packet *pk
session_set_ex_data(sess, ctx->ex_data_idx, queue);
}
- if (0 == payload_len) {
+ if (0 == payload_len || NULL == queue) {
return 0;
}
@@ -385,15 +381,8 @@ int http_decoder_entry(struct session *sess, int events, const struct packet *pk
return -1;
}
- // printf("\n-------------------------------------------\n");
- // for (size_t i = 0; i < payload_len; i++) {
- // printf(" %x", payload[i]);
- // }
- // printf("\n-------------------------------------------\n");
-
if (NULL == ctx->decoder) {
- ctx->decoder = http_decoder_new(http_event_handler, HD_IS_CACHE_LINE,
- HD_IS_CACHE_HEADER, 0);
+ ctx->decoder = http_decoder_new(http_event_handler, 0);
}
struct http_decoder_half *cur_half = NULL;
@@ -516,8 +505,7 @@ int http_message_get_response_line(struct http_message *msg, struct http_respons
int http_message_get_request_header(struct http_message *msg, struct hstring *key,
struct http_header *header_array, size_t array_size)
{
- if (NULL == msg ||
- (msg->type != HTTP_MESSAGE_REQ_HEADER && msg->type != HTTP_MESSAGE_REQ_HEADER_END) ||
+ if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER ||
NULL == key || NULL == header_array || 0 == array_size) {
return -1;
}
@@ -528,8 +516,7 @@ int http_message_get_request_header(struct http_message *msg, struct hstring *ke
int http_message_get_response_header(struct http_message *msg, struct hstring *key,
struct http_header *header_array, size_t array_size)
{
- if (NULL == msg ||
- (msg->type != HTTP_MESSAGE_RES_HEADER && msg->type != HTTP_MESSAGE_RES_HEADER_END) ||
+ if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER ||
NULL == key || NULL == header_array || 0 == array_size) {
return -1;
}
@@ -539,9 +526,7 @@ int http_message_get_response_header(struct http_message *msg, struct hstring *k
int http_message_request_header_next(struct http_message *msg, struct http_header *header)
{
- if (NULL == msg ||
- (msg->type != HTTP_MESSAGE_REQ_HEADER && msg->type != HTTP_MESSAGE_REQ_HEADER_END) ||
- NULL == header) {
+ if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER || NULL == header) {
return -1;
}
@@ -550,9 +535,7 @@ int http_message_request_header_next(struct http_message *msg, struct http_heade
int http_message_response_header_next(struct http_message *msg, struct http_header *header)
{
- if (NULL == msg ||
- (msg->type != HTTP_MESSAGE_RES_HEADER && msg->type != HTTP_MESSAGE_RES_HEADER_END) ||
- NULL == header) {
+ if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER || NULL == header) {
return -1;
}
diff --git a/src/http_decoder/http_decoder.h b/src/http_decoder/http_decoder.h
index 430ea16..e253bb6 100644
--- a/src/http_decoder/http_decoder.h
+++ b/src/http_decoder/http_decoder.h
@@ -21,11 +21,9 @@ extern "C"
enum http_message_type {
HTTP_MESSAGE_REQ_LINE,
HTTP_MESSAGE_REQ_HEADER,
- HTTP_MESSAGE_REQ_HEADER_END,
HTTP_MESSAGE_REQ_BODY,
HTTP_MESSAGE_RES_LINE,
HTTP_MESSAGE_RES_HEADER,
- HTTP_MESSAGE_RES_HEADER_END,
HTTP_MESSAGE_RES_BODY,
HTTP_MESSAGE_MAX
};
diff --git a/src/http_decoder/http_decoder_half.c b/src/http_decoder/http_decoder_half.c
index e2e25f1..45c936d 100644
--- a/src/http_decoder/http_decoder_half.c
+++ b/src/http_decoder/http_decoder_half.c
@@ -35,8 +35,6 @@ struct http_decoder_half {
llhttp_settings_t settings;
enum llhttp_errno error;
- int is_cache_line;
- int is_cache_header;
int is_cache_body;
struct http_decoder_half_data *ref_data;
@@ -248,8 +246,7 @@ static int on_uri_complete(llhttp_t *http)
assert(half);
if (half->ref_data != NULL) {
- if (half->is_cache_line &&
- http_decoder_table_state(half->ref_data->table, HTTP_ITEM_URI) == STRING_STATE_REFER) {
+ 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);
}
@@ -290,8 +287,7 @@ static int on_version_complete(llhttp_t *http)
assert(half);
if (half->ref_data) {
- if (half->is_cache_line &&
- 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);
}
http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_VERSION);
@@ -334,8 +330,7 @@ static int on_status_complete(llhttp_t *http)
assert(half);
if (half->ref_data != NULL) {
- if (half->is_cache_line &&
- 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);
}
@@ -375,8 +370,7 @@ static int on_header_field_complete(llhttp_t *http)
assert(half);
if (half->ref_data != NULL) {
- if (half->is_cache_header &&
- http_decoder_table_state(half->ref_data->table, HTTP_ITEM_HDRKEY) == STRING_STATE_REFER) {
+ if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_HDRKEY) == STRING_STATE_REFER) {
http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_HDRKEY);
}
@@ -410,9 +404,7 @@ static int on_header_value_complete(llhttp_t *http)
assert(half);
if (half->ref_data != NULL) {
- if (half->is_cache_header &&
- 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) {
http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_HDRVAL);
}
http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_HDRVAL);
@@ -543,7 +535,6 @@ static int on_body(llhttp_t *http, const char *at, size_t length)
}
void http_decoder_half_init(struct http_decoder_half *half, http_event_cb *http_ev_cb,
- int is_cache_line, int is_cache_header,
int is_cache_body)
{
if (NULL == half) {
@@ -585,22 +576,18 @@ void http_decoder_half_init(struct http_decoder_half *half, http_event_cb *http_
half->error = HPE_OK;
half->http_ev_cb = http_ev_cb;
- half->is_cache_line = is_cache_line;
- half->is_cache_header = is_cache_header;
half->is_cache_body = is_cache_body;
half->ref_data = NULL;
}
struct http_decoder_half *
-http_decoder_half_new(http_event_cb *ev_cb, int is_cache_line,
- int is_cache_header, int is_cache_body)
+http_decoder_half_new(http_event_cb *ev_cb, int is_cache_body)
{
struct http_decoder_half *half = CALLOC(struct http_decoder_half, 1);
assert(half);
- http_decoder_half_init(half, ev_cb, is_cache_line,
- is_cache_header, is_cache_body);
+ http_decoder_half_init(half, ev_cb, is_cache_body);
return half;
}
diff --git a/src/http_decoder/http_decoder_half.h b/src/http_decoder/http_decoder_half.h
index b09aa5e..a856929 100644
--- a/src/http_decoder/http_decoder_half.h
+++ b/src/http_decoder/http_decoder_half.h
@@ -50,8 +50,7 @@ struct http_decoder_half_data;
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_line,
- int is_cache_header, int is_cache_body);
+http_decoder_half_new(http_event_cb *event_cb, int is_cache_body);
void http_decoder_half_free(struct http_decoder_half *half);
diff --git a/test/http_decoder/CMakeLists.txt b/test/http_decoder/CMakeLists.txt
index 7465cdc..5221630 100644
--- a/test/http_decoder/CMakeLists.txt
+++ b/test/http_decoder/CMakeLists.txt
@@ -70,11 +70,11 @@ add_test(NAME HTTP_OVER_TLS_TEST COMMAND ${TEST_MAIN} ${CMAKE_CURRENT_SOURCE_DIR
add_test(NAME NON_HTTP_TEST COMMAND ${TEST_MAIN} ${CMAKE_CURRENT_SOURCE_DIR}/test_result_json/non_http.json
-f "find ${CMAKE_CURRENT_SOURCE_DIR}/http_pcap/ -name non_http.pcap|sort -V" WORKING_DIRECTORY ${TEST_RUN_DIR})
-add_test(NAME HTTP_REQ_WINDOW_1_TEST COMMAND ${TEST_MAIN} ${CMAKE_CURRENT_SOURCE_DIR}/test_result_json/http_req_window_1.json
- -f "find ${CMAKE_CURRENT_SOURCE_DIR}/http_pcap/ -name http_req_window_1.pcap|sort -V" WORKING_DIRECTORY ${TEST_RUN_DIR})
+add_test(NAME HTTP_REQ_1BYTE_SLIDING_WINDOW_TEST COMMAND ${TEST_MAIN} ${CMAKE_CURRENT_SOURCE_DIR}/test_result_json/http_req_1byte_sliding_window.json
+ -f "find ${CMAKE_CURRENT_SOURCE_DIR}/http_pcap/ -name http_req_1byte_sliding_window.pcap|sort -V" WORKING_DIRECTORY ${TEST_RUN_DIR})
-add_test(NAME HTTP_RES_WINDOW_1_TEST COMMAND ${TEST_MAIN} ${CMAKE_CURRENT_SOURCE_DIR}/test_result_json/http_res_window_1.json
- -f "find ${CMAKE_CURRENT_SOURCE_DIR}/http_pcap/ -name http_res_window_1.pcap|sort -V" WORKING_DIRECTORY ${TEST_RUN_DIR})
+add_test(NAME HTTP_RES_1BYTE_SLIDING_WINDOW_TEST COMMAND ${TEST_MAIN} ${CMAKE_CURRENT_SOURCE_DIR}/test_result_json/http_res_1byte_sliding_window.json
+ -f "find ${CMAKE_CURRENT_SOURCE_DIR}/http_pcap/ -name http_res_1byte_sliding_window.pcap|sort -V" WORKING_DIRECTORY ${TEST_RUN_DIR})
set_tests_properties(HTTP_GET_SINGLE_TRANS_TEST
HTTP_GET_MULTI_TRANS_TEST
@@ -88,6 +88,6 @@ set_tests_properties(HTTP_GET_SINGLE_TRANS_TEST
HTTP_OVER_PPPOE_TEST
HTTP_OVER_TLS_TEST
NON_HTTP_TEST
- HTTP_REQ_WINDOW_1_TEST
- HTTP_RES_WINDOW_1_TEST
+ HTTP_REQ_1BYTE_SLIDING_WINDOW_TEST
+ HTTP_RES_1BYTE_SLIDING_WINDOW_TEST
PROPERTIES FIXTURES_REQUIRED TestFixture) \ No newline at end of file
diff --git a/test/http_decoder/http_decoder_gtest.cpp b/test/http_decoder/http_decoder_gtest.cpp
index 67bb924..5ba7103 100644
--- a/test/http_decoder/http_decoder_gtest.cpp
+++ b/test/http_decoder/http_decoder_gtest.cpp
@@ -72,6 +72,19 @@ void output_http_header(struct http_header *header)
}
#endif
+void output_http_header(struct http_header *header, int res)
+{
+ char key[MAX_KEY_STR_LEN] = {0};
+
+ snprintf(key, header->key.str_len + 1, "%s", header->key.str);
+ if (0 == res) {
+ printf("<ouput_http_req_header> header:%s\n", key);
+ } else {
+ printf("<ouput_http_res_header> header:%s\n", key);
+ }
+
+}
+
void output_http_body(struct hstring *body, int decompress_flag)
{
int counter = 0;
@@ -150,11 +163,10 @@ static int http_decoder_test_entry(struct session *sess, int topic_id, const voi
struct http_header header = {0};
struct hstring body = {0};
int exdata_idx = 0;
-
enum http_message_type msg_type = http_message_type(msg);
if (msg_type == HTTP_MESSAGE_REQ_LINE ||
- msg_type == HTTP_MESSAGE_REQ_HEADER_END) {
+ msg_type == HTTP_MESSAGE_REQ_HEADER) {
exdata_idx = g_req_exdata_idx;
} else {
exdata_idx = g_res_exdata_idx;
@@ -178,7 +190,7 @@ next:
http_message_get_request_line(msg, &req_line);
req_line_to_json(ctx, &req_line);
break;
- case HTTP_MESSAGE_REQ_HEADER_END:
+ case HTTP_MESSAGE_REQ_HEADER:
while (http_message_request_header_next(msg, &header) > 0) {
http_header_to_json(ctx, &header);
}
@@ -195,7 +207,7 @@ next:
http_message_get_response_line(msg, &res_line);
res_line_to_json(ctx, &res_line);
break;
- case HTTP_MESSAGE_RES_HEADER_END:
+ case HTTP_MESSAGE_RES_HEADER:
while (http_message_response_header_next(msg, &header) > 0) {
http_header_to_json(ctx, &header);
}
@@ -213,7 +225,7 @@ next:
}
char result_name[MAX_KEY_STR_LEN] = {0};
- if (msg_type == HTTP_MESSAGE_REQ_HEADER_END) {
+ if (msg_type == HTTP_MESSAGE_REQ_HEADER) {
sprintf(result_name, "HTTP_DECODER_RESULT_%d", g_result_count);
commit_test_result_json(ctx, result_name);
//printf("req json:%s\n", cJSON_Print(ctx));
@@ -221,7 +233,7 @@ next:
g_result_count++;
}
- if (msg_type == HTTP_MESSAGE_RES_HEADER_END) {
+ if (msg_type == HTTP_MESSAGE_RES_HEADER) {
sprintf(result_name, "HTTP_DECODER_RESULT_%d", g_result_count);
commit_test_result_json(ctx, result_name);
//printf("res json:%s\n", cJSON_Print(ctx));
diff --git a/test/http_decoder/http_pcap/http_req_window_1.pcap b/test/http_decoder/http_pcap/http_req_1byte_sliding_window.pcap
index 632c676..632c676 100644
--- a/test/http_decoder/http_pcap/http_req_window_1.pcap
+++ b/test/http_decoder/http_pcap/http_req_1byte_sliding_window.pcap
Binary files differ
diff --git a/test/http_decoder/http_pcap/http_res_window_1.pcap b/test/http_decoder/http_pcap/http_res_1byte_sliding_window.pcap
index 6d1d6a4..6d1d6a4 100644
--- a/test/http_decoder/http_pcap/http_res_window_1.pcap
+++ b/test/http_decoder/http_pcap/http_res_1byte_sliding_window.pcap
Binary files differ
diff --git a/test/http_decoder/test_result_json/http_req_window_1.json b/test/http_decoder/test_result_json/http_req_1byte_sliding_window.json
index 1612574..1612574 100644
--- a/test/http_decoder/test_result_json/http_req_window_1.json
+++ b/test/http_decoder/test_result_json/http_req_1byte_sliding_window.json
diff --git a/test/http_decoder/test_result_json/http_res_window_1.json b/test/http_decoder/test_result_json/http_res_1byte_sliding_window.json
index 845e1b2..845e1b2 100644
--- a/test/http_decoder/test_result_json/http_res_window_1.json
+++ b/test/http_decoder/test_result_json/http_res_1byte_sliding_window.json