diff options
Diffstat (limited to 'src/http_decoder.cpp')
| -rw-r--r-- | src/http_decoder.cpp | 93 |
1 files changed, 73 insertions, 20 deletions
diff --git a/src/http_decoder.cpp b/src/http_decoder.cpp index 3960869..663d730 100644 --- a/src/http_decoder.cpp +++ b/src/http_decoder.cpp @@ -17,24 +17,40 @@ struct http_message *http_message_new(enum http_message_type type, struct http_d } struct http_message *http_body_message_new(enum http_message_type type, struct http_decoder_result_queue *queue, - int queue_index, uint8_t flow_type, hstring *raw_payload) + int queue_index, uint8_t flow_type, hstring *raw_payload, hstring *decompress_payload) { struct http_message *msg = CALLOC(struct http_message, 1); msg->type = type; msg->ref_queue = queue; msg->queue_index = queue_index; msg->flow_type = flow_type; - msg->raw_payload.iov_base = raw_payload->iov_base; - msg->raw_payload.iov_len = raw_payload->iov_len; + if (raw_payload) + { + msg->raw_payload.iov_base = raw_payload->iov_base; + msg->raw_payload.iov_len = raw_payload->iov_len; + } + if (decompress_payload) + { + msg->decompress_payload.iov_base = decompress_payload->iov_base; + msg->decompress_payload.iov_len = decompress_payload->iov_len; + } return msg; } -static void http_message_decompress_free(struct http_message *msg) +static void http_message_decompress_buffer_free(struct http_message *msg) { - if((msg->type == HTTP_MESSAGE_REQ_BODY) - || (msg->type == HTTP_MESSAGE_RES_BODY)) + struct http_decoder_half_data *ref_data = NULL; + if (HTTP_MESSAGE_REQ_BODY_START == msg->type || HTTP_MESSAGE_REQ_BODY == msg->type || HTTP_MESSAGE_REQ_BODY_END == msg->type) { - // todo, for tcp reorder, maybe receive many many tcp segment + ref_data = msg->ref_queue->array[msg->queue_index].req_data; + } + else if (HTTP_MESSAGE_RES_BODY_START == msg->type || HTTP_MESSAGE_RES_BODY == msg->type || HTTP_MESSAGE_RES_BODY_END == msg->type) + { + ref_data = msg->ref_queue->array[msg->queue_index].res_data; + } + if (ref_data != NULL && msg->decompress_payload.iov_base != NULL) + { + http_half_decompress_buffer_free(ref_data, &msg->decompress_payload); } } @@ -42,7 +58,7 @@ static void http_message_free(void *http_msg, void *cb_arg) { if (http_msg) { - http_message_decompress_free((struct http_message *)http_msg); + http_message_decompress_buffer_free((struct http_message *)http_msg); FREE(http_msg); } } @@ -153,11 +169,17 @@ static void http_event_handler(enum http_event event, struct http_decoder_half_d case HTTP_EVENT_REQ_BODY_DATA: { hstring raw_body = {}; + hstring decompress_body = {}; http_decoder_half_data_get_raw_body(half_data, &raw_body); - msg = http_body_message_new(HTTP_MESSAGE_REQ_BODY, queue, queue_idx, HTTP_REQUEST, &raw_body); + http_half_get_lastest_decompress_buffer(half_data, &decompress_body); + msg = http_body_message_new(HTTP_MESSAGE_REQ_BODY, queue, queue_idx, HTTP_REQUEST, &raw_body, &decompress_body); session_mq_publish_message(ev_ctx->ref_session, exdata->pub_topic_id, msg); + if(decompress_body.iov_base != NULL){ + http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_ZIP_BYTES, raw_body.iov_len); + http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_UNZIP_BYTES, decompress_body.iov_len); + } } - break; + break; case HTTP_EVENT_REQ_BODY_END: msg = http_message_new(HTTP_MESSAGE_REQ_BODY_END, queue, queue_idx, HTTP_REQUEST); session_mq_publish_message(ev_ctx->ref_session, exdata->pub_topic_id, msg); @@ -279,10 +301,16 @@ static void http_event_handler(enum http_event event, struct http_decoder_half_d { hstring raw_body = {}; http_decoder_half_data_get_raw_body(half_data, &raw_body); - msg = http_body_message_new(HTTP_MESSAGE_RES_BODY, queue, queue_idx, HTTP_RESPONSE, &raw_body); + hstring decompress_body = {}; + http_half_get_lastest_decompress_buffer(half_data, &decompress_body); + msg = http_body_message_new(HTTP_MESSAGE_RES_BODY, queue, queue_idx, HTTP_RESPONSE, &raw_body, &decompress_body); session_mq_publish_message(ev_ctx->ref_session, exdata->pub_topic_id, msg); + if(decompress_body.iov_base != NULL){ + http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_ZIP_BYTES, raw_body.iov_len); + http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_UNZIP_BYTES, decompress_body.iov_len); + } } - break; + break; case HTTP_EVENT_RES_BODY_END: msg = http_message_new(HTTP_MESSAGE_RES_BODY_END, queue, queue_idx, HTTP_RESPONSE); session_mq_publish_message(ev_ctx->ref_session, exdata->pub_topic_id, msg); @@ -535,6 +563,7 @@ static int http_msg_response_header_next(const struct http_message *msg, struct return http_decoder_half_data_iter_header((struct http_decoder_half_data *)res_data, hdr); } +#if 0 static int http_msg_get_request_raw_body(const struct http_message *msg, hstring *body) { const struct http_decoder_half_data *req_data = @@ -562,6 +591,7 @@ static int http_msg_get_response_decompress_body(const struct http_message *msg, msg->ref_queue->array[msg->queue_index].res_data; return http_decoder_half_data_get_decompress_body(res_data, body); } +#endif static struct http_decoder_exdata *httpd_session_exdata_new(struct session *sess, struct http_decoder_env *httpd_env, long long req_start_seq, long long res_start_seq) @@ -619,10 +649,7 @@ extern "C" { http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_ASYMMETRY_SESSION_S2C, 1); } - else - { - http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_SESSION_FREE, 1); - } + http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_SESSION_FREE, 1); } static void http_decoder_execute(struct session *sess, struct http_decoder_env *httpd_env, http_decoder_exdata *exdata, const char *payload, uint16_t payload_len) @@ -650,7 +677,7 @@ extern "C" http_decoder_stat_update(&httpd_env->hd_stat, thread_id, HTTPD_STAT_TCP_SEG_S2C, 1); } - http_decoder_half_reinit(cur_half, exdata->queue, exdata->mempool, sess); + http_decoder_half_reinit(cur_half, exdata->queue, exdata->mempool, sess); int ret = http_decoder_half_parse(httpd_env->hd_cfg.proxy_enable, cur_half, payload, payload_len); if (ret < 0) { @@ -1025,7 +1052,7 @@ extern "C" } assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); - if(msg->raw_payload.iov_base != NULL && msg->raw_payload.iov_len != 0) + if (msg->raw_payload.iov_base != NULL && msg->raw_payload.iov_len != 0) { body->iov_base = msg->raw_payload.iov_base; body->iov_len = msg->raw_payload.iov_len; @@ -1042,6 +1069,8 @@ extern "C" void http_message_decompress_body_get0(const struct http_message *msg, hstring *decompress_body) { + enum http_content_encoding ecode = HTTP_CONTENT_ENCODING_NONE; + struct http_decoder_half_data *ref_data = NULL; int ret = -1; if (unlikely(NULL == msg)) { @@ -1049,12 +1078,36 @@ extern "C" } assert(msg->ref_queue); assert(msg->queue_index < HD_RESULT_QUEUE_LEN); - if(msg->decompress_payload.iov_base !=NULL && msg->decompress_payload.iov_len != 0){ + if (msg->decompress_payload.iov_base != NULL && msg->decompress_payload.iov_len != 0) + { decompress_body->iov_base = msg->decompress_payload.iov_base; decompress_body->iov_len = msg->decompress_payload.iov_len; return; } - if(msg->raw_payload.iov_base != NULL && msg->raw_payload.iov_len != 0){ + /** + * @brief If the body hasn't been compressed, same as http_message_raw_body_get0(). + * + */ + + if (HTTP_MESSAGE_REQ_BODY_START == msg->type + || HTTP_MESSAGE_REQ_BODY == msg->type + || HTTP_MESSAGE_REQ_BODY_END == msg->type) + { + ref_data = msg->ref_queue->array[msg->queue_index].req_data; + } + else if (HTTP_MESSAGE_RES_BODY_START == msg->type + || HTTP_MESSAGE_RES_BODY == msg->type + || HTTP_MESSAGE_RES_BODY_END == msg->type) + { + ref_data = msg->ref_queue->array[msg->queue_index].res_data; + } + ecode = http_half_data_get_content_encoding(ref_data); + if(ref_data != NULL && HTTP_CONTENT_ENCODING_NONE != ecode){ + goto fail; + } + + if (msg->raw_payload.iov_base != NULL && msg->raw_payload.iov_len != 0) + { decompress_body->iov_base = msg->raw_payload.iov_base; decompress_body->iov_len = msg->raw_payload.iov_len; } |
