197 lines
6.1 KiB
C++
197 lines
6.1 KiB
C++
|
|
#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;
|
||
|
|
}
|
||
|
|
|
||
|
|
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.major_version = 1;
|
||
|
|
__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);
|
||
|
|
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_session_private * hs_private = TAILQ_LAST(&hc_private->hs_private_list, hs_private_list);
|
||
|
|
struct http_half_private * hf_private_request = NULL;
|
||
|
|
int ret = 0;
|
||
|
|
|
||
|
|
/* There is no available in session list,
|
||
|
|
* that indicate all HTTP request has corresponding response */
|
||
|
|
if (hs_private == NULL)
|
||
|
|
{
|
||
|
|
goto __new_session;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* 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->finished)
|
||
|
|
{
|
||
|
|
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);
|
||
|
|
TAILQ_INSERT_TAIL(&hc_private->hs_private_list, hs_private, next);
|
||
|
|
|
||
|
|
__parse:
|
||
|
|
ret = hf_private_parse(hf_private_request, data, len);
|
||
|
|
/* Need more data, no boundary touched */
|
||
|
|
if (ret == 0)
|
||
|
|
{
|
||
|
|
return ACTION_DEFER_DATA;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* 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));
|
||
|
|
|
||
|
|
tfe_stream_detach(stream);
|
||
|
|
return ACTION_FORWARD_DATA;
|
||
|
|
}
|
||
|
|
|
||
|
|
assert(ret == 1);
|
||
|
|
|
||
|
|
/* 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));
|
||
|
|
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)
|
||
|
|
{
|
||
|
|
struct http_half_private * hf_private = hf_private_create(TFE_HTTP_RESPONSE, 1, 0);
|
||
|
|
int ret = hf_private_parse(hf_private, data, len);
|
||
|
|
hf_private_destory(hf_private);
|
||
|
|
return ret;
|
||
|
|
}
|
||
|
|
|
||
|
|
#define HTTP_INDENTIFY_LENGTH 8
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
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 */
|
||
|
|
if (len < HTTP_INDENTIFY_LENGTH)
|
||
|
|
{
|
||
|
|
static const unsigned int __defer_bytes = HTTP_INDENTIFY_LENGTH;
|
||
|
|
tfe_stream_action_set_opt(stream, ACTION_OPT_DEFER_BYTES, (void *) &__defer_bytes, sizeof(__defer_bytes));
|
||
|
|
return ACTION_DEFER_DATA;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* 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;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* 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)
|
||
|
|
{
|
||
|
|
struct http_connection_private * __ht_conn = __GET_PME_HC_PRIVATE(pme);
|
||
|
|
}
|
||
|
|
|
||
|
|
static struct tfe_plugin __http_plugin_info =
|
||
|
|
{
|
||
|
|
.symbol = "HTTP",
|
||
|
|
.type = TFE_PLUGIN_TYPE_PROTOCOL,
|
||
|
|
.proto = APP_PROTO_HTTP1,
|
||
|
|
.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)
|