diff options
| author | liuwentan <[email protected]> | 2024-01-04 22:23:59 +0800 |
|---|---|---|
| committer | liuwentan <[email protected]> | 2024-01-04 22:23:59 +0800 |
| commit | b446f5cc666495e1ed6cb7064bfb0e6df6dfc3b8 (patch) | |
| tree | fe4f1e4f9c771e7f509777d5e5db8d919cc61e18 | |
| parent | 643cb8e408f8f026237df156f0242b8e2f85c266 (diff) | |
[HTTP_DECODER]format code style
| -rw-r--r-- | src/http_decoder/http_content_decompress.c | 81 | ||||
| -rw-r--r-- | src/http_decoder/http_content_decompress.h | 18 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder.c | 176 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder.h | 48 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_half.c | 302 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_half.h | 55 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_string.c | 67 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_string.h | 24 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_table.c | 84 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_table.h | 66 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_utils.c | 5 | ||||
| -rw-r--r-- | src/http_decoder/http_decoder_utils.h | 8 |
12 files changed, 599 insertions, 335 deletions
diff --git a/src/http_decoder/http_content_decompress.c b/src/http_decoder/http_content_decompress.c index fa99677..4afc2cb 100644 --- a/src/http_decoder/http_content_decompress.c +++ b/src/http_decoder/http_content_decompress.c @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #include <zlib.h> #include <string.h> #include <assert.h> @@ -23,13 +24,14 @@ struct http_content_decompress { enum http_content_encoding encoding; z_stream *z_stream_ptr; - BrotliDecoderState *brdec_state; + BrotliDecoderState *br_state; char *buffer; size_t buffer_size; }; -enum http_content_encoding http_content_encoding_str2int(const char *content_encoding) +enum http_content_encoding +http_content_encoding_str2int(const char *content_encoding) { if (strcasestr(content_encoding, "gzip") != NULL) { return HTTP_CONTENT_ENCODING_GZIP; @@ -46,7 +48,8 @@ enum http_content_encoding http_content_encoding_str2int(const char *content_enc return HTTP_CONTENT_ENCODING_NONE; } -const char *http_content_encoding_int2str(enum http_content_encoding content_encoding) +const char * +http_content_encoding_int2str(enum http_content_encoding content_encoding) { if (content_encoding == HTTP_CONTENT_ENCODING_GZIP) { return "gzip"; @@ -66,19 +69,20 @@ const char *http_content_encoding_int2str(enum http_content_encoding content_enc struct http_content_decompress * http_content_decompress_create(enum http_content_encoding encoding) { - struct http_content_decompress *decompress = CALLOC(struct http_content_decompress, 1); + struct http_content_decompress *decompress = + CALLOC(struct http_content_decompress, 1); assert(decompress); decompress->encoding = encoding; decompress->z_stream_ptr = NULL; - decompress->brdec_state = NULL; + decompress->br_state = NULL; decompress->buffer = CALLOC(char, BUFFER_SIZE); assert(decompress->buffer); decompress->buffer_size = BUFFER_SIZE; - if (encoding == HTTP_CONTENT_ENCODING_GZIP || - encoding == HTTP_CONTENT_ENCODING_DEFLATE) { + if (encoding == HTTP_CONTENT_ENCODING_GZIP + || encoding == HTTP_CONTENT_ENCODING_DEFLATE) { decompress->z_stream_ptr = CALLOC(z_stream, 1); assert(decompress->z_stream_ptr); @@ -89,7 +93,8 @@ http_content_decompress_create(enum http_content_encoding encoding) decompress->z_stream_ptr->next_in = Z_NULL; if (encoding == HTTP_CONTENT_ENCODING_GZIP) { - if (inflateInit2(decompress->z_stream_ptr, MAX_WBITS + 16) != Z_OK) { + if (inflateInit2(decompress->z_stream_ptr, MAX_WBITS + 16) + != Z_OK) { goto error; } } @@ -102,8 +107,8 @@ http_content_decompress_create(enum http_content_encoding encoding) } if (encoding == HTTP_CONTENT_ENCODING_BR) { - decompress->brdec_state = BrotliDecoderCreateInstance(NULL, NULL, NULL); - if (decompress->brdec_state == NULL) { + decompress->br_state = BrotliDecoderCreateInstance(NULL, NULL, NULL); + if (decompress->br_state == NULL) { goto error; } } @@ -115,7 +120,8 @@ 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; @@ -126,18 +132,18 @@ void http_content_decompress_destroy(struct http_content_decompress *decompress) FREE(decompress->z_stream_ptr); } - if (decompress->brdec_state) { - BrotliDecoderDestroyInstance(decompress->brdec_state); - decompress->brdec_state = NULL; + if (decompress->br_state) { + BrotliDecoderDestroyInstance(decompress->br_state); + decompress->br_state = NULL; } FREE(decompress->buffer); FREE(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) +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) { z_stream *z_stream_ptr = decompress->z_stream_ptr; z_stream_ptr->avail_in = (unsigned int)indata_len; @@ -152,8 +158,8 @@ static int http_content_decompress_write_zlib(struct http_content_decompress *de z_stream_ptr->next_out = (unsigned char *)decompress->buffer; ret = inflate(z_stream_ptr, Z_NO_FLUSH); - if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT || - ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) { + if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT + || ret == Z_DATA_ERROR || ret == Z_MEM_ERROR) { (void)inflateEnd(z_stream_ptr); return -1; } @@ -162,13 +168,14 @@ static int http_content_decompress_write_zlib(struct http_content_decompress *de if (have > 0) { if (NULL == *outdata) { http_decoder_log(DEBUG, "%s alloc outbuffer %zu bytes", - http_content_encoding_int2str(decompress->encoding), have); + 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_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; @@ -180,9 +187,9 @@ static int http_content_decompress_write_zlib(struct http_content_decompress *de return 0; } -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) +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) { size_t available_out; size_t available_in = indata_len; @@ -197,13 +204,14 @@ static int http_content_decompress_write_br(struct http_content_decompress *deco available_out = decompress->buffer_size; next_out = (unsigned char *)decompress->buffer; - ret = BrotliDecoderDecompressStream(decompress->brdec_state, &available_in, + ret = BrotliDecoderDecompressStream(decompress->br_state, &available_in, &next_in, &available_out, &next_out, 0); 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_content_encoding_int2str(decompress->encoding), + have); *outdata = safe_dup(decompress->buffer, have); *outdata_len = have; } else { @@ -217,14 +225,16 @@ static int http_content_decompress_write_br(struct http_content_decompress *deco } } - if (ret == BROTLI_DECODER_RESULT_SUCCESS || - ret == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) { + if (ret == BROTLI_DECODER_RESULT_SUCCESS + || ret == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) { return 0; } if (ret == BROTLI_DECODER_RESULT_ERROR) { - BrotliDecoderErrorCode errcode = BrotliDecoderGetErrorCode(decompress->brdec_state); - http_decoder_log(ERROR, "BrotliDecoderDecompressStream() failed: errno = %d, %s", + BrotliDecoderErrorCode errcode = + BrotliDecoderGetErrorCode(decompress->br_state); + http_decoder_log(ERROR, + "BrotliDecoderDecompressStream() failed: errno = %d, %s", errcode, BrotliDecoderErrorString(errcode)); return -1; } @@ -233,8 +243,9 @@ static int http_content_decompress_write_br(struct http_content_decompress *deco } } -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); @@ -245,8 +256,8 @@ int http_content_decompress_write(struct http_content_decompress *decompress, co *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 66ea567..a754e59 100644 --- a/src/http_decoder/http_content_decompress.h +++ b/src/http_decoder/http_content_decompress.h @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #ifndef _HTTP_CONTENT_DECOMPRESS_H_ #define _HTTP_CONTENT_DECOMPRESS_H_ @@ -30,18 +31,23 @@ enum http_content_encoding { struct http_content_decompress; -enum http_content_encoding http_content_encoding_str2int(const char *content_encoding); +enum http_content_encoding +http_content_encoding_str2int(const char *content_encoding); -const char *http_content_encoding_int2str(enum http_content_encoding content_encoding); +const char * +http_content_encoding_int2str(enum http_content_encoding content_encoding); -struct http_content_decompress *http_content_decompress_create(enum http_content_encoding 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 32cfc46..6a531f4 100644 --- a/src/http_decoder/http_decoder.c +++ b/src/http_decoder/http_decoder.c @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #include <assert.h> #include <stdio.h> @@ -24,6 +25,7 @@ #define HTTP_DECODER_RESULT_QUEUE_SIZE 16 #define HD_IS_CACHE_BODY 1 + const char *http_decoder_topic = "HTTP_DECODER_MESSAGE"; struct http_decoder_result { @@ -68,9 +70,11 @@ struct http_decoder_context { struct http_event_context *http_ev_ctx; }; -struct http_decoder_result *http_decoder_result_new() +static struct http_decoder_result * +http_decoder_result_new() { - struct http_decoder_result *result = CALLOC(struct http_decoder_result, 1); + struct http_decoder_result *result = + CALLOC(struct http_decoder_result, 1); assert(result); result->req_data = NULL; @@ -79,7 +83,8 @@ struct http_decoder_result *http_decoder_result_new() return result; } -void http_decoder_result_free(struct http_decoder_result *result) +static void +http_decoder_result_free(struct http_decoder_result *result) { if (NULL == result) { return; @@ -99,7 +104,9 @@ void http_decoder_result_free(struct http_decoder_result *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) +static void +http_decoder_result_queue_push(struct http_decoder_result_queue *queue, + size_t index) { assert(queue); assert(index < queue->queue_size); @@ -111,7 +118,9 @@ static void http_decoder_result_queue_push(struct http_decoder_result_queue *que } // 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) +static void +http_decoder_result_queue_pop(struct http_decoder_result_queue *queue, + size_t index) { assert(queue); assert(index < queue->queue_size); @@ -122,7 +131,8 @@ static void http_decoder_result_queue_pop(struct http_decoder_result_queue *queu } } -static void http_decoder_result_queue_inc_req_index(struct http_decoder_result_queue *queue) +static void +http_decoder_result_queue_inc_req_index(struct http_decoder_result_queue *queue) { assert(queue); @@ -130,7 +140,8 @@ static void http_decoder_result_queue_inc_req_index(struct http_decoder_result_q queue->req_index = queue->req_index % queue->queue_size; } -static void http_decoder_result_queue_inc_res_index(struct http_decoder_result_queue *queue) +static void +http_decoder_result_queue_inc_res_index(struct http_decoder_result_queue *queue) { assert(queue); @@ -138,7 +149,8 @@ static void http_decoder_result_queue_inc_res_index(struct http_decoder_result_q queue->res_index = queue->res_index % queue->queue_size; } -static void http_decoder_result_queue_inc_del_index(struct http_decoder_result_queue *queue) +static void +http_decoder_result_queue_inc_del_index(struct http_decoder_result_queue *queue) { assert(queue); @@ -146,7 +158,9 @@ static void http_decoder_result_queue_inc_del_index(struct http_decoder_result_q 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) +static void +http_decoder_result_queue_gc(struct http_decoder_result_queue *queue, + size_t index) { assert(queue); assert(index < queue->queue_size); @@ -157,9 +171,9 @@ static void http_decoder_result_queue_gc(struct http_decoder_result_queue *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); @@ -238,7 +252,8 @@ static void http_event_handler(enum http_event event, } } -static struct http_decoder *http_decoder_new(http_event_cb *ev_cb, 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); @@ -249,7 +264,8 @@ static struct http_decoder *http_decoder_new(http_event_cb *ev_cb, int is_cache_ return decoder; } -void http_decoder_free(struct http_decoder *decoder) +static void +http_decoder_free(struct http_decoder *decoder) { if (NULL == decoder) { return; @@ -268,9 +284,11 @@ void 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_result_queue * +http_decoder_result_queue_new(size_t queue_size) { - struct http_decoder_result_queue *queue = CALLOC(struct http_decoder_result_queue, 1); + struct http_decoder_result_queue *queue = + CALLOC(struct http_decoder_result_queue, 1); assert(queue); queue->del_index = 0; @@ -288,7 +306,8 @@ static struct http_decoder_result_queue *http_decoder_result_queue_new(size_t qu return queue; } -static void http_decoder_result_queue_free(struct http_decoder_result_queue *queue) +static void +http_decoder_result_queue_free(struct http_decoder_result_queue *queue) { if (NULL == queue) { return; @@ -308,7 +327,8 @@ static void http_decoder_result_queue_free(struct http_decoder_result_queue *que FREE(queue); } -static int http_protocol_identify(const char *data, size_t data_len) +static int +http_protocol_identify(const char *data, size_t data_len) { enum llhttp_type type = HTTP_BOTH; llhttp_t parser; @@ -326,7 +346,8 @@ static int http_protocol_identify(const char *data, size_t data_len) return 0; } -int http_decoder_entry(struct session *sess, int events, const struct packet *pkt, void *cb_arg) +int http_decoder_entry(struct session *sess, int events, + const struct packet *pkt, void *cb_arg) { struct http_decoder_context *ctx = (struct http_decoder_context *)cb_arg; size_t payload_len = 0; @@ -337,7 +358,8 @@ int http_decoder_entry(struct session *sess, int events, const struct packet *pk return 0; } - struct http_decoder_result_queue *queue = session_get_ex_data(sess, ctx->ex_data_idx);; + struct http_decoder_result_queue *queue = + session_get_ex_data(sess, ctx->ex_data_idx);; if (events & SESS_EV_CLOSING) { if (queue != NULL) { @@ -351,18 +373,23 @@ int http_decoder_entry(struct session *sess, int events, const struct packet *pk const char *payload = session_get0_current_payload(sess, &payload_len); if (events & SESS_EV_OPENING) { if (queue != NULL) { - fprintf(stderr, "http_decoder_result_queue should be null for new session\n"); + fprintf(stderr, + "http_decoder_result_queue should be null for new session\n"); return -1; } //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); + 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); return 0; } } @@ -396,7 +423,8 @@ int http_decoder_entry(struct session *sess, int events, const struct packet *pk ctx->http_ev_ctx->ref_queue = queue; ctx->http_ev_ctx->ref_session = sess; - ret = http_decoder_half_parse(cur_half, ctx->http_ev_ctx, payload, payload_len); + ret = http_decoder_half_parse(cur_half, ctx->http_ev_ctx, payload, + payload_len); if (ret < 0) { if (dir == PACKET_DIRECTION_C2S) { http_decoder_result_queue_pop(queue, queue->req_index); @@ -408,14 +436,16 @@ int http_decoder_entry(struct session *sess, int events, const struct packet *pk return 0; } -static void http_decoder_ex_data_free(struct session *s, int idx, void *ex_ptr, void *arg) +static void +http_decoder_ex_data_free(struct session *s, int idx, void *ex_ptr, void *arg) { if (ex_ptr != NULL) { FREE(ex_ptr); } } -void http_message_free(void *msg, void *cb_arg) +static void +http_message_free(void *msg, void *cb_arg) { if (NULL == msg) { return; @@ -434,21 +464,25 @@ void *http_decoder_init(struct stellar *st) ctx->http_ev_ctx = CALLOC(struct http_event_context, 1); ctx->st = st; ctx->ex_data_idx = stellar_session_get_ex_new_index(st, "HTTP_DECODER", - http_decoder_ex_data_free, NULL); + http_decoder_ex_data_free, + NULL); - int plugin_id = stellar_plugin_register(st, SESS_EV_TCP|SESS_EV_CLOSING, http_decoder_entry, ctx); + int plugin_id = stellar_plugin_register(st, SESS_EV_TCP|SESS_EV_CLOSING, + http_decoder_entry, ctx); if (plugin_id >= 0) { ctx->plugin_id = plugin_id; } int topic_id = session_mq_get_topic_id(st, http_decoder_topic); if (topic_id < 0) { - topic_id = session_mq_create_topic(st, http_decoder_topic, http_message_free, NULL); + topic_id = session_mq_create_topic(st, http_decoder_topic, + http_message_free, NULL); } ctx->topic_id = topic_id; 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; } @@ -458,7 +492,9 @@ void http_decoder_exit(void *decoder_ctx) return; } - struct http_decoder_context *ctx = (struct http_decoder_context *)decoder_ctx; + struct http_decoder_context *ctx = + (struct http_decoder_context *)decoder_ctx; + if (ctx->http_ev_ctx != NULL) { FREE(ctx->http_ev_ctx); } @@ -475,7 +511,8 @@ void http_decoder_exit(void *decoder_ctx) FREE(decoder_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; @@ -484,7 +521,9 @@ enum http_message_type 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) { return -1; @@ -493,7 +532,9 @@ int http_message_get_request_line(struct http_message *msg, struct http_request_ 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) { return -1; @@ -502,76 +543,99 @@ int http_message_get_response_line(struct http_message *msg, struct http_respons 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 *header_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 == header_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, header_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 *header_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 == header_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, header_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 *header) { - if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER || NULL == header) { + if (NULL == msg || msg->type != HTTP_MESSAGE_REQ_HEADER + || NULL == header) { return -1; } return http_decoder_half_data_iter_header(msg->data, 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) { - if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER || NULL == header) { + if (NULL == msg || msg->type != HTTP_MESSAGE_RES_HEADER + || NULL == header) { return -1; } return http_decoder_half_data_iter_header(msg->data, 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) { - 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 e253bb6..b06876f 100644 --- a/src/http_decoder/http_decoder.h +++ b/src/http_decoder/http_decoder.h @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #ifndef _HTTP_DECODER_H_ #define _HTTP_DECODER_H_ @@ -18,6 +19,7 @@ extern "C" #include <stddef.h> + enum http_message_type { HTTP_MESSAGE_REQ_LINE, HTTP_MESSAGE_REQ_HEADER, @@ -59,33 +61,53 @@ 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 *header_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 *header_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_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_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 2128025..294fcd2 100644 --- a/src/http_decoder/http_decoder_half.c +++ b/src/http_decoder/http_decoder_half.c @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #include <assert.h> #include <stdio.h> @@ -18,6 +19,7 @@ #include "http_decoder_table.h" #include "http_content_decompress.h" + struct http_decoder_half_data { struct http_decoder_table *table; @@ -61,7 +63,8 @@ static void printf_debug_info(const char *desc, const char *at, size_t length) #define printf_debug_info(desc, at, length) #endif -static void http_decoder_half_data_decompress(struct http_decoder_half_data *data) +static void +http_decoder_half_data_decompress(struct http_decoder_half_data *data) { assert(data); @@ -78,7 +81,8 @@ static void http_decoder_half_data_decompress(struct http_decoder_half_data *dat return; } - struct http_content_decompress *decompress = http_content_decompress_create(data->content_encoding); + struct http_content_decompress *decompress = + http_content_decompress_create(data->content_encoding); assert(decompress); if (http_content_decompress_write(decompress, raw_body.str, raw_body.str_len, &data->decompress_body, @@ -89,29 +93,34 @@ static void http_decoder_half_data_decompress(struct http_decoder_half_data *dat } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); half->event = HTTP_EVENT_INIT; 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); // if enable cache_body, trigger body_data/body_end event // if disable cache_body, trigger body_end event - if (half->is_cache_body || - (half->ref_data->content_encoding != HTTP_CONTENT_ENCODING_NONE)) { - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) == STRING_STATE_CACHE) { + if (half->is_cache_body + || (half->ref_data->content_encoding != HTTP_CONTENT_ENCODING_NONE)) { + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) + == STRING_STATE_CACHE) { http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_BODY); http_decoder_half_data_decompress(half->ref_data); } @@ -120,24 +129,28 @@ static int on_message_complete(llhttp_t *http) if (half->event == HTTP_EVENT_REQ_BODY_BEGIN) { half->event = HTTP_EVENT_REQ_BODY_DATA; if (half->http_ev_cb != NULL) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } half->event = HTTP_EVENT_REQ_BODY_END; if (half->http_ev_cb != NULL) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } else { if (half->event == HTTP_EVENT_RES_BODY_BEGIN) { half->event = HTTP_EVENT_RES_BODY_DATA; if (half->http_ev_cb != NULL) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } half->event = HTTP_EVENT_RES_BODY_END; if (half->http_ev_cb != NULL) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } @@ -147,14 +160,16 @@ static int on_message_complete(llhttp_t *http) if (half->event == HTTP_EVENT_REQ_BODY_DATA) { half->event = HTTP_EVENT_REQ_BODY_END; if (half->http_ev_cb != NULL) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } else { if (half->event == HTTP_EVENT_RES_BODY_DATA) { half->event = HTTP_EVENT_RES_BODY_END; if (half->http_ev_cb != NULL) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } @@ -176,18 +191,21 @@ static int 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->parser.type == HTTP_REQUEST) { @@ -198,22 +216,26 @@ static int on_method(llhttp_t *http, const char *at, size_t length) } if (half->ref_data != NULL) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_METHOD, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_METHOD, + at, length); } return 0; } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - 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); } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_METHOD); @@ -223,30 +245,36 @@ static int on_method_complete(llhttp_t *http) } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_URI, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_URI, + at, length); } return 0; } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - if (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); } @@ -257,11 +285,13 @@ static int on_uri_complete(llhttp_t *http) } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->parser.type == HTTP_RESPONSE) { @@ -272,22 +302,26 @@ static int on_version(llhttp_t *http, const char *at, size_t length) } if (half->ref_data != NULL) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_VERSION, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_VERSION, + at, length); } return 0; } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data) { - 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); } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_VERSION); @@ -307,30 +341,36 @@ static int 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_STATUS, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_STATUS, + at, length); } return 0; } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - 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); } @@ -347,30 +387,36 @@ static int 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_HDRKEY, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_HDRKEY, + at, length); } return 0; } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - if (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); } @@ -381,30 +427,36 @@ static int on_header_field_complete(llhttp_t *http) } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_HDRVAL, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_HDRVAL, + at, length); } return 0; } /* 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->ref_data != NULL) { - 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) { http_decoder_table_cache(half->ref_data->table, HTTP_ITEM_HDRVAL); } http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_HDRVAL); @@ -413,10 +465,16 @@ static int on_header_value_complete(llhttp_t *http) if (half->ref_data != NULL) { 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) { + 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) { char *str = safe_dup(http_hdr.val.str, http_hdr.val.str_len); - half->ref_data->content_encoding = http_content_encoding_str2int(str); + half->ref_data->content_encoding = + http_content_encoding_str2int(str); FREE(str); } } @@ -429,7 +487,8 @@ static int 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); @@ -440,7 +499,8 @@ static int 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); @@ -453,11 +513,13 @@ static int 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); if (half->parser.type == HTTP_REQUEST) { @@ -478,11 +540,13 @@ static int 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); - struct http_decoder_half *half = container_of(http, struct http_decoder_half, parser); + struct http_decoder_half *half = + container_of(http, struct http_decoder_half, parser); assert(half); // trigger body_begin event @@ -490,43 +554,50 @@ static int on_body(llhttp_t *http, const char *at, size_t length) if (half->event == HTTP_EVENT_REQ_HDR_END) { half->event = HTTP_EVENT_REQ_BODY_BEGIN; if (half->http_ev_cb) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } else { if (half->event == HTTP_EVENT_RES_HDR_END) { half->event = HTTP_EVENT_RES_BODY_BEGIN; if (half->http_ev_cb) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } // 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)) { - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_BODY, at, length); + 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); } else { if (half->ref_data != NULL) { - if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) == STRING_STATE_COMMIT) { + if (http_decoder_table_state(half->ref_data->table, HTTP_ITEM_BODY) + == STRING_STATE_COMMIT) { http_decoder_table_reset(half->ref_data->table, HTTP_ITEM_BODY); } - http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_BODY, at, length); + http_decoder_table_refer(half->ref_data->table, HTTP_ITEM_BODY, + at, length); http_decoder_table_commit(half->ref_data->table, HTTP_ITEM_BODY); } if (half->parser.type == HTTP_REQUEST) { half->event = HTTP_EVENT_REQ_BODY_DATA; if (half->http_ev_cb) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } else { half->event = HTTP_EVENT_RES_BODY_DATA; if (half->http_ev_cb) { - half->http_ev_cb(half->event, &half->ref_data, half->http_ev_ctx); + half->http_ev_cb(half->event, &half->ref_data, + half->http_ev_ctx); } } } @@ -534,8 +605,9 @@ static int on_body(llhttp_t *http, const char *at, size_t length) return 0; } -void http_decoder_half_init(struct http_decoder_half *half, http_event_cb *http_ev_cb, - int is_cache_body) +void +http_decoder_half_init(struct http_decoder_half *half, + http_event_cb *http_ev_cb, int is_cache_body) { if (NULL == half) { return; @@ -591,7 +663,8 @@ http_decoder_half_new(http_event_cb *ev_cb, int is_cache_body) 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; @@ -600,8 +673,9 @@ void 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; @@ -611,11 +685,12 @@ int http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, half->error = llhttp_execute(&half->parser, data, data_len); int ret = 0; + switch (half->error) { case HPE_OK: + break; case HPE_PAUSED_UPGRADE: llhttp_resume_after_upgrade(&half->parser); - ret = 0; break; default: ret = -1; @@ -629,38 +704,38 @@ int http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, } if (half->ref_data != NULL) { - if (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); } - 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_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); } - 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) { 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); } } @@ -668,9 +743,11 @@ int http_decoder_half_parse(struct http_decoder_half *half, void *http_ev_ctx, return 0; } -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); + struct http_decoder_half_data *data = + CALLOC(struct http_decoder_half_data, 1); assert(data); data->table = http_decoder_table_new(); @@ -687,7 +764,8 @@ struct http_decoder_half_data *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; @@ -705,8 +783,9 @@ void 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; @@ -722,8 +801,9 @@ int 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; @@ -739,20 +819,21 @@ int 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 *header_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, header_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; @@ -761,8 +842,9 @@ int 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; @@ -771,8 +853,9 @@ int 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; @@ -788,7 +871,8 @@ int http_decoder_half_data_get_decompress_body(struct http_decoder_half_data *da 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 f682c8f..0f62fdf 100644 --- a/src/http_decoder/http_decoder_half.h +++ b/src/http_decoder/http_decoder_half.h @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #ifndef _HTTP_DECODER_HALF_H_ #define _HTTP_DECODER_HALF_H_ @@ -21,6 +22,7 @@ extern "C" #include "http_decoder.h" #include "http_content_decompress.h" + // only one http event is fired at a time enum http_event { HTTP_EVENT_INIT = 0, @@ -47,42 +49,53 @@ 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); -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); //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 *header_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_string.c b/src/http_decoder/http_decoder_string.c index 4b5b104..b4fba34 100644 --- a/src/http_decoder/http_decoder_string.c +++ b/src/http_decoder/http_decoder_string.c @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -17,7 +18,9 @@ #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: @@ -38,8 +41,9 @@ static const char *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; @@ -59,37 +63,44 @@ void 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; } - switch (rstr->state) { - case STRING_STATE_REFER: - if (rstr->refer.str_len > 0) { - if (NULL == rstr->cache.str) { - rstr->cache.str = CALLOC(char, rstr->refer.str_len + 1); - } else { - rstr->cache.str = REALLOC(char, rstr->cache.str, rstr->cache.str_len + rstr->refer.str_len + 2); - } - - memcpy(rstr->cache.str + rstr->cache.str_len, rstr->refer.str, rstr->refer.str_len); - rstr->cache.str_len += rstr->refer.str_len; + size_t length = 0; - rstr->refer.str = NULL; - rstr->refer.str_len = 0; + switch (rstr->state) { + case STRING_STATE_REFER: + if (rstr->refer.str_len > 0) { + if (NULL == rstr->cache.str) { + length = rstr->refer.str_len + 1; + rstr->cache.str = CALLOC(char, length); + } else { + length = rstr->cache.str_len + rstr->refer.str_len + 2; + rstr->cache.str = REALLOC(char, rstr->cache.str, length); } - break; - default: - abort(); - break; + + memcpy(rstr->cache.str + rstr->cache.str_len, rstr->refer.str, + rstr->refer.str_len); + rstr->cache.str_len += rstr->refer.str_len; + + rstr->refer.str = NULL; + rstr->refer.str_len = 0; + } + break; + default: + abort(); + break; } 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; @@ -124,7 +135,8 @@ void 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); @@ -144,12 +156,14 @@ void 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; @@ -166,7 +180,8 @@ int http_decoder_string_get(struct http_decoder_string *rstr, struct hstring *ou 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 363d6af..6f3ff0b 100644 --- a/src/http_decoder/http_decoder_string.h +++ b/src/http_decoder/http_decoder_string.h @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #ifndef _HTTP_DECODER_STRING_H_ #define _HTTP_DECODER_STRING_H_ @@ -18,6 +19,7 @@ extern "C" #include "http_decoder.h" + enum string_state { STRING_STATE_INIT, STRING_STATE_REFER, @@ -66,19 +68,27 @@ 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 5523375..4867125 100644 --- a/src/http_decoder/http_decoder_table.c +++ b/src/http_decoder/http_decoder_table.c @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #include <assert.h> #include <stdlib.h> #include <string.h> @@ -19,6 +20,7 @@ #define MAX_HEADER_SIZE 16 + struct http_decoder_header { struct http_decoder_string key; struct http_decoder_string val; @@ -37,7 +39,8 @@ 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); @@ -49,7 +52,8 @@ struct http_decoder_table *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; @@ -92,12 +96,13 @@ void http_decoder_table_free(struct http_decoder_table *table) FREE(table); } -enum string_state http_decoder_table_state(struct http_decoder_table *table, - enum http_item type) +enum string_state +http_decoder_table_state(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return STRING_STATE_INIT; } + struct http_decoder_header *header = NULL; enum string_state state = STRING_STATE_INIT; assert(table); @@ -134,8 +139,9 @@ enum string_state http_decoder_table_state(struct http_decoder_table *table, 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; @@ -183,7 +189,8 @@ void http_decoder_table_refer(struct http_decoder_table *table, enum http_item t } } -void http_decoder_table_cache(struct http_decoder_table *table, enum http_item type) +void +http_decoder_table_cache(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return; @@ -222,7 +229,8 @@ void http_decoder_table_cache(struct http_decoder_table *table, enum http_item t } } -void http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) +void +http_decoder_table_commit(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return; @@ -263,7 +271,8 @@ void http_decoder_table_commit(struct http_decoder_table *table, enum http_item } } -void http_decoder_table_reset(struct http_decoder_table *table, enum http_item type) +void +http_decoder_table_reset(struct http_decoder_table *table, enum http_item type) { if (NULL == table) { return; @@ -302,7 +311,8 @@ void http_decoder_table_reset(struct http_decoder_table *table, enum http_item t } } -void http_decoder_table_dump(struct http_decoder_table *table) +void +http_decoder_table_dump(struct http_decoder_table *table) { if (NULL == table) { return; @@ -321,8 +331,9 @@ 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) { if (NULL == table || NULL == out) { return -1; @@ -331,8 +342,9 @@ int 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; @@ -341,8 +353,9 @@ int 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; @@ -351,8 +364,9 @@ int 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; @@ -361,8 +375,9 @@ int 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; @@ -371,8 +386,10 @@ int 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 *header_array, + size_t array_size) { if (NULL == table || NULL == key->str || 0 == key->str_len) { return 0; @@ -381,14 +398,15 @@ int http_decoder_table_get_header(struct http_decoder_table *table, struct hstri int header_cnt = 0; for (int i = 0; i < table->header_index && header_cnt < array_size; i++) { struct http_decoder_header *tmp_header = &table->headers[i]; - if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT && - http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) { + if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT + && http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) { struct hstring tmp_key; http_decoder_string_get(&tmp_header->key, &tmp_key); - 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); + 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); } } } @@ -396,8 +414,9 @@ int http_decoder_table_get_header(struct http_decoder_table *table, struct hstri 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 *header) { if (NULL == table || NULL == header) { return -1; @@ -407,9 +426,10 @@ int http_decoder_table_iter_header(struct http_decoder_table *table, return 0; } - struct http_decoder_header *tmp_header = &table->headers[table->header_iter++]; - if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT && - http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) { + struct http_decoder_header *tmp_header = + &table->headers[table->header_iter++]; + if (http_decoder_string_state(&tmp_header->key) == STRING_STATE_COMMIT + && http_decoder_string_state(&tmp_header->val) == STRING_STATE_COMMIT) { http_decoder_string_get(&tmp_header->key, &header->key); http_decoder_string_get(&tmp_header->val, &header->val); return 1; diff --git a/src/http_decoder/http_decoder_table.h b/src/http_decoder/http_decoder_table.h index 3457151..0e04f0e 100644 --- a/src/http_decoder/http_decoder_table.h +++ b/src/http_decoder/http_decoder_table.h @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #ifndef _HTTP_DECODER_TABLE_H_ #define _HTTP_DECODER_TABLE_H_ @@ -20,6 +21,7 @@ extern "C" { #include "http_decoder.h" #include "http_decoder_string.h" + enum http_item { HTTP_ITEM_URI = 0x01, HTTP_ITEM_STATUS = 0x02, @@ -32,47 +34,59 @@ 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); +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 *header_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 *header); #ifdef __cplusplus } #endif diff --git a/src/http_decoder/http_decoder_utils.c b/src/http_decoder/http_decoder_utils.c index a5dfbe1..33b2d8e 100644 --- a/src/http_decoder/http_decoder_utils.c +++ b/src/http_decoder/http_decoder_utils.c @@ -8,11 +8,14 @@ *********************************************************************************************** */ + #include <string.h> #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 0d981c5..97f1fd2 100644 --- a/src/http_decoder/http_decoder_utils.h +++ b/src/http_decoder/http_decoder_utils.h @@ -8,6 +8,7 @@ *********************************************************************************************** */ + #ifndef _HTTP_DECODER_UTILS_H_ #define _HTTP_DECODER_UTILS_H_ @@ -19,14 +20,15 @@ extern "C" #include <stdlib.h> #include <stdio.h> -char *safe_dup(const char *str, size_t len); + +char * +safe_dup(const char *str, size_t len); /****************************************************************************** * Logger ******************************************************************************/ -enum http_decoder_log_level -{ +enum http_decoder_log_level { DEBUG = 0x11, WARN = 0x12, INFO = 0x13, |
