初步调通HTTP请求头部内容替换业务

This commit is contained in:
Lu Qiuwen
2018-09-25 20:32:24 +08:00
parent d2e4ce94c2
commit d0ab629f4c
8 changed files with 297 additions and 166 deletions

View File

@@ -252,7 +252,7 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
hf_public->minor_version = parser->http_minor;
/* Copy version to session */
if(hf_private->session != NULL)
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;
@@ -277,7 +277,15 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
hf_private->event_cb(hf_private, EV_HTTP_RESP_HDR, NULL, 0, hf_private->event_cb_user);
}
hf_private->stream_action = ACTION_FORWARD_DATA;
if (hf_private->is_user_stream_action_set)
{
hf_private->stream_action = hf_private->user_stream_action;
}
else
{
hf_private->stream_action = ACTION_FORWARD_DATA;
}
return 0;
}
@@ -423,7 +431,19 @@ const char * hf_ops_field_iterate(const struct tfe_http_half * half, void ** ite
int hf_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag)
{
struct http_half_private * hf_private = to_hf_private(half);
if (hf_private->evbuf_body == NULL) { hf_private->evbuf_body = evbuffer_new(); }
/* Indicate the body is finished */
if (buff == NULL && size == 0)
{
hf_private->message_status = STATUS_COMPLETE;
return 0;
}
if (hf_private->evbuf_body == NULL)
{
hf_private->evbuf_body = evbuffer_new();
}
return evbuffer_add(hf_private->evbuf_body, buff, size);
}
@@ -517,7 +537,7 @@ int hf_private_parse(struct http_half_private * hf_private, const unsigned char
if (sz_parsed == len)
{
hf_private->parse_cursor += sz_parsed;
return 0;
return HTTP_PARSER_ERRNO(hf_private->parse_object) == HPE_PAUSED ? 1 : 0;
}
/* The paused parsar indicate the message boundary has been touched, we should return.
@@ -550,19 +570,48 @@ void hs_ops_drop(struct tfe_http_session * session)
return;
}
void hs_ops_request_set(struct tfe_http_session * session, struct tfe_http_half * req)
// TODO: change the return type to int, there is something happend where -1 returned.
void hs_ops_request_set(struct tfe_http_session * session, struct tfe_http_half * req_user)
{
struct http_half_private * hf_private = to_hf_private(req);
struct http_session_private * hs_private = to_hs_private(session);
struct http_half_private * hf_in_private = to_hf_request_private(hs_private);
struct http_half_private * hf_user_private = to_hf_private(req_user);
assert(hs_private->hf_private_req_user != NULL);
hs_private->hf_private_req_user = hf_private;
if (hf_in_private != NULL)
{
if (hf_in_private->stream_action == ACTION_DEFER_DATA)
{
hf_in_private->user_stream_action = ACTION_DROP_DATA;
hf_in_private->is_user_stream_action_set = true;
}
else
{
assert(0);
}
}
assert(hs_private->hf_private_req_user == NULL);
hs_private->hf_private_req_user = hf_user_private;
}
void hs_ops_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
{
struct http_half_private * hf_private = to_hf_private(resp);
struct http_session_private * hs_private = to_hs_private(session);
struct http_half_private * hf_in_private = to_hf_response_private(hs_private);
if (hf_in_private != NULL)
{
if (hf_in_private->stream_action == ACTION_DEFER_DATA)
{
hf_in_private->user_stream_action = ACTION_DROP_DATA;
hf_in_private->is_user_stream_action_set = true;
}
else
{
assert(0);
}
}
assert(hs_private->hf_private_resp_user == NULL);
hs_private->hf_private_resp_user = hf_private;
@@ -603,7 +652,18 @@ struct tfe_http_session_ops __http_session_ops =
void __construct_request_line(struct http_half_private * hf_private)
{
enum tfe_http_std_method __std_method = (enum tfe_http_std_method ) hf_private->method_or_status;
const char * __str_method = http_std_method_to_string(__std_method);
if (__str_method == NULL)
{
__str_method = "";
}
hf_private->major = 1;
hf_private->minor = 1;
evbuffer_add_printf(hf_private->evbuf_raw, "%s %s HTTP/%d.%d\r\n",
__str_method, hf_private->url_storage, hf_private->major, hf_private->minor);
}
void __construct_response_line(struct http_half_private * hf_private)
@@ -615,6 +675,9 @@ void __construct_response_line(struct http_half_private * hf_private)
__str_resp_code = "";
}
hf_private->major = 1;
hf_private->minor = 1;
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);
}