初步完成HTTP应答侧解析功能,并修正一系类错误处理类的问题。

This commit is contained in:
Lu Qiuwen
2018-09-23 17:33:05 +08:00
parent 7b6dbb06aa
commit 2798783641
10 changed files with 223 additions and 49 deletions

View File

@@ -29,7 +29,7 @@ int http_connection_entry_open(const struct tfe_stream * stream, unsigned int th
struct http_connection_private * ht_conn = ALLOC(struct http_connection_private, 1);
TAILQ_INIT(&ht_conn->hs_private_list);
ht_conn->stream = stream;
*pme = (void *)ht_conn;
*pme = (void *) ht_conn;
return 0;
}
@@ -43,11 +43,12 @@ struct user_event_dispatch_closure
static int __user_event_dispatch(struct http_half_private * hf_private,
enum tfe_http_event ev, const unsigned char * data, size_t len, void * user)
{
struct user_event_dispatch_closure * __closure = (struct user_event_dispatch_closure *)user;
struct http_frame_session_ctx* ht_frame = hf_private->session->ht_frame;
struct user_event_dispatch_closure * __closure = (struct user_event_dispatch_closure *) user;
struct http_frame_session_ctx * ht_frame = hf_private->session->ht_frame;
//todo:
http_frame_raise_event(ht_frame, __closure->stream, (struct tfe_http_session *)__closure->session,
ev, data, len, __closure->thread_id); return 0;
http_frame_raise_event(ht_frame, __closure->stream, (struct tfe_http_session *) __closure->session,
ev, data, len, __closure->thread_id);
return 0;
}
enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_stream * stream,
@@ -139,8 +140,78 @@ __errout:
}
enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stream * stream,
struct http_connection_private * ht_conn, unsigned int thread_id, const unsigned char * data, size_t len)
struct http_connection_private * hc_private, unsigned int thread_id, const unsigned char * data, size_t len)
{
struct http_session_private * hs_private = TAILQ_FIRST(&hc_private->hs_private_list);
struct http_half_private * hf_private_response = to_hf_response_private(hs_private);
int ret = 0;
/* Standalone response, it means missing something or malformed http protocol */
if (hs_private == NULL)
{
TFE_STREAM_LOG_ERROR(stream, "Standlone HTTP response emerged. Malformed HTTP Protocol, detached. ");
goto __errout;
}
/* First time parse http response */
if (hf_private_response == NULL)
{
/* HTTP Version */
short resp_major = to_hf_request_private(hs_private)->major;
short resp_minor = to_hf_request_private(hs_private)->minor;
/* Response */
hf_private_response = hf_private_create(TFE_HTTP_RESPONSE, resp_major, resp_minor);
hs_private_hf_private_set(hs_private, hf_private_response, TFE_HTTP_RESPONSE);
hf_private_set_session(hf_private_response, hs_private);
/* Closure, catch stream, session and thread_id */
struct user_event_dispatch_closure * __closure = ALLOC(struct user_event_dispatch_closure, 1);
__closure->thread_id = thread_id;
__closure->stream = stream;
__closure->session = to_hs_public(hs_private);
/* Set callback, this callback used to raise business event */
hf_private_set_callback(hf_private_response, __user_event_dispatch, __closure, free);
/* Inherit user stream action, this action can affact session's behavior */
hf_private_response->user_stream_action = to_hf_request_private(hs_private)->user_stream_action;
}
/* Parse the content, the data which in defered state has been ignored. */
ret = hf_private_parse(hf_private_response, data, len);
/* Need more data, no boundary touched */
if (ret == 0)
{
if (hf_private_response->stream_action == ACTION_DROP_DATA ||
hf_private_response->stream_action == ACTION_FORWARD_DATA)
{
hf_private_response->parse_cursor = 0;
}
return hf_private_response->stream_action;
}
/* Some kind of error happened, write log and detach the stream */
if (ret == -1)
{
TFE_STREAM_LOG_ERROR(stream, "Failed at parsing HTTP response: %u, %s, %s",
hf_private_request->parse_errno, http_errno_name(hf_private_request->parse_errno),
http_errno_description(hf_private_request->parse_errno));
goto __errout;
}
if (hf_private_response->message_status == STATUS_COMPLETE)
{
http_frame_raise_session_end(hs_private->ht_frame, stream, &hs_private->hs_public, thread_id);
TAILQ_REMOVE(&hc_private->hs_private_list, hs_private, next);
hs_private_destory(hs_private);
}
__errout:
tfe_stream_detach(stream);
return ACTION_FORWARD_DATA;
}
@@ -158,7 +229,7 @@ int __http_connection_identify(const struct tfe_stream * stream,
enum tfe_stream_action http_connection_entry_data(const struct tfe_stream * stream, unsigned int thread_id,
enum tfe_conn_dir dir, const unsigned char * data, size_t len, void ** pme)
{
struct http_connection_private * ht_conn = (struct http_connection_private *)(*pme);
struct http_connection_private * ht_conn = (struct http_connection_private *) (*pme);
if (ht_conn->is_preempted == 0)
{
@@ -192,30 +263,33 @@ __detach:
void http_connection_entry_close(const struct tfe_stream * stream, unsigned int thread_id,
enum tfe_stream_close_reason reason, void ** pme)
{
struct http_connection_private * ht_conn = (struct http_connection_private *)(*pme);
struct http_connection_private * ht_conn = (struct http_connection_private *) (*pme);
*pme = NULL;
/* Delete all live sessions */
struct http_session_private * hs_private_iter = NULL;
TAILQ_FOREACH(hs_private_iter, &ht_conn->hs_private_list, next)
while(true)
{
struct http_session_private * hs_private_iter = TAILQ_FIRST(&ht_conn->hs_private_list);
if (hs_private_iter == NULL) break;
TAILQ_REMOVE(&ht_conn->hs_private_list, hs_private_iter, next);
hs_private_destory(hs_private_iter);
}
/* Clear session counter, and free ht_conn structure */
ht_conn->session_id_counter = 0;
free(ht_conn); *pme = NULL;
free(ht_conn);
}
static struct tfe_plugin __http_plugin_info =
{
.symbol = "HTTP",
.type = TFE_PLUGIN_TYPE_PROTOCOL,
.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,
.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)