diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a78fc1..3ad232e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,6 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) add_subdirectory(vendor) add_subdirectory(common) +add_subdirectory(plugin) add_subdirectory(platform) add_subdirectory(cache) -add_subdirectory(plugin) diff --git a/common/include/tfe_http.h b/common/include/tfe_http.h index 3452088..2524895 100644 --- a/common/include/tfe_http.h +++ b/common/include/tfe_http.h @@ -1,55 +1,58 @@ #include -#include #include struct tfe_http_req_spec { int method; - char* uri; - char* host; - char* url; //uri+host - char* accept_encoding; + char * uri; + char * host; + char * url; //uri+host + char * accept_encoding; }; struct tfe_http_resp_spec { int resp_code; - int major_version; //HTTP/1.x or HTTP/ 2 - int minor_version; //HTTP 1.1 or 1.0 - char* content_encoding; + int major_version; //HTTP/1.x or HTTP/ 2 + int minor_version; //HTTP 1.1 or 1.0 + char * content_encoding; }; -#define HTTP_REQUEST 1 -#define HTTP_RESPONSE 2 +enum tfe_http_direction +{ + TFE_HTTP_REQUEST = 1, + TFE_HTTP_RESPONSE = 2 +}; struct tfe_http_half { - int direction; //HTTP_REQUEST or HTTP_RESPONSE + enum tfe_http_direction direction; union { struct tfe_http_req_spec req_spec; struct tfe_http_resp_spec resp_spec; }; + size_t field_cnt; uint64_t cont_len; uint64_t cont_range_from; uint64_t cont_range_to; - struct evbuffer *body; - void * fields; //hide by protocol layer + struct evbuffer * body; + void * fields; //hide by protocol layer }; struct tfe_http_session { int session_sequence;//? int major_version;//1:HTTP 1.x, 2:HTTP 2 - struct tfe_http_half* req; - struct tfe_http_half* resp;//value is NULL before response received. - void* proto_spec; + struct tfe_http_half * req; + struct tfe_http_half * resp;//value is NULL before response received. + void * proto_spec; }; enum tfe_http_std_field { - HTTP_UNKNOWN_FIELD=0, + HTTP_UNKNOWN_FIELD = 0, HTTP_MESSAGE_URL, HTTP_URI, HTTP_HOST, @@ -60,9 +63,9 @@ enum tfe_http_std_field HTTP_AUTHORIZATION, HTTP_LOCATION, HTTP_SERVER, - HTTP_ETAG , - HTTP_DATE , - HTTP_TRAILER , + HTTP_ETAG, + HTTP_DATE, + HTTP_TRAILER, HTTP_TRANSFER_ENCODING, HTTP_VIA, HTTP_PRAGMA, @@ -77,18 +80,18 @@ enum tfe_http_std_field HTTP_CHARSET, HTTP_EXPIRES, HTTP_X_FLASH_VERSION, - HTTP_TRANSFER_LENGTH + HTTP_TRANSFER_LENGTH }; struct http_field_name { enum tfe_http_std_field field_id; - char* field_name; //Non-NULL when field_id isHTTP_UNKNOWN_FIELD. + char * field_name; //Non-NULL when field_id isHTTP_UNKNOWN_FIELD. }; -const char* tfe_http_field_read(const struct tfe_http_half* half, const struct http_field_name* name); -int tfe_http_field_to_range(const char* field_value, long* range_from, long* range_to); -int tfe_http_field_to_digit(const char* field_value, long* digit); +const char * tfe_http_field_read(const struct tfe_http_half * half, const struct http_field_name * name); +int tfe_http_field_to_range(const char * field_value, long * range_from, long * range_to); +int tfe_http_field_to_digit(const char * field_value, long * digit); //******obsolete //long tfe_http_read_num_field(const struct tfe_http_half* half, const struct http_field_name* name); @@ -96,13 +99,13 @@ int tfe_http_field_to_digit(const char* field_value, long* digit); //@Input param interator: NULL to get the first field. //@Output param name: //@return: field value. -const char* tfe_http_field_iterate(const struct tfe_http_half* half, void* interator, struct http_field_name* name); +const char * tfe_http_field_iterate(const struct tfe_http_half * half, void * interator, struct http_field_name * name); -struct tfe_http_half* tfe_http_allow_write(const struct tfe_http_half* half); +struct tfe_http_half * tfe_http_allow_write(const struct tfe_http_half * half); //@param value: NULL for delete //@param name: Could be CHUNK/ENCODING -int tfe_http_field_write(struct tfe_http_half* half, const struct http_field_name* name, const char* value); +int tfe_http_field_write(struct tfe_http_half * half, const struct http_field_name * name, const char * value); //******obsolete @@ -110,7 +113,7 @@ int tfe_http_field_write(struct tfe_http_half* half, const struct http_field_nam enum http_ev_bit_number { - REQ_HDR_BITNUM=0, + REQ_HDR_BITNUM = 0, REQ_BODY_BEGIN_BITNUM, REQ_BODY_CONT_BITNUM, REQ_BODY_END_BITNUM, @@ -119,20 +122,21 @@ enum http_ev_bit_number RESP_BODY_CONT_BITNUM, RESP_BODY_END_BITNUM }; -#define EV_HTTP_REQ_HDR ((uint64_t)1< + +struct tfe_proxy; +struct tfe_thread_ctx; + +typedef int plugin_init_cb_t(struct tfe_proxy * proxy); +typedef void plugin_deinit_cb_t(struct tfe_proxy * proxy); + +//Return 1 for identify as its traffic; +//Return 0 for unknown traffic; +typedef int stream_open_cb_t(const struct tfe_stream * stream, unsigned int thread_id, + enum tfe_conn_dir dir, void ** pme); + +typedef tfe_stream_action stream_data_cb_t(const struct tfe_stream * stream, unsigned int thread_id, + enum tfe_conn_dir dir, const unsigned char * data, size_t len, void ** pme); + +typedef void stream_close_cb_t(const struct tfe_stream * stream, unsigned int thread_id, + enum tfe_stream_close_reason reason, void ** pme); + +enum tfe_plugin_type +{ + TFE_PLUGIN_TYPE_PLATFORM, + TFE_PLUGIN_TYPE_PROTOCOL, + TFE_PLUGIN_TYPE_BUSINESS +}; + +struct tfe_plugin +{ + /* SYMBOL & PROTOCOL */ + const char * symbol; + enum tfe_plugin_type type; + enum tfe_app_proto proto; + + /* PLUGIN INIT & DEINIT */ + plugin_init_cb_t * on_init; + plugin_deinit_cb_t * on_deinit; + + /* PLUGIN STREAM ENTRIES */ + stream_open_cb_t * on_open; + stream_data_cb_t * on_data; + stream_close_cb_t * on_close; +}; + +/* Register plugin */ +int tfe_plugin_register(struct tfe_plugin * plugin); +/* Unregister plugin */ +void tfe_plugin_unregister(struct tfe_plugin * plugin); + +#define TFE_PLUGIN_REGISTER(_symbol, _plugin) \ +static void __attribute__((constructor, used)) __plugin_loader_##_symbol(); \ +static void __plugin_loader_##_symbol() \ +{ \ + _plugin.symbol = #_symbol; \ + tfe_plugin_register(&_plugin); \ +} diff --git a/common/include/tfe_proxy.h b/common/include/tfe_proxy.h new file mode 100644 index 0000000..aaa4ab1 --- /dev/null +++ b/common/include/tfe_proxy.h @@ -0,0 +1,6 @@ +#pragma once + +struct tfe_proxy; + +const char * tfe_proxy_default_conffile(); +const char * tfe_proxy_default_logger(); diff --git a/common/include/tfe_stream.h b/common/include/tfe_stream.h index 1e0176e..6f803e6 100644 --- a/common/include/tfe_stream.h +++ b/common/include/tfe_stream.h @@ -86,17 +86,6 @@ struct tfe_stream_write_ctx * tfe_stream_write_frag_start(const struct tfe_strea int tfe_stream_write_frag(struct tfe_stream_write_ctx * w_ctx, const unsigned char * data, size_t size); void tfe_stream_write_frag_end(struct tfe_stream_write_ctx * w_ctx); -//Return 1 for identify as its traffic; -//Return 0 for unknown traffic; -typedef enum tfe_stream_action stream_open_cb_t(const struct tfe_stream * stream, unsigned int thread_id, - enum tfe_conn_dir dir, const unsigned char * data, size_t len, void ** pme); - -typedef enum tfe_stream_action stream_data_cb_t(const struct tfe_stream * stream, unsigned int thread_id, - enum tfe_conn_dir dir, const unsigned char * data, size_t len, void ** pme); - -typedef void stream_close_cb_t(const struct tfe_stream * stream, unsigned int thread_id, - enum tfe_stream_close_reason reason, void ** pme); - void tfe_stream_detach(const struct tfe_stream * stream); int tfe_stream_preempt(const struct tfe_stream * stream); struct promise * tfe_stream_suspend(const struct tfe_stream * stream); @@ -106,11 +95,4 @@ void tfe_stream_resume(struct promise * promise); int tfe_stream_shutdown(const struct tfe_stream * stream); int tfe_stream_shutdown_dir(const struct tfe_stream * stream, enum tfe_conn_dir dir); -struct tfe_plugin -{ - char symbol[TFE_SYMBOL_MAX]; - enum tfe_app_proto proto; - stream_open_cb_t * on_open; - stream_data_cb_t * on_data; - stream_close_cb_t * on_close; -}; + diff --git a/platform/src/proxy.cpp b/platform/src/proxy.cpp index f0f9208..74c0c6f 100644 --- a/platform/src/proxy.cpp +++ b/platform/src/proxy.cpp @@ -25,6 +25,8 @@ #include #include #include +#include + #include #include #include diff --git a/platform/src/tcp_stream.cpp b/platform/src/tcp_stream.cpp index 55f3090..2da0cc0 100644 --- a/platform/src/tcp_stream.cpp +++ b/platform/src/tcp_stream.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -301,8 +302,7 @@ static void __stream_bev_readcb(struct bufferevent * bev, void * arg) if (_stream->is_plugin_opened == 0) { - action_tmp = plugins[i].on_open(&_stream->head, _stream->thread_ref->thread_id, - dir, contiguous_data, contigous_len, &(plug_ctx->pme)); + plugins[i].on_open(&_stream->head, _stream->thread_ref->thread_id, dir, &(plug_ctx->pme)); _stream->is_plugin_opened = 1; } else diff --git a/plugin/protocol/CMakeLists.txt b/plugin/protocol/CMakeLists.txt index e69de29..962bdfb 100644 --- a/plugin/protocol/CMakeLists.txt +++ b/plugin/protocol/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(http) diff --git a/plugin/protocol/http/CMakeLists.txt b/plugin/protocol/http/CMakeLists.txt new file mode 100644 index 0000000..d52c758 --- /dev/null +++ b/plugin/protocol/http/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_library(http src/http.cpp) +target_link_libraries(http common) +target_link_libraries(http http-parser-static) diff --git a/plugin/protocol/http/src/http.cpp b/plugin/protocol/http/src/http.cpp new file mode 100644 index 0000000..ee06b0a --- /dev/null +++ b/plugin/protocol/http/src/http.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include + +extern "C" +{ +#include +} + +struct http_plugin +{ + +}; + +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; +} + +TAILQ_HEAD(http_session_private_list, http_session_private); +struct http_session_private +{ + TAILQ_ENTRY(http_session_private) s_next; + struct tfe_http_session s_public; +}; + +struct http_connection_private +{ + struct layer_addr * layer_addr; + struct http_session_private_list session_private_list; +}; + +struct http_half_private +{ + struct tfe_http_half head; +}; + +int http_connection_entry_open(const struct tfe_stream * stream, unsigned int thread_id, + enum tfe_conn_dir dir, void ** pme) +{ + return 0; +} + +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) +{ + 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) +{ + +} + +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)