初步调通HTTP请求头部内容替换业务

This commit is contained in:
Lu Qiuwen
2018-09-25 20:32:24 +08:00
parent d2e4ce94c2
commit d0ab629f4c
8 changed files with 297 additions and 166 deletions

View File

@@ -56,7 +56,8 @@ enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_strea
struct http_connection_private * hc_private, unsigned int thread_id, const unsigned char * data, size_t len)
{
struct http_session_private * hs_private = TAILQ_LAST(&hc_private->hs_private_list, hs_private_list);
struct http_half_private * hf_private_request = to_hf_request_private(hs_private);
struct http_half_private * hf_private_req_in = to_hf_request_private(hs_private);
struct http_half_private * hf_private_req_user;
/* tfe_hexdump(stderr, __FUNCTION__, data, (unsigned int)len); */
int ret = 0;
@@ -65,12 +66,12 @@ enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_strea
/* There is no available in session list,
* that indicate all HTTP request has corresponding response,
* or the last request is finished, we need to create a new session. */
if (hs_private == NULL || hf_private_request->message_status == STATUS_COMPLETE)
if (hs_private == NULL || hf_private_req_in->message_status == STATUS_COMPLETE)
{
/* HTTP Request and Session */
hf_private_request = hf_private_create(TFE_HTTP_REQUEST, 1, 0);
hs_private = hs_private_create(hc_private, hf_private_request, NULL);
hf_private_set_session(hf_private_request, hs_private);
hf_private_req_in = hf_private_create(TFE_HTTP_REQUEST, 1, 0);
hs_private = hs_private_create(hc_private, hf_private_req_in, NULL);
hf_private_set_session(hf_private_req_in, hs_private);
/* Closure, catch stream, session and thread_id */
struct user_event_dispatch_closure * __closure = ALLOC(struct user_event_dispatch_closure, 1);
@@ -79,7 +80,7 @@ enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_strea
__closure->session = to_hs_public(hs_private);
/* Set callback, this callback used to raise business event */
hf_private_set_callback(hf_private_request, __user_event_dispatch, __closure, free);
hf_private_set_callback(hf_private_req_in, __user_event_dispatch, __closure, free);
/* Call business plugin */
hs_private->ht_frame = http_frame_raise_session_begin(stream, &hs_private->hs_public, thread_id);
@@ -93,41 +94,57 @@ enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_strea
}
/* Parse the content, the data which in defered state has been ignored. */
ret = hf_private_parse(hf_private_request, data, len);
ret = hf_private_parse(hf_private_req_in, data, len);
/* Need more data, no boundary touched */
if (ret == 0)
{
if (hf_private_request->stream_action == ACTION_DROP_DATA ||
hf_private_request->stream_action == ACTION_FORWARD_DATA)
if (hf_private_req_in->stream_action == ACTION_DROP_DATA ||
hf_private_req_in->stream_action == ACTION_FORWARD_DATA)
{
hf_private_request->parse_cursor = 0;
hf_private_req_in->parse_cursor = 0;
}
return hf_private_request->stream_action;
return hf_private_req_in->stream_action;
}
/* Some kind of error happened, write log and detach the stream */
if (ret == -1)
{
TFE_STREAM_LOG_ERROR(stream, "Failed at parsing stream as HTTP: %u, %s, %s",
hf_private_request->parse_errno, http_errno_name(hf_private_request->parse_errno),
http_errno_description(hf_private_request->parse_errno));
hf_private_req_in->parse_errno, http_errno_name(hf_private_req_in->parse_errno),
http_errno_description(hf_private_req_in->parse_errno));
goto __errout;
}
/* Touch a boundary, such as the end of HTTP headers, bodys, et al. */
__action_byptes = hf_private_request->parse_cursor;
hf_private_request->parse_cursor = 0;
__action_byptes = hf_private_req_in->parse_cursor;
hf_private_req_in->parse_cursor = 0;
if (hf_private_request->stream_action == ACTION_FORWARD_DATA)
hf_private_req_user = hs_private->hf_private_req_user;
if (hf_private_req_user != NULL && hf_private_req_user->message_status == STATUS_COMPLETE)
{
/* Construct, and write response immediately */
hf_private_construct(hf_private_req_user);
size_t __to_write_len = evbuffer_get_length(hf_private_req_user->evbuf_raw);
unsigned char * __to_write = evbuffer_pullup(hf_private_req_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_UPSTREAM, __to_write, __to_write_len);
if (unlikely(ret < 0)) { assert(0); }
hf_private_destory(hf_private_req_user);
hs_private->hf_private_resp_user = NULL;
}
if (hf_private_req_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_request->stream_action == ACTION_DROP_DATA)
if (hf_private_req_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;
@@ -158,8 +175,6 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
}
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_resp_in == NULL)
{
@@ -172,41 +187,14 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
hs_private_hf_private_set(hs_private, hf_private_resp_in, TFE_HTTP_RESPONSE);
hf_private_set_session(hf_private_resp_in, 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);
/* 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_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;
}
}
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;
/* Set callback, this callback used to raise business event */
hf_private_set_callback(hf_private_resp_in, __user_event_dispatch, __closure, free);
}
/* Parse the content, the data which in defered state has been ignored. */
@@ -234,6 +222,22 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
goto __errout;
}
hf_private_resp_user = hs_private->hf_private_resp_user;
if (hf_private_resp_user != NULL && hf_private_resp_user->message_status == STATUS_COMPLETE)
{
/* 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;
}
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);
@@ -290,7 +294,7 @@ enum tfe_stream_action http_connection_entry_data(const struct tfe_stream * stre
/* Now, we want to identify this stream */
int ret = __http_connection_identify(stream, ht_conn, data, len);
if (ret != 0) goto __detach;
if (ret < 0) goto __detach;
/* This is HTTP, try to preempt the stream
* It may be failed because other plugin preempted before us */