完成HTTP请求侧解析调试,可以解析请求的URL。
* 增加插件管理功能(简单实现),可以调用解析层插件; * 调整HTTP请求侧解析回调函数实现; * 增加hexdump工具函数;
This commit is contained in:
@@ -68,6 +68,8 @@ enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_strea
|
||||
{
|
||||
struct http_session_private * hs_private = TAILQ_LAST(&hc_private->hs_private_list, hs_private_list);
|
||||
struct http_half_private * hf_private_request = NULL;
|
||||
|
||||
/* tfe_hexdump(stderr, __FUNCTION__, data, (unsigned int)len); */
|
||||
int ret = 0;
|
||||
|
||||
/* There is no available in session list,
|
||||
@@ -80,7 +82,7 @@ enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_strea
|
||||
/* The last request is finished, we need to create a new session,
|
||||
* or proceed parse content for last request */
|
||||
hf_private_request = to_hf_request_private(hs_private);
|
||||
if (hf_private_request->finished)
|
||||
if (hf_private_request->status_message == STATUS_COMPLETE)
|
||||
{
|
||||
goto __new_session;
|
||||
}
|
||||
@@ -112,11 +114,18 @@ __parse:
|
||||
}
|
||||
|
||||
assert(ret == 1);
|
||||
if (hf_private_request->status_header == STATUS_COMPLETE)
|
||||
{
|
||||
printf("===== URI: %s\n", hf_private_request->hf_public.req_spec.uri);
|
||||
}
|
||||
|
||||
/* Touch a boundary, such as the end of HTTP headers, bodys, et al.
|
||||
* need to call user's cb */
|
||||
size_t __forward_bytes = hf_private_request->parse_cursor;
|
||||
tfe_stream_action_set_opt(stream, ACTION_OPT_FOWARD_BYTES, &__forward_bytes, sizeof(__forward_bytes));
|
||||
|
||||
/* Clear the parser cursor */
|
||||
hf_private_request->parse_cursor = 0;
|
||||
return ACTION_FORWARD_DATA;
|
||||
}
|
||||
|
||||
@@ -164,6 +173,7 @@ enum tfe_stream_action http_connection_entry_data(const struct tfe_stream * stre
|
||||
* It may be failed because other plugin preempted before us */
|
||||
ret = tfe_stream_preempt(stream);
|
||||
if (ret != 0) goto __detach;
|
||||
ht_conn->is_preempted = 1;
|
||||
}
|
||||
|
||||
/* This stream has been preempt, this plugin try to parse it */
|
||||
@@ -182,15 +192,15 @@ void http_connection_entry_close(const struct tfe_stream * stream, unsigned int
|
||||
}
|
||||
|
||||
static struct tfe_plugin __http_plugin_info =
|
||||
{
|
||||
.symbol = "HTTP",
|
||||
.type = TFE_PLUGIN_TYPE_PROTOCOL,
|
||||
.proto = APP_PROTO_HTTP1,
|
||||
.on_init = http_plugin_init,
|
||||
.on_deinit = http_plugin_deinit,
|
||||
.on_open = http_connection_entry_open,
|
||||
.on_data = http_connection_entry_data,
|
||||
.on_close = http_connection_entry_close
|
||||
};
|
||||
{
|
||||
.symbol = "HTTP",
|
||||
.type = TFE_PLUGIN_TYPE_PROTOCOL,
|
||||
.proto = APP_PROTO_HTTP1,
|
||||
.on_init = http_plugin_init,
|
||||
.on_deinit = http_plugin_deinit,
|
||||
.on_open = http_connection_entry_open,
|
||||
.on_data = http_connection_entry_data,
|
||||
.on_close = http_connection_entry_close
|
||||
};
|
||||
|
||||
TFE_PLUGIN_REGISTER(HTTP, __http_plugin_info)
|
||||
|
||||
@@ -160,6 +160,9 @@ void __hf_public_resp_fill_from_private(struct http_half_private * hf_private, s
|
||||
* REQUEST PARSER CALLBACKS
|
||||
* ================================================================================================================== */
|
||||
|
||||
#define __HF_PRIVATE_CHANGE_STATUS(_status, _now, _to) \
|
||||
do { assert(_status == _now); _status = _to; } while(0)
|
||||
|
||||
static int __parser_callback_on_message_begin(struct http_parser * parser)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
@@ -172,18 +175,25 @@ static int __parser_callback_on_message_begin(struct http_parser * parser)
|
||||
hf_private->evbuf_header_field = evbuffer_new();
|
||||
hf_private->evbuf_header_value = evbuffer_new();
|
||||
hf_private->evbuf_body = evbuffer_new();
|
||||
|
||||
hf_private->status_header = STATUS_INIT;
|
||||
hf_private->status_body = STATUS_INIT;
|
||||
hf_private->status_message = STATUS_READING;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __parser_callback_on_uri_field(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_INIT, STATUS_READING);
|
||||
return evbuffer_add(hf_private->evbuf_uri, at, length);
|
||||
}
|
||||
|
||||
static int __parser_callback_on_header_field(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_READING, STATUS_READING);
|
||||
|
||||
/* Last field-value tuple doesn't push into hf_private, flush these */
|
||||
if (evbuffer_get_length(hf_private->evbuf_header_field) != 0)
|
||||
{
|
||||
@@ -196,15 +206,10 @@ static int __parser_callback_on_header_field(struct http_parser * parser, const
|
||||
static int __parser_callback_on_header_value(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_READING, STATUS_READING);
|
||||
return evbuffer_add(hf_private->evbuf_header_value, at, length);
|
||||
}
|
||||
|
||||
static int __parser_callback_on_body(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
return evbuffer_add(hf_private->evbuf_body, at, length);
|
||||
}
|
||||
|
||||
static int __parser_callback_on_headers_complete(http_parser * parser)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
@@ -228,14 +233,24 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
|
||||
__hf_public_resp_fill_from_private(hf_private, parser);
|
||||
}
|
||||
|
||||
http_parser_pause(parser, 1);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_READING, STATUS_COMPLETE);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_body, STATUS_INIT, STATUS_READING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __parser_callback_on_body(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
return evbuffer_add(hf_private->evbuf_body, at, length);
|
||||
}
|
||||
|
||||
static int __parser_callback_on_message_complete(http_parser * parser)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
http_parser_pause(parser, 1);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_COMPLETE, STATUS_COMPLETE);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_body, STATUS_READING || STATUS_COMPLETE, STATUS_COMPLETE);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_message, STATUS_READING, STATUS_COMPLETE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -311,6 +326,13 @@ int hf_private_parse(struct http_half_private * hf_private, const unsigned char
|
||||
goto __out;
|
||||
}
|
||||
|
||||
/* Touch message's boundary, the parser jumps to end */
|
||||
if (hf_private->status_message == STATUS_COMPLETE)
|
||||
{
|
||||
ret = 1;
|
||||
goto __out;
|
||||
}
|
||||
|
||||
/* Some kind of exception happend */
|
||||
if (sz_parsed && HTTP_PARSER_ERRNO(hf_private->parse_object) > 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user