增加HTTP流式构造Body的接口及实现并修正HTTP单元测试用例
This commit is contained in:
@@ -333,8 +333,8 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
|
||||
hf_private->stream_action = hf_private->user_stream_action;
|
||||
}
|
||||
|
||||
/* user's suspend tag is set, which indicate that the way to handle request/response
|
||||
* cannot be determinate at now, need to defer */
|
||||
/* user's suspend tag is set, which indicate that the way to handle request/response
|
||||
* cannot be determinate at now, need to defer */
|
||||
else if (hs_private && hs_private->suspend_tag_user)
|
||||
{
|
||||
/* Pause parser, prevent to parse request/response body,
|
||||
@@ -349,7 +349,7 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
|
||||
assert(hf_private->stream_action == ACTION_DEFER_DATA);
|
||||
}
|
||||
|
||||
/* Otherwise, forward the request/response */
|
||||
/* Otherwise, forward the request/response */
|
||||
else
|
||||
{
|
||||
hf_private->stream_action = ACTION_FORWARD_DATA;
|
||||
@@ -584,6 +584,77 @@ int hf_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, in
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hf_ops_body_begin(struct tfe_http_half * half, int by_stream)
|
||||
{
|
||||
struct http_half_private * hf_private = to_hf_private(half);
|
||||
|
||||
assert(hf_private->evbuf_body == NULL);
|
||||
if (hf_private->evbuf_body != NULL)
|
||||
{
|
||||
evbuffer_free(hf_private->evbuf_body);
|
||||
}
|
||||
|
||||
if (by_stream)
|
||||
{
|
||||
hf_private->is_setup_by_stream = true;
|
||||
}
|
||||
|
||||
hf_private->evbuf_body = evbuffer_new();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hf_ops_body_data(struct tfe_http_half * half, const unsigned char * data, size_t sz_data)
|
||||
{
|
||||
struct http_half_private * hf_private = to_hf_private(half);
|
||||
struct evbuffer * __tmp_output_buffer = evbuffer_new();
|
||||
int ret = 0;
|
||||
|
||||
/* Need to compress output */
|
||||
if (hf_private->cv_compress_object)
|
||||
{
|
||||
ret = hf_content_compress_write(hf_private->cv_compress_object, data, sz_data, __tmp_output_buffer, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = evbuffer_add(__tmp_output_buffer, data, sz_data);
|
||||
}
|
||||
|
||||
if (ret < 0) goto __out;
|
||||
ret = evbuffer_add_buffer(hf_private->evbuf_body, __tmp_output_buffer);
|
||||
|
||||
/* Have write ctx, should write the body to TCP stream immediately but not to store in evbuf_body */
|
||||
if (hf_private->write_ctx)
|
||||
{
|
||||
/* Perpare to write data, TODO: write to TCP stream by transfer evbuffer */
|
||||
const unsigned char * ptr_write_data = evbuffer_pullup(hf_private->evbuf_body, -1);
|
||||
size_t sz_write_data = evbuffer_get_length(hf_private->evbuf_body);
|
||||
|
||||
assert(ptr_write_data != NULL && sz_write_data >= 0);
|
||||
tfe_stream_write_frag(hf_private->write_ctx, ptr_write_data, sz_write_data);
|
||||
|
||||
/* Need to drain all data */
|
||||
evbuffer_drain(hf_private->evbuf_body, sz_write_data);
|
||||
}
|
||||
|
||||
__out:
|
||||
evbuffer_free(__tmp_output_buffer);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int hf_ops_body_end(struct tfe_http_half * half)
|
||||
{
|
||||
struct http_half_private * hf_private = to_hf_private(half);
|
||||
if (hf_private->write_ctx)
|
||||
{
|
||||
tfe_stream_write_frag_end(hf_private->write_ctx);
|
||||
hf_private->write_ctx = NULL;
|
||||
}
|
||||
|
||||
hf_private->body_status = STATUS_COMPLETE;
|
||||
hf_private->message_status = STATUS_COMPLETE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hf_private_destory(struct http_half_private * hf_private)
|
||||
{
|
||||
if (hf_private->parse_object != NULL)
|
||||
@@ -611,6 +682,9 @@ struct tfe_http_half_ops __http_half_ops =
|
||||
.ops_http_allow_write = hf_ops_allow_write,
|
||||
.ops_http_field_iterate = hf_ops_field_iterate,
|
||||
.ops_append_body = hf_ops_append_body,
|
||||
.ops_body_begin = NULL,
|
||||
.ops_body_data = NULL,
|
||||
.ops_body_end = NULL,
|
||||
.ops_free = hf_ops_free
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user