初步调通HTTP重定向业务
* 增加HTTP Status标准化定义及辅助函数; * 增加HTTP解析层发送应答的功能 * 修正了Pangu HTTP实现导致段错误的一系列问题。
This commit is contained in:
@@ -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