2018-09-06 10:12:08 +08:00
|
|
|
#include <MESA/MESA_list_queue.h>
|
|
|
|
|
#include <tfe_stream.h>
|
|
|
|
|
#include <tfe_utils.h>
|
|
|
|
|
#include <tfe_plugin.h>
|
|
|
|
|
#include <tfe_stream.h>
|
|
|
|
|
#include <http_parser.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
|
|
#include <http_common.h>
|
|
|
|
|
#include <http_half.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
struct http_plugin __g_http_plugin;
|
|
|
|
|
struct http_plugin * g_http_plugin = &__g_http_plugin;
|
|
|
|
|
|
|
|
|
|
int http_plugin_init(struct tfe_proxy * proxy)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_plugin_deinit(struct tfe_proxy * proxy)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
int http_connection_entry_open(const struct tfe_stream * stream, unsigned int thread_id,
|
|
|
|
|
enum tfe_conn_dir dir, void ** pme)
|
2018-09-06 10:12:08 +08:00
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_connection_private * ht_conn = ALLOC(struct http_connection_private, 1);
|
|
|
|
|
TAILQ_INIT(&ht_conn->hs_private_list);
|
|
|
|
|
*pme = (void *)ht_conn;
|
|
|
|
|
return 0;
|
2018-09-06 10:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
struct user_event_dispatch_closure
|
2018-09-06 10:12:08 +08:00
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
const struct tfe_stream * stream;
|
|
|
|
|
const struct tfe_http_session * session;
|
|
|
|
|
unsigned int thread_id;
|
|
|
|
|
};
|
2018-09-06 10:12:08 +08:00
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
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)
|
2018-09-06 10:12:08 +08:00
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
struct user_event_dispatch_closure * __closure = (struct user_event_dispatch_closure *)user;
|
|
|
|
|
struct http_frame_session_ctx* ht_frame = hf_private->session->ht_frame;
|
2018-09-18 11:15:25 +08:00
|
|
|
//todo:
|
|
|
|
|
http_frame_raise_event(ht_frame, __closure->stream, (struct tfe_http_session *)__closure->session,
|
2018-09-17 15:44:44 +08:00
|
|
|
ev, data, len, __closure->thread_id); return 0;
|
2018-09-06 10:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum tfe_stream_action __http_connection_entry_on_request(const struct tfe_stream * stream,
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_connection_private * hc_private, unsigned int thread_id, const unsigned char * data, size_t len)
|
2018-09-06 10:12:08 +08:00
|
|
|
{
|
|
|
|
|
struct http_session_private * hs_private = TAILQ_LAST(&hc_private->hs_private_list, hs_private_list);
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_half_private * hf_private_request = to_hf_request_private(hs_private);
|
2018-09-07 17:27:23 +08:00
|
|
|
|
|
|
|
|
/* tfe_hexdump(stderr, __FUNCTION__, data, (unsigned int)len); */
|
2018-09-06 10:12:08 +08:00
|
|
|
int ret = 0;
|
2018-09-17 15:44:44 +08:00
|
|
|
size_t __action_byptes;
|
2018-09-06 10:12:08 +08:00
|
|
|
|
|
|
|
|
/* There is no available in session list,
|
2018-09-17 15:44:44 +08:00
|
|
|
* 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)
|
2018-09-06 10:12:08 +08:00
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
/* 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);
|
2018-09-18 18:50:25 +08:00
|
|
|
hf_private_set_session(hf_private_request, hs_private);
|
2018-09-17 15:44:44 +08:00
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
}
|
2018-09-12 15:29:35 +08:00
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
TAILQ_INSERT_TAIL(&hc_private->hs_private_list, hs_private, next);
|
2018-09-12 15:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
/* proceed parse content for last request */
|
2018-09-06 10:12:08 +08:00
|
|
|
ret = hf_private_parse(hf_private_request, data, len);
|
2018-09-17 15:44:44 +08:00
|
|
|
|
2018-09-06 10:12:08 +08:00
|
|
|
/* Need more data, no boundary touched */
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
return hf_private_request->stream_action;
|
2018-09-06 10:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
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));
|
2018-09-06 10:12:08 +08:00
|
|
|
return ACTION_FORWARD_DATA;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
if (hf_private_request->stream_action == ACTION_DROP_DATA)
|
2018-09-07 17:27:23 +08:00
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
tfe_stream_action_set_opt(stream, ACTION_OPT_DROP_BYTES, &__action_byptes, sizeof(__action_byptes));
|
|
|
|
|
return ACTION_DROP_DATA;
|
2018-09-07 17:27:23 +08:00
|
|
|
}
|
2018-09-06 10:12:08 +08:00
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
goto __errout;
|
2018-09-07 17:27:23 +08:00
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
__errout:
|
|
|
|
|
tfe_stream_detach(stream);
|
2018-09-06 10:12:08 +08:00
|
|
|
return ACTION_FORWARD_DATA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
return ACTION_FORWARD_DATA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int __http_connection_identify(const struct tfe_stream * stream,
|
|
|
|
|
struct http_connection_private * ht_conn, const unsigned char * data, size_t len)
|
|
|
|
|
{
|
2018-09-18 18:50:25 +08:00
|
|
|
struct http_half_private * hf_private = hf_private_create(TFE_HTTP_REQUEST, 1, 0);
|
2018-09-06 10:12:08 +08:00
|
|
|
int ret = hf_private_parse(hf_private, data, len);
|
|
|
|
|
hf_private_destory(hf_private);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-18 18:50:25 +08:00
|
|
|
#define HTTP_INDENTIFY_LENGTH 4
|
2018-09-06 10:12:08 +08:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_connection_private * ht_conn = (struct http_connection_private *)(*pme);
|
2018-09-06 10:12:08 +08:00
|
|
|
|
|
|
|
|
if (ht_conn->is_preempted == 0)
|
|
|
|
|
{
|
|
|
|
|
/* If the server push response before client send request, this must not be HTTP, detach the stream */
|
|
|
|
|
if (dir == CONN_DIR_UPSTREAM)
|
|
|
|
|
goto __detach;
|
|
|
|
|
|
|
|
|
|
/* Protocol Identification, we need 8 bytes at least to tell it is HTTP or not */
|
2018-09-18 18:50:25 +08:00
|
|
|
if (len < HTTP_INDENTIFY_LENGTH) goto __detach;
|
2018-09-06 10:12:08 +08:00
|
|
|
|
|
|
|
|
/* Now, we want to identify this stream */
|
|
|
|
|
int ret = __http_connection_identify(stream, ht_conn, data, len);
|
|
|
|
|
if (ret != 0) goto __detach;
|
|
|
|
|
|
|
|
|
|
/* This is HTTP, try to preempt the stream
|
|
|
|
|
* It may be failed because other plugin preempted before us */
|
|
|
|
|
ret = tfe_stream_preempt(stream);
|
|
|
|
|
if (ret != 0) goto __detach;
|
2018-09-07 17:27:23 +08:00
|
|
|
ht_conn->is_preempted = 1;
|
2018-09-06 10:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This stream has been preempt, this plugin try to parse it */
|
|
|
|
|
return (dir == CONN_DIR_DOWNSTREAM) ? __http_connection_entry_on_request(stream, ht_conn, thread_id,
|
|
|
|
|
data, len) : __http_connection_entry_on_response(stream, ht_conn, thread_id, data, len);
|
|
|
|
|
|
|
|
|
|
__detach:
|
|
|
|
|
tfe_stream_detach(stream);
|
|
|
|
|
return ACTION_FORWARD_DATA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_connection_entry_close(const struct tfe_stream * stream, unsigned int thread_id,
|
|
|
|
|
enum tfe_stream_close_reason reason, void ** pme)
|
|
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_connection_private * __ht_conn = (struct http_connection_private *)(*pme);
|
|
|
|
|
free(__ht_conn);
|
2018-09-06 10:12:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct tfe_plugin __http_plugin_info =
|
2018-09-07 17:27:23 +08:00
|
|
|
{
|
|
|
|
|
.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
|
|
|
|
|
};
|
2018-09-06 10:12:08 +08:00
|
|
|
|
|
|
|
|
TFE_PLUGIN_REGISTER(HTTP, __http_plugin_info)
|