初步调通HTTP重定向业务
* 增加HTTP Status标准化定义及辅助函数; * 增加HTTP解析层发送应答的功能 * 修正了Pangu HTTP实现导致段错误的一系列问题。
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include <http_common.h>
|
||||
#include <http_half.h>
|
||||
#include <assert.h>
|
||||
#include <event.h>
|
||||
|
||||
struct http_plugin __g_http_plugin;
|
||||
struct http_plugin * g_http_plugin = &__g_http_plugin;
|
||||
@@ -143,8 +144,11 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
|
||||
struct http_connection_private * hc_private, unsigned int thread_id, const unsigned char * data, size_t len)
|
||||
{
|
||||
struct http_session_private * hs_private = TAILQ_FIRST(&hc_private->hs_private_list);
|
||||
struct http_half_private * hf_private_response = to_hf_response_private(hs_private);
|
||||
struct http_half_private * hf_private_resp_in;
|
||||
struct http_half_private * hf_private_resp_user;
|
||||
|
||||
int ret = 0;
|
||||
size_t __action_byptes;
|
||||
|
||||
/* Standalone response, it means missing something or malformed http protocol */
|
||||
if (hs_private == NULL)
|
||||
@@ -153,44 +157,71 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
|
||||
goto __errout;
|
||||
}
|
||||
|
||||
hf_private_resp_in = to_hf_response_private(hs_private);
|
||||
hf_private_resp_user = hs_private->hf_private_resp_user;
|
||||
|
||||
/* First time parse http response */
|
||||
if (hf_private_response == NULL)
|
||||
if (hf_private_resp_in == NULL)
|
||||
{
|
||||
/* HTTP Version */
|
||||
short resp_major = to_hf_request_private(hs_private)->major;
|
||||
short resp_minor = to_hf_request_private(hs_private)->minor;
|
||||
|
||||
/* Response */
|
||||
hf_private_response = hf_private_create(TFE_HTTP_RESPONSE, resp_major, resp_minor);
|
||||
hs_private_hf_private_set(hs_private, hf_private_response, TFE_HTTP_RESPONSE);
|
||||
hf_private_set_session(hf_private_response, hs_private);
|
||||
hf_private_resp_in = hf_private_create(TFE_HTTP_RESPONSE, resp_major, resp_minor);
|
||||
hs_private_hf_private_set(hs_private, hf_private_resp_in, TFE_HTTP_RESPONSE);
|
||||
hf_private_set_session(hf_private_resp_in, hs_private);
|
||||
|
||||
/* 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);
|
||||
if (hf_private_resp_user != NULL)
|
||||
{
|
||||
/* Set nothing callback, dont call user callback because the data need to be droped */
|
||||
hf_private_set_callback(hf_private_resp_in, NULL, NULL, NULL);
|
||||
/* Drop all data, because the user's response need to be send */
|
||||
hf_private_resp_in->stream_action = ACTION_DROP_DATA;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 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_response, __user_event_dispatch, __closure, free);
|
||||
/* Set callback, this callback used to raise business event */
|
||||
hf_private_set_callback(hf_private_resp_in, __user_event_dispatch, __closure, free);
|
||||
/* Inherit user stream action, this action can affact session's behavior */
|
||||
hf_private_resp_in->user_stream_action = to_hf_request_private(hs_private)->user_stream_action;
|
||||
}
|
||||
}
|
||||
|
||||
/* Inherit user stream action, this action can affact session's behavior */
|
||||
hf_private_response->user_stream_action = to_hf_request_private(hs_private)->user_stream_action;
|
||||
if (hf_private_resp_user != NULL)
|
||||
{
|
||||
/* Construct, and write response immediately */
|
||||
hf_private_construct(hf_private_resp_user);
|
||||
size_t __to_write_len = evbuffer_get_length(hf_private_resp_user->evbuf_raw);
|
||||
unsigned char * __to_write = evbuffer_pullup(hf_private_resp_user->evbuf_raw, __to_write_len);
|
||||
|
||||
/* Write the data to stream, UPSTREAM is the incoming direction for response */
|
||||
ret = tfe_stream_write(stream, CONN_DIR_DOWNSTREAM, __to_write, __to_write_len);
|
||||
if (unlikely(ret < 0)) { assert(0); }
|
||||
|
||||
hf_private_destory(hf_private_resp_user);
|
||||
hs_private->hf_private_resp_user = NULL;
|
||||
}
|
||||
|
||||
/* Parse the content, the data which in defered state has been ignored. */
|
||||
ret = hf_private_parse(hf_private_response, data, len);
|
||||
ret = hf_private_parse(hf_private_resp_in, data, len);
|
||||
|
||||
/* Need more data, no boundary touched */
|
||||
if (ret == 0)
|
||||
{
|
||||
if (hf_private_response->stream_action == ACTION_DROP_DATA ||
|
||||
hf_private_response->stream_action == ACTION_FORWARD_DATA)
|
||||
if (hf_private_resp_in->stream_action == ACTION_DROP_DATA ||
|
||||
hf_private_resp_in->stream_action == ACTION_FORWARD_DATA)
|
||||
{
|
||||
hf_private_response->parse_cursor = 0;
|
||||
hf_private_resp_in->parse_cursor = 0;
|
||||
}
|
||||
|
||||
return hf_private_response->stream_action;
|
||||
return hf_private_resp_in->stream_action;
|
||||
}
|
||||
|
||||
/* Some kind of error happened, write log and detach the stream */
|
||||
@@ -203,13 +234,30 @@ enum tfe_stream_action __http_connection_entry_on_response(const struct tfe_stre
|
||||
goto __errout;
|
||||
}
|
||||
|
||||
if (hf_private_response->message_status == STATUS_COMPLETE)
|
||||
if (hf_private_resp_in->message_status == STATUS_COMPLETE)
|
||||
{
|
||||
http_frame_raise_session_end(hs_private->ht_frame, stream, &hs_private->hs_public, thread_id);
|
||||
TAILQ_REMOVE(&hc_private->hs_private_list, hs_private, next);
|
||||
hs_private_destory(hs_private);
|
||||
}
|
||||
|
||||
__action_byptes = hf_private_resp_in->parse_cursor;
|
||||
hf_private_resp_in->parse_cursor = 0;
|
||||
|
||||
if (hf_private_resp_in->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_resp_in->stream_action == ACTION_DROP_DATA)
|
||||
{
|
||||
tfe_stream_action_set_opt(stream, ACTION_OPT_DROP_BYTES, &__action_byptes, sizeof(__action_byptes));
|
||||
return ACTION_DROP_DATA;
|
||||
}
|
||||
|
||||
goto __errout;
|
||||
|
||||
__errout:
|
||||
tfe_stream_detach(stream);
|
||||
return ACTION_FORWARD_DATA;
|
||||
|
||||
@@ -121,10 +121,10 @@ void __hf_public_req_fill_from_private(struct http_half_private * hf_private, st
|
||||
/* accept-encoding, host is located in header's K-V structure */
|
||||
hf_req_spec->method = (enum tfe_http_std_method) parser->method;
|
||||
const static struct http_field_name __host_field_name =
|
||||
{
|
||||
.field_id = TFE_HTTP_HOST,
|
||||
.field_name = NULL
|
||||
};
|
||||
{
|
||||
.field_id = TFE_HTTP_HOST,
|
||||
.field_name = NULL
|
||||
};
|
||||
|
||||
hf_req_spec->host = (char *) tfe_http_field_read(hf_public, &__host_field_name);
|
||||
|
||||
@@ -158,28 +158,28 @@ void __hf_public_resp_fill_from_private(struct http_half_private * hf_private, s
|
||||
|
||||
/* Content Type */
|
||||
const static struct http_field_name __cont_encoding_type_name =
|
||||
{
|
||||
.field_id = TFE_HTTP_CONT_TYPE,
|
||||
.field_name = NULL
|
||||
};
|
||||
{
|
||||
.field_id = TFE_HTTP_CONT_TYPE,
|
||||
.field_name = NULL
|
||||
};
|
||||
|
||||
hf_resp_spec->content_type = (char *) tfe_http_field_read(hf_public, &__cont_encoding_type_name);
|
||||
|
||||
/* Content Length */
|
||||
const static struct http_field_name __cont_encoding_length_name =
|
||||
{
|
||||
.field_id = TFE_HTTP_CONT_LENGTH,
|
||||
.field_name = NULL
|
||||
};
|
||||
{
|
||||
.field_id = TFE_HTTP_CONT_LENGTH,
|
||||
.field_name = NULL
|
||||
};
|
||||
|
||||
hf_resp_spec->content_length = (char *) tfe_http_field_read(hf_public, &__cont_encoding_length_name);
|
||||
|
||||
/* Content Encoding */
|
||||
const static struct http_field_name __cont_encoding_field_name =
|
||||
{
|
||||
.field_id = TFE_HTTP_CONT_ENCODING,
|
||||
.field_name = NULL
|
||||
};
|
||||
{
|
||||
.field_id = TFE_HTTP_CONT_ENCODING,
|
||||
.field_name = NULL
|
||||
};
|
||||
|
||||
hf_resp_spec->content_encoding = (char *) tfe_http_field_read(hf_public, &__cont_encoding_field_name);
|
||||
}
|
||||
@@ -251,6 +251,13 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
|
||||
hf_public->major_version = parser->http_major;
|
||||
hf_public->minor_version = parser->http_minor;
|
||||
|
||||
/* Copy version to session */
|
||||
if(hf_private->session != NULL)
|
||||
{
|
||||
to_hs_public(hf_private->session)->major_version = hf_public->major_version;
|
||||
to_hs_public(hf_private->session)->minor_version = hf_public->minor_version;
|
||||
}
|
||||
|
||||
if (hf_direction == TFE_HTTP_REQUEST)
|
||||
{
|
||||
__hf_public_req_fill_from_private(hf_private, parser);
|
||||
@@ -439,14 +446,14 @@ void hf_ops_free(struct tfe_http_half * half)
|
||||
}
|
||||
|
||||
struct tfe_http_half_ops __http_half_ops =
|
||||
{
|
||||
.ops_http_field_read = hf_ops_field_read,
|
||||
.ops_http_field_write = hf_ops_field_write,
|
||||
.ops_http_allow_write = hf_ops_allow_write,
|
||||
.ops_http_field_iterate = hf_ops_field_iterate,
|
||||
.ops_append_body = hf_ops_append_body,
|
||||
.ops_free = hf_ops_free
|
||||
};
|
||||
{
|
||||
.ops_http_field_read = hf_ops_field_read,
|
||||
.ops_http_field_write = hf_ops_field_write,
|
||||
.ops_http_allow_write = hf_ops_allow_write,
|
||||
.ops_http_field_iterate = hf_ops_field_iterate,
|
||||
.ops_append_body = hf_ops_append_body,
|
||||
.ops_free = hf_ops_free
|
||||
};
|
||||
|
||||
struct http_half_private * hf_private_create(tfe_http_direction ht_dir, short major, short minor)
|
||||
{
|
||||
@@ -526,13 +533,13 @@ int hf_private_parse(struct http_half_private * hf_private, const unsigned char
|
||||
|
||||
static struct tfe_http_session * hs_ops_allow_write(const struct tfe_http_session * session)
|
||||
{
|
||||
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *)session);
|
||||
return http_frame_currect_plugin_preempt(hs_private->ht_frame) ? (struct tfe_http_session *)session : NULL;
|
||||
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *) session);
|
||||
return http_frame_currect_plugin_preempt(hs_private->ht_frame) == 0 ? (struct tfe_http_session *) session : NULL;
|
||||
}
|
||||
|
||||
void hs_ops_detach(const struct tfe_http_session * session)
|
||||
{
|
||||
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *)session);
|
||||
struct http_session_private * hs_private = to_hs_private((struct tfe_http_session *) session);
|
||||
return http_frame_currect_plugin_detach(hs_private->ht_frame);
|
||||
}
|
||||
|
||||
@@ -543,35 +550,108 @@ void hs_ops_drop(struct tfe_http_session * session)
|
||||
|
||||
void hs_ops_request_set(struct tfe_http_session * session, struct tfe_http_half * req)
|
||||
{
|
||||
return;
|
||||
struct http_half_private * hf_private = to_hf_private(req);
|
||||
struct http_session_private * hs_private = to_hs_private(session);
|
||||
|
||||
assert(hs_private->hf_private_req_user != NULL);
|
||||
hs_private->hf_private_req_user = hf_private;
|
||||
}
|
||||
|
||||
void hs_ops_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
|
||||
{
|
||||
return;
|
||||
struct http_half_private * hf_private = to_hf_private(resp);
|
||||
struct http_session_private * hs_private = to_hs_private(session);
|
||||
|
||||
assert(hs_private->hf_private_resp_user == NULL);
|
||||
hs_private->hf_private_resp_user = hf_private;
|
||||
}
|
||||
|
||||
struct tfe_http_half * hs_ops_request_create(struct tfe_http_session * session,
|
||||
enum tfe_http_std_method method, const char * uri)
|
||||
{
|
||||
return NULL;
|
||||
struct http_half_private * hf_req_private = hf_private_create(TFE_HTTP_REQUEST,
|
||||
session->major_version, session->minor_version);
|
||||
|
||||
hf_req_private->method_or_status = method;
|
||||
hf_req_private->url_storage = tfe_strdup(uri);
|
||||
hf_req_private->is_setup_by_user = true;
|
||||
return to_hf_public(hf_req_private);
|
||||
}
|
||||
|
||||
struct tfe_http_half * hs_ops_response_create(struct tfe_http_session * session, int resp_code)
|
||||
{
|
||||
return NULL;
|
||||
struct http_half_private * hf_resp_private = hf_private_create(TFE_HTTP_RESPONSE,
|
||||
session->major_version, session->minor_version);
|
||||
|
||||
hf_resp_private->method_or_status = resp_code;
|
||||
hf_resp_private->is_setup_by_user = true;
|
||||
return to_hf_public(hf_resp_private);
|
||||
}
|
||||
|
||||
struct tfe_http_session_ops __http_session_ops =
|
||||
{
|
||||
.ops_allow_write = hs_ops_allow_write,
|
||||
.ops_detach = hs_ops_detach,
|
||||
.ops_drop = hs_ops_drop,
|
||||
.ops_request_set = hs_ops_request_set,
|
||||
.ops_response_set = hs_ops_response_set,
|
||||
.ops_request_create = hs_ops_request_create,
|
||||
.ops_response_create = hs_ops_response_create
|
||||
};
|
||||
|
||||
void __construct_request_line(struct http_half_private * hf_private)
|
||||
{
|
||||
.ops_allow_write = hs_ops_allow_write,
|
||||
.ops_detach = hs_ops_detach,
|
||||
.ops_drop = hs_ops_drop,
|
||||
.ops_request_set = hs_ops_request_set,
|
||||
.ops_response_set = hs_ops_response_set,
|
||||
.ops_request_create = hs_ops_request_create,
|
||||
.ops_response_create = hs_ops_response_create
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void __construct_response_line(struct http_half_private * hf_private)
|
||||
{
|
||||
enum tfe_http_std_status __resp_code = (enum tfe_http_std_status) hf_private->method_or_status;
|
||||
const char * __str_resp_code = http_std_status_to_string(__resp_code);
|
||||
if (__str_resp_code == NULL)
|
||||
{
|
||||
__str_resp_code = "";
|
||||
}
|
||||
|
||||
evbuffer_add_printf(hf_private->evbuf_raw, "HTTP/%d.%d %d %s\r\n",
|
||||
hf_private->major, hf_private->minor, __resp_code, __str_resp_code);
|
||||
}
|
||||
|
||||
void hf_private_construct(struct http_half_private * hf_private)
|
||||
{
|
||||
assert(hf_private->is_setup_by_user);
|
||||
struct tfe_http_half * hf_public = to_hf_public(hf_private);
|
||||
|
||||
/* Clear the output buffer */
|
||||
if (hf_private->evbuf_raw == NULL)
|
||||
{
|
||||
hf_private->evbuf_raw = evbuffer_new();
|
||||
assert(hf_private->evbuf_raw != NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t __buf_length = evbuffer_get_length(hf_private->evbuf_raw);
|
||||
evbuffer_drain(hf_private->evbuf_raw, __buf_length);
|
||||
}
|
||||
|
||||
/* HTTP Request/Response first line */
|
||||
if (hf_public->direction == TFE_HTTP_REQUEST) __construct_request_line(hf_private);
|
||||
else __construct_response_line(hf_private);
|
||||
|
||||
/* Headers */
|
||||
void * iterator = NULL;
|
||||
struct http_field_name field_name{};
|
||||
|
||||
for (const char * str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name);
|
||||
str_value != NULL; str_value = tfe_http_field_iterate(hf_public, &iterator, &field_name))
|
||||
{
|
||||
const char * str_field = http_field_to_string(&field_name);
|
||||
evbuffer_add_printf(hf_private->evbuf_raw, "%s: %s\r\n", str_field, str_value);
|
||||
}
|
||||
|
||||
/* delimitor between header and body */
|
||||
evbuffer_add_printf(hf_private->evbuf_raw, "\r\n");
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -611,11 +691,11 @@ void __write_access_log(struct http_session_private * hs_private)
|
||||
char __str_resp_code[TFE_STRING_MAX];
|
||||
if (resp_spec)
|
||||
{
|
||||
snprintf(__str_resp_code, sizeof(__str_resp_code) - 1, "%d", resp_spec->resp_code);
|
||||
snprintf(__str_resp_code, sizeof(__str_resp_code) - 1, "%d", resp_spec->resp_code);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(__str_resp_code, sizeof(__str_resp_code) - 1, "%s", "-");
|
||||
snprintf(__str_resp_code, sizeof(__str_resp_code) - 1, "%s", "-");
|
||||
}
|
||||
|
||||
/* Content Type */
|
||||
@@ -640,8 +720,8 @@ void hs_private_destory(struct http_session_private * hs_private)
|
||||
free(hs_private);
|
||||
}
|
||||
|
||||
void hs_private_hf_private_set(struct http_session_private * hs_private, struct http_half_private * hf,
|
||||
enum tfe_http_direction direction)
|
||||
void hs_private_hf_private_set(struct http_session_private * hs_private,
|
||||
struct http_half_private * hf, enum tfe_http_direction direction)
|
||||
{
|
||||
struct tfe_http_half ** ref_old_half_public;
|
||||
struct http_half_private * old_half_private;
|
||||
|
||||
Reference in New Issue
Block a user