增加简单的HTTP协议插件管理层,基本联通HTTP解析层与业务层插件。

This commit is contained in:
Lu Qiuwen
2018-09-17 15:44:44 +08:00
parent 9fec5c3cc4
commit 48c8d6a659
8 changed files with 439 additions and 212 deletions

View File

@@ -23,92 +23,78 @@ void http_plugin_deinit(struct tfe_proxy * proxy)
return;
}
struct http_session_private * hs_private_create(struct http_connection_private * hc_private,
struct http_half_private * hf_private_req, struct http_half_private * hf_private_resp)
{
struct http_session_private * __hs_private = ALLOC(struct http_session_private, 1);
/* HS-PUBLIC */
__hs_private->hs_public.req = hf_private_req != NULL ? to_hf_public(hf_private_req) : NULL;
__hs_private->hs_public.resp = hf_private_req != NULL ? to_hf_public(hf_private_resp) : NULL;
__hs_private->hs_public.session_id = hc_private->session_id_counter++;
/* HS-PRIVATE*/
__hs_private->hc_private = hc_private;
return __hs_private;
}
void hs_private_destory(struct http_session_private * hs_private)
{
free(hs_private);
}
static void __SET_PME_HC_PRIVATE(struct http_connection_private * h_conn, void ** pme)
{
*pme = (void *) h_conn;
}
static struct http_connection_private * __GET_PME_HC_PRIVATE(void ** pme)
{
return (struct http_connection_private *) (*pme);
}
int http_connection_entry_open(const struct tfe_stream * stream, unsigned int thread_id,
enum tfe_conn_dir dir, void ** pme)
{
struct http_connection_private * ht_conn = ALLOC(struct http_connection_private, 1);
TAILQ_INIT(&ht_conn->hs_private_list);
__SET_PME_HC_PRIVATE(ht_conn, pme);
*pme = (void *)ht_conn;
return 0;
}
struct user_event_dispatch_closure
{
const struct tfe_stream * stream;
const struct tfe_http_session * session;
unsigned int thread_id;
};
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;
http_frame_raise_event(ht_frame, __closure->stream, __closure->session,
ev, data, len, __closure->thread_id); return 0;
}
enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_stream * stream,
struct http_connection_private * hc_private, 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_LAST(&hc_private->hs_private_list, hs_private_list);
struct http_half_private * hf_private_request = NULL;
struct http_half_private * hf_private_request = to_hf_request_private(hs_private);
/* tfe_hexdump(stderr, __FUNCTION__, data, (unsigned int)len); */
int ret = 0;
size_t __action_byptes;
/* There is no available in session list,
* that indicate all HTTP request has corresponding response */
if (hs_private == NULL)
* 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)
{
goto __new_session;
/* 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);
/* 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_request, __user_event_dispatch, __closure, free);
/* Call business plugin */
hs_private->ht_frame = http_frame_raise_session_begin(stream, &hs_private->hs_public, thread_id);
if (hs_private->ht_frame == NULL)
{
TFE_STREAM_LOG_ERROR(stream, "Failed at raising session begin event. ");
goto __errout;
}
TAILQ_INSERT_TAIL(&hc_private->hs_private_list, hs_private, next);
}
/* 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->status_message == STATUS_COMPLETE)
{
goto __new_session;
}
goto __parse;
__new_session:
hf_private_request = hf_private_create(TFE_HTTP_REQUEST, 1, 0);
hs_private = hs_private_create(hc_private, hf_private_request, NULL);
/* Call business plugin */
hs_private->ht_frame = http_frame_raise_session_begin(stream, &hs_private->hs_public, thread_id);
if(hs_private->ht_frame == NULL)
{
TFE_STREAM_LOG_ERROR(stream, "Failed at raising session begin event. ");
tfe_stream_detach(stream);
return ACTION_FORWARD_DATA;
}
TAILQ_INSERT_TAIL(&hc_private->hs_private_list, hs_private, next);
__parse:
/* proceed parse content for last request */
ret = hf_private_parse(hf_private_request, data, len);
/* Need more data, no boundary touched */
if (ret == 0)
{
return ACTION_DEFER_DATA;
return hf_private_request->stream_action;
}
/* Some kind of error happened, write log and detach the stream */
@@ -118,23 +104,29 @@ __parse:
hf_private_request->parse_errno, http_errno_name(hf_private_request->parse_errno),
http_errno_description(hf_private_request->parse_errno));
tfe_stream_detach(stream);
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;
if (hf_private_request->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->status_header == STATUS_COMPLETE)
if (hf_private_request->stream_action == ACTION_DROP_DATA)
{
http_frame_raise_event(hs_private->ht_frame, stream, &hs_private->hs_public,
EV_HTTP_REQ_HDR, NULL, 0, thread_id);
tfe_stream_action_set_opt(stream, ACTION_OPT_DROP_BYTES, &__action_byptes, sizeof(__action_byptes));
return ACTION_DROP_DATA;
}
/* 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));
goto __errout;
/* Clear the parser cursor */
hf_private_request->parse_cursor = 0;
__errout:
tfe_stream_detach(stream);
return ACTION_FORWARD_DATA;
}
@@ -158,7 +150,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 = __GET_PME_HC_PRIVATE(pme);
struct http_connection_private * ht_conn = (struct http_connection_private *)(*pme);
if (ht_conn->is_preempted == 0)
{
@@ -197,7 +189,8 @@ __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 = __GET_PME_HC_PRIVATE(pme);
struct http_connection_private * __ht_conn = (struct http_connection_private *)(*pme);
free(__ht_conn);
}
static struct tfe_plugin __http_plugin_info =