初步调通HTTP重定向业务

* 增加HTTP Status标准化定义及辅助函数;
* 增加HTTP解析层发送应答的功能
* 修正了Pangu HTTP实现导致段错误的一系列问题。
This commit is contained in:
Lu Qiuwen
2018-09-25 10:17:50 +08:00
parent 0776cb3ec8
commit c2f0bde211
9 changed files with 350 additions and 82 deletions

View File

@@ -9,6 +9,7 @@
#include <http_common.h>
#include <http_half.h>
#include <assert.h>
#include <event.h>
struct http_plugin __g_http_plugin;
struct http_plugin * g_http_plugin = &__g_http_plugin;
@@ -143,8 +144,11 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
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);
struct http_half_private * hf_private_resp_in;
struct http_half_private * hf_private_resp_user;
int ret = 0;
size_t __action_byptes;
/* Standalone response, it means missing something or malformed http protocol */
if (hs_private == NULL)
@@ -153,44 +157,71 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
goto __errout;
}
hf_private_resp_in = to_hf_response_private(hs_private);
hf_private_resp_user = hs_private->hf_private_resp_user;
/* First time parse http response */
if (hf_private_response == NULL)
if (hf_private_resp_in == 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);
hf_private_resp_in = hf_private_create(TFE_HTTP_RESPONSE, resp_major, resp_minor);
hs_private_hf_private_set(hs_private, hf_private_resp_in, TFE_HTTP_RESPONSE);
hf_private_set_session(hf_private_resp_in, 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);
if (hf_private_resp_user != NULL)
{
/* Set nothing callback, dont call user callback because the data need to be droped */
hf_private_set_callback(hf_private_resp_in, NULL, NULL, NULL);
/* Drop all data, because the user's response need to be send */
hf_private_resp_in->stream_action = ACTION_DROP_DATA;
}
else
{
/* 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);
/* Set callback, this callback used to raise business event */
hf_private_set_callback(hf_private_resp_in, __user_event_dispatch, __closure, free);
/* Inherit user stream action, this action can affact session's behavior */
hf_private_resp_in->user_stream_action = to_hf_request_private(hs_private)->user_stream_action;
}
}
/* 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;
if (hf_private_resp_user != NULL)
{
/* Construct, and write response immediately */
hf_private_construct(hf_private_resp_user);
size_t __to_write_len = evbuffer_get_length(hf_private_resp_user->evbuf_raw);
unsigned char * __to_write = evbuffer_pullup(hf_private_resp_user->evbuf_raw, __to_write_len);
/* Write the data to stream, UPSTREAM is the incoming direction for response */
ret = tfe_stream_write(stream, CONN_DIR_DOWNSTREAM, __to_write, __to_write_len);
if (unlikely(ret < 0)) { assert(0); }
hf_private_destory(hf_private_resp_user);
hs_private->hf_private_resp_user = NULL;
}
/* Parse the content, the data which in defered state has been ignored. */
ret = hf_private_parse(hf_private_response, data, len);
ret = hf_private_parse(hf_private_resp_in, 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)
if (hf_private_resp_in->stream_action == ACTION_DROP_DATA ||
hf_private_resp_in->stream_action == ACTION_FORWARD_DATA)
{
hf_private_response->parse_cursor = 0;
hf_private_resp_in->parse_cursor = 0;
}
return hf_private_response->stream_action;
return hf_private_resp_in->stream_action;
}
/* Some kind of error happened, write log and detach the stream */
@@ -203,13 +234,30 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
goto __errout;
}
if (hf_private_response->message_status == STATUS_COMPLETE)
if (hf_private_resp_in->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);
}
__action_byptes = hf_private_resp_in->parse_cursor;
hf_private_resp_in->parse_cursor = 0;
if (hf_private_resp_in->stream_action == ACTION_FORWARD_DATA)
{
tfe_stream_action_set_opt(stream, ACTION_OPT_FOWARD_BYTES, &__action_byptes, sizeof(__action_byptes));
return ACTION_FORWARD_DATA;
}
if (hf_private_resp_in->stream_action == ACTION_DROP_DATA)
{
tfe_stream_action_set_opt(stream, ACTION_OPT_DROP_BYTES, &__action_byptes, sizeof(__action_byptes));
return ACTION_DROP_DATA;
}
goto __errout;
__errout:
tfe_stream_detach(stream);
return ACTION_FORWARD_DATA;