2018-09-07 17:27:23 +08:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <tfe_http.h>
|
2018-09-12 15:29:35 +08:00
|
|
|
#include <tfe_plugin.h>
|
2018-09-07 17:27:23 +08:00
|
|
|
|
2018-09-18 18:48:21 +08:00
|
|
|
static const char * __str_std_header_field_map[] =
|
|
|
|
|
{
|
|
|
|
|
[TFE_HTTP_UNKNOWN_FIELD] = NULL,
|
|
|
|
|
[TFE_HTTP_HOST] = "Host",
|
|
|
|
|
[TFE_HTTP_REFERER] = "Referer",
|
|
|
|
|
[TFE_HTTP_USER_AGENT] = "User-Agent",
|
|
|
|
|
[TFE_HTTP_COOKIE] = "Cookie",
|
|
|
|
|
[TFE_HTTP_PROXY_AUTHORIZATION] = "Proxy-Authorization",
|
|
|
|
|
[TFE_HTTP_AUTHORIZATION] = "Authorization",
|
|
|
|
|
[TFE_HTTP_LOCATION] = "Location",
|
|
|
|
|
[TFE_HTTP_SERVER] = "Server",
|
|
|
|
|
[TFE_HTTP_ETAG] = "Etag",
|
|
|
|
|
[TFE_HTTP_DATE] = "Date",
|
|
|
|
|
[TFE_HTTP_TRAILER] = "Trailer",
|
|
|
|
|
[TFE_HTTP_TRANSFER_ENCODING] = "Transfer-Encoding",
|
|
|
|
|
[TFE_HTTP_VIA] = "Via",
|
|
|
|
|
[TFE_HTTP_PRAGMA] = "Pragma",
|
|
|
|
|
[TFE_HTTP_CONNECTION] = "Connection",
|
|
|
|
|
[TFE_HTTP_CONT_ENCODING] = "Content-Encoding",
|
|
|
|
|
[TFE_HTTP_CONT_LANGUAGE] = "Content-Language",
|
|
|
|
|
[TFE_HTTP_CONT_LOCATION] = "Content-Location",
|
|
|
|
|
[TFE_HTTP_CONT_RANGE] = "Content-Range",
|
|
|
|
|
[TFE_HTTP_CONT_LENGTH] = "Content-Length",
|
|
|
|
|
[TFE_HTTP_CONT_TYPE] = "Content-Type",
|
|
|
|
|
[TFE_HTTP_CONT_DISPOSITION] = "Content-Disposition",
|
|
|
|
|
[TFE_HTTP_EXPIRES] = "Expires",
|
2018-10-14 14:29:23 +08:00
|
|
|
[TFE_HTTP_ACCEPT_ENCODING] = "Accept-Encoding",
|
|
|
|
|
[TFE_HTTP_CACHE_CONTROL] = "Cache-Control",
|
|
|
|
|
[TFE_HTTP_IF_MATCH] = "If-Match",
|
|
|
|
|
[TFE_HTTP_IF_NONE_MATCH] = "If-None-Match",
|
|
|
|
|
[TFE_HTTP_IF_MODIFIED_SINCE] = "If-Modified-Since",
|
|
|
|
|
[TFE_HTTP_IF_UNMODIFIED_SINCE] = "If-Unmodified-Since",
|
|
|
|
|
[TFE_HTTP_LAST_MODIFIED] = "Last-Modified"
|
2018-09-18 18:48:21 +08:00
|
|
|
};
|
|
|
|
|
|
2018-09-25 10:17:50 +08:00
|
|
|
static const char * __str_std_method_map[1024] = {};
|
|
|
|
|
void __str_std_method_map_init() __attribute__((constructor, used));
|
|
|
|
|
void __str_std_method_map_init()
|
2018-09-21 15:00:54 +08:00
|
|
|
{
|
2018-09-25 10:17:50 +08:00
|
|
|
#define XX(num, name, string) __str_std_method_map[TFE_HTTP_METHOD_##name] = #string;
|
|
|
|
|
__HTTP_METHOD_MAP(XX)
|
2018-09-21 15:00:54 +08:00
|
|
|
#undef XX
|
2018-09-25 10:17:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char * __str_std_status_map[1024] = {};
|
|
|
|
|
void __str_std_status_map_init() __attribute__((constructor, used));
|
|
|
|
|
void __str_std_status_map_init()
|
|
|
|
|
{
|
|
|
|
|
#define XX(num, name, string) __str_std_status_map[TFE_HTTP_STATUS_##name] = #string;
|
|
|
|
|
__HTTP_STATUS_MAP(XX)
|
|
|
|
|
#undef XX
|
|
|
|
|
}
|
2018-09-21 15:00:54 +08:00
|
|
|
|
|
|
|
|
const char * http_std_method_to_string(enum tfe_http_std_method method)
|
|
|
|
|
{
|
|
|
|
|
return __str_std_method_map[method];
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 10:17:50 +08:00
|
|
|
const char * http_std_status_to_string(enum tfe_http_std_status status)
|
|
|
|
|
{
|
|
|
|
|
return __str_std_status_map[status];
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-18 18:48:21 +08:00
|
|
|
struct http_field_name * http_field_name_duplicate(const struct http_field_name * orig)
|
|
|
|
|
{
|
|
|
|
|
struct http_field_name * __duplicated = ALLOC(struct http_field_name, 1);
|
|
|
|
|
assert(__duplicated != NULL);
|
|
|
|
|
|
|
|
|
|
if (orig->field_id == TFE_HTTP_UNKNOWN_FIELD)
|
|
|
|
|
{
|
|
|
|
|
__duplicated->field_id = TFE_HTTP_UNKNOWN_FIELD;
|
|
|
|
|
__duplicated->field_name = tfe_strdup(orig->field_name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
__duplicated->field_id = orig->field_id;
|
|
|
|
|
__duplicated->field_name = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return __duplicated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int http_field_name_compare(const struct http_field_name * lvalue, const struct http_field_name * rvalue)
|
|
|
|
|
{
|
|
|
|
|
if (lvalue->field_id != rvalue->field_id)
|
|
|
|
|
{
|
|
|
|
|
return (lvalue->field_id - rvalue->field_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* unknown field, compare field_name in string */
|
|
|
|
|
if (lvalue->field_id == TFE_HTTP_UNKNOWN_FIELD)
|
|
|
|
|
{
|
|
|
|
|
return strcasecmp(lvalue->field_name, rvalue->field_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* field_id is equal, but not unknown, hit */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct http_field_name * http_field_construct_from_string(const char * str_field)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char * http_field_to_string(const struct http_field_name * field)
|
|
|
|
|
{
|
|
|
|
|
if (field->field_id != TFE_HTTP_UNKNOWN_FIELD) return __str_std_header_field_map[field->field_id];
|
|
|
|
|
return field->field_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_field_destory(struct http_field_name * field)
|
|
|
|
|
{
|
|
|
|
|
free(field);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_frame_session_ctx
|
2018-09-07 17:27:23 +08:00
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_frame_plugin_status * plugin_status;
|
|
|
|
|
unsigned int nr_plugin_status;
|
|
|
|
|
|
|
|
|
|
struct tfe_plugin * calling_plugin;
|
|
|
|
|
struct http_frame_plugin_status * calling_plugin_status;
|
2018-09-12 15:29:35 +08:00
|
|
|
};
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_frame_plugin_status * http_frame_current_plugin_status(struct http_frame_session_ctx * ht_frame)
|
|
|
|
|
{
|
|
|
|
|
return ht_frame->calling_plugin_status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct http_frame_session_ctx * http_frame_raise_session_begin(const struct tfe_stream * stream,
|
2018-09-12 15:29:35 +08:00
|
|
|
struct tfe_http_session * ht_session, unsigned int thread_id)
|
|
|
|
|
{
|
2018-09-17 15:44:44 +08:00
|
|
|
struct http_frame_session_ctx * ht_frame = ALLOC(struct http_frame_session_ctx, 1);
|
|
|
|
|
ht_frame->nr_plugin_status = tfe_plugin_total_counts();
|
|
|
|
|
ht_frame->plugin_status = ALLOC(struct http_frame_plugin_status, ht_frame->nr_plugin_status);
|
2018-09-12 15:29:35 +08:00
|
|
|
|
|
|
|
|
unsigned int __for_each_iterator = 0;
|
|
|
|
|
unsigned int __plugin_id = 0;
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
struct tfe_plugin * plugin_info_iter;
|
2018-09-12 15:29:35 +08:00
|
|
|
TFE_PLUGIN_FOREACH(plugin_info_iter, &__for_each_iterator)
|
|
|
|
|
{
|
|
|
|
|
if (plugin_info_iter->on_session_begin == NULL) continue;
|
2018-09-17 15:44:44 +08:00
|
|
|
|
|
|
|
|
/* Calling ctx, in callback can fetch by calling frame_plugin_status_get_XXX */
|
|
|
|
|
ht_frame->calling_plugin = plugin_info_iter;
|
|
|
|
|
ht_frame->calling_plugin_status = &ht_frame->plugin_status[__plugin_id];
|
|
|
|
|
|
|
|
|
|
/* Call session begin */
|
|
|
|
|
void ** calling_pme = &ht_frame->calling_plugin_status->pme;
|
|
|
|
|
plugin_info_iter->on_session_begin(stream, ht_session, thread_id, calling_pme);
|
2018-09-12 15:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
/* Clear calling ctx */
|
|
|
|
|
ht_frame->calling_plugin = NULL;
|
|
|
|
|
ht_frame->calling_plugin_status = NULL;
|
|
|
|
|
return ht_frame;
|
2018-09-12 15:29:35 +08:00
|
|
|
};
|
|
|
|
|
|
2018-09-23 17:33:05 +08:00
|
|
|
void http_frame_raise_session_end(struct http_frame_session_ctx * ht_frame, const tfe_stream * stream,
|
2018-09-12 15:29:35 +08:00
|
|
|
struct tfe_http_session * ht_session, unsigned int thread_id)
|
|
|
|
|
{
|
|
|
|
|
unsigned int __for_each_iterator = 0;
|
|
|
|
|
unsigned int __plugin_id = 0;
|
|
|
|
|
struct tfe_plugin * plugin_info_iter;
|
|
|
|
|
|
|
|
|
|
TFE_PLUGIN_FOREACH(plugin_info_iter, &__for_each_iterator)
|
|
|
|
|
{
|
|
|
|
|
if (plugin_info_iter->on_session_end == NULL) continue;
|
2018-09-17 15:44:44 +08:00
|
|
|
|
|
|
|
|
/* Calling ctx, in callback can fetch by calling frame_plugin_status_get_XXX */
|
|
|
|
|
ht_frame->calling_plugin = plugin_info_iter;
|
|
|
|
|
ht_frame->calling_plugin_status = &ht_frame->plugin_status[__plugin_id];
|
|
|
|
|
|
|
|
|
|
/* Call session end */
|
|
|
|
|
void ** calling_pme = &ht_frame->calling_plugin_status->pme;
|
|
|
|
|
plugin_info_iter->on_session_end(stream, ht_session, thread_id, calling_pme);
|
2018-09-12 15:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
ht_frame->calling_plugin = NULL;
|
|
|
|
|
ht_frame->calling_plugin_status = NULL;
|
|
|
|
|
|
|
|
|
|
free(ht_frame->plugin_status);
|
|
|
|
|
free(ht_frame);
|
2018-09-07 17:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
void http_frame_raise_event(struct http_frame_session_ctx * ht_frame,
|
2018-09-12 15:29:35 +08:00
|
|
|
const struct tfe_stream * stream, struct tfe_http_session * ht_session, enum tfe_http_event event,
|
|
|
|
|
const unsigned char * data, size_t datalen, unsigned int thread_id)
|
2018-09-07 17:27:23 +08:00
|
|
|
{
|
2018-09-12 15:29:35 +08:00
|
|
|
unsigned int __for_each_iterator = 0;
|
|
|
|
|
unsigned int __plugin_id = 0;
|
|
|
|
|
|
|
|
|
|
struct tfe_plugin * plugin_info_iter;
|
|
|
|
|
TFE_PLUGIN_FOREACH(plugin_info_iter, &__for_each_iterator)
|
|
|
|
|
{
|
2018-09-25 10:17:50 +08:00
|
|
|
if (plugin_info_iter->on_session_data == NULL)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-09-12 15:29:35 +08:00
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
/* Calling ctx, in callback can fetch by calling frame_plugin_status_get_XXX */
|
|
|
|
|
ht_frame->calling_plugin = plugin_info_iter;
|
|
|
|
|
ht_frame->calling_plugin_status = &ht_frame->plugin_status[__plugin_id];
|
|
|
|
|
|
2018-09-25 10:17:50 +08:00
|
|
|
if (ht_frame->calling_plugin_status->detached)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 15:44:44 +08:00
|
|
|
void ** calling_pme = &ht_frame->calling_plugin_status->pme;
|
|
|
|
|
plugin_info_iter->on_session_data(stream, ht_session, event, data, datalen, thread_id, calling_pme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ht_frame->calling_plugin = NULL;
|
|
|
|
|
ht_frame->calling_plugin_status = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_frame_currect_plugin_detach(struct http_frame_session_ctx * ht_frame)
|
|
|
|
|
{
|
|
|
|
|
ht_frame->calling_plugin_status->detached = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int http_frame_currect_plugin_preempt(struct http_frame_session_ctx * ht_frame)
|
|
|
|
|
{
|
|
|
|
|
for(unsigned int i = 0; i < ht_frame->nr_plugin_status; i++)
|
|
|
|
|
{
|
|
|
|
|
struct http_frame_plugin_status * __plugin_status = &ht_frame->plugin_status[i];
|
2018-09-25 20:32:24 +08:00
|
|
|
if (__plugin_status->is_preempted) return -1;
|
2018-09-12 15:29:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-25 20:32:24 +08:00
|
|
|
//TODO:
|
|
|
|
|
//assert(ht_frame->calling_plugin_status != NULL);
|
|
|
|
|
//ht_frame->calling_plugin_status->is_preempted = 1;
|
2018-09-17 15:44:44 +08:00
|
|
|
return 0;
|
2018-09-07 17:27:23 +08:00
|
|
|
}
|