增加简单的HTTP协议插件管理层,基本联通HTTP解析层与业务层插件。
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
#include <stdlib.h>
|
||||
#include <tfe_http.h>
|
||||
#include <tfe_utils.h>
|
||||
#include <http_common.h>
|
||||
#include <assert.h>
|
||||
|
||||
@@ -9,6 +7,10 @@
|
||||
#include <sys/param.h>
|
||||
#include <event2/buffer.h>
|
||||
|
||||
#include <tfe_http.h>
|
||||
#include <tfe_utils.h>
|
||||
#include <http_half.h>
|
||||
|
||||
#define __PARSER_TO_HF_PRIVATE(_parser) ((struct http_half_private *)(_parser->data))
|
||||
|
||||
static const char * __str_std_header_field_map[] =
|
||||
@@ -115,10 +117,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);
|
||||
|
||||
@@ -140,10 +142,10 @@ void __hf_public_resp_fill_from_private(struct http_half_private * hf_private, s
|
||||
/* Status Code */
|
||||
hf_resp_spec->resp_code = parser->status_code;
|
||||
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
|
||||
};
|
||||
|
||||
/* Content Encoding */
|
||||
hf_resp_spec->content_encoding = (char *) tfe_http_field_read(hf_public, &__cont_encoding_field_name);
|
||||
@@ -167,24 +169,23 @@ static int __parser_callback_on_message_begin(struct http_parser * parser)
|
||||
hf_private->evbuf_header_value = evbuffer_new();
|
||||
hf_private->evbuf_body = evbuffer_new();
|
||||
|
||||
hf_private->status_header = STATUS_INIT;
|
||||
hf_private->status_body = STATUS_INIT;
|
||||
hf_private->status_message = STATUS_READING;
|
||||
hf_private->body_status = STATUS_INIT;
|
||||
hf_private->message_status = STATUS_READING;
|
||||
|
||||
/* Never call user's callback, need to defer data */
|
||||
hf_private->stream_action = ACTION_DEFER_DATA;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __parser_callback_on_uri_field(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_INIT, STATUS_READING);
|
||||
return evbuffer_add(hf_private->evbuf_uri, at, length);
|
||||
}
|
||||
|
||||
static int __parser_callback_on_header_field(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_READING, STATUS_READING);
|
||||
|
||||
/* Last field-value tuple doesn't push into hf_private, flush these */
|
||||
if (evbuffer_get_length(hf_private->evbuf_header_field) != 0)
|
||||
@@ -198,7 +199,6 @@ static int __parser_callback_on_header_field(struct http_parser * parser, const
|
||||
static int __parser_callback_on_header_value(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_READING, STATUS_READING);
|
||||
return evbuffer_add(hf_private->evbuf_header_value, at, length);
|
||||
}
|
||||
|
||||
@@ -225,41 +225,91 @@ static int __parser_callback_on_headers_complete(http_parser * parser)
|
||||
__hf_public_resp_fill_from_private(hf_private, parser);
|
||||
}
|
||||
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_READING, STATUS_COMPLETE);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_body, STATUS_INIT, STATUS_READING);
|
||||
if (hf_private->event_cb && hf_direction == TFE_HTTP_REQUEST)
|
||||
{
|
||||
hf_private->event_cb(hf_private, EV_HTTP_REQ_HDR, NULL, 0, hf_private->event_cb_user);
|
||||
}
|
||||
|
||||
if (hf_private->event_cb && hf_direction == TFE_HTTP_RESPONSE)
|
||||
{
|
||||
hf_private->event_cb(hf_private, EV_HTTP_RESP_HDR, NULL, 0, hf_private->event_cb_user);
|
||||
}
|
||||
|
||||
hf_private->stream_action = ACTION_FORWARD_DATA;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __parser_callback_on_body(struct http_parser * parser, const char * at, size_t length)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
return evbuffer_add(hf_private->evbuf_body, at, length);
|
||||
enum tfe_http_direction hf_direction = hf_private->hf_public.direction;
|
||||
enum tfe_http_event ev_body_begin;
|
||||
enum tfe_http_event ev_body_cont;
|
||||
|
||||
if (hf_direction == TFE_HTTP_REQUEST)
|
||||
{
|
||||
ev_body_begin = EV_HTTP_REQ_BODY_BEGIN;
|
||||
ev_body_cont = EV_HTTP_REQ_BODY_CONT;
|
||||
}
|
||||
else
|
||||
{
|
||||
ev_body_begin = EV_HTTP_RESP_BODY_BEGIN;
|
||||
ev_body_cont = EV_HTTP_RESP_BODY_CONT;
|
||||
}
|
||||
|
||||
if (hf_private->body_status == STATUS_INIT && hf_private->event_cb)
|
||||
{
|
||||
hf_private->event_cb(hf_private, ev_body_begin, NULL, parser->content_length, hf_private->event_cb_user);
|
||||
}
|
||||
|
||||
if (hf_private->event_cb)
|
||||
{
|
||||
hf_private->event_cb(hf_private, ev_body_cont, (const unsigned char *) at, length, hf_private->event_cb_user);
|
||||
}
|
||||
|
||||
hf_private->body_status = STATUS_READING;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __parser_callback_on_message_complete(http_parser * parser)
|
||||
{
|
||||
struct http_half_private * hf_private = __PARSER_TO_HF_PRIVATE(parser);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_header, STATUS_COMPLETE, STATUS_COMPLETE);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_body, STATUS_READING || STATUS_COMPLETE, STATUS_COMPLETE);
|
||||
__HF_PRIVATE_CHANGE_STATUS(hf_private->status_message, STATUS_READING, STATUS_COMPLETE);
|
||||
enum tfe_http_direction hf_direction = hf_private->hf_public.direction;
|
||||
enum tfe_http_event ev_body_end;
|
||||
|
||||
if (hf_direction == TFE_HTTP_REQUEST)
|
||||
{
|
||||
ev_body_end = EV_HTTP_REQ_BODY_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
ev_body_end = EV_HTTP_RESP_BODY_END;
|
||||
}
|
||||
|
||||
if (hf_private->event_cb)
|
||||
{
|
||||
hf_private->event_cb(hf_private, ev_body_end, NULL, 0, hf_private->event_cb_user);
|
||||
}
|
||||
|
||||
hf_private->body_status = STATUS_COMPLETE;
|
||||
hf_private->message_status = STATUS_COMPLETE;
|
||||
http_parser_pause(parser, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static http_parser_settings __http_half_parse_setting =
|
||||
{
|
||||
.on_message_begin = __parser_callback_on_message_begin,
|
||||
.on_url = __parser_callback_on_uri_field,
|
||||
.on_status = NULL,
|
||||
.on_header_field = __parser_callback_on_header_field,
|
||||
.on_header_value = __parser_callback_on_header_value,
|
||||
.on_headers_complete = __parser_callback_on_headers_complete,
|
||||
.on_body = __parser_callback_on_body,
|
||||
.on_message_complete = __parser_callback_on_message_complete,
|
||||
.on_chunk_header = NULL,
|
||||
.on_chunk_complete = NULL
|
||||
};
|
||||
{
|
||||
.on_message_begin = __parser_callback_on_message_begin,
|
||||
.on_url = __parser_callback_on_uri_field,
|
||||
.on_status = NULL,
|
||||
.on_header_field = __parser_callback_on_header_field,
|
||||
.on_header_value = __parser_callback_on_header_value,
|
||||
.on_headers_complete = __parser_callback_on_headers_complete,
|
||||
.on_body = __parser_callback_on_body,
|
||||
.on_message_complete = __parser_callback_on_message_complete,
|
||||
.on_chunk_header = NULL,
|
||||
.on_chunk_complete = NULL
|
||||
};
|
||||
|
||||
const char * hf_ops_field_read(const struct tfe_http_half * half, const struct http_field_name * field)
|
||||
{
|
||||
@@ -326,7 +376,16 @@ int hf_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, in
|
||||
|
||||
void hf_private_destory(struct http_half_private * hf_private)
|
||||
{
|
||||
if (hf_private->parse_object != NULL) free(hf_private->parse_object);
|
||||
if (hf_private->parse_object != NULL)
|
||||
{
|
||||
free(hf_private->parse_object);
|
||||
}
|
||||
|
||||
if (hf_private->event_cb_user_deleter != NULL)
|
||||
{
|
||||
hf_private->event_cb_user_deleter(hf_private->event_cb_user);
|
||||
}
|
||||
|
||||
free(hf_private);
|
||||
}
|
||||
|
||||
@@ -373,10 +432,17 @@ struct http_half_private * hf_private_create(tfe_http_direction ht_dir, short ma
|
||||
return hf_private;
|
||||
}
|
||||
|
||||
void hf_private_set_callback(struct http_half_private * hf_private, hf_private_cb * cb,
|
||||
void * user, void (* user_deleter)(void *))
|
||||
{
|
||||
hf_private->event_cb = cb;
|
||||
hf_private->event_cb_user = user;
|
||||
hf_private->event_cb_user_deleter = user_deleter;
|
||||
}
|
||||
|
||||
int hf_private_parse(struct http_half_private * hf_private, const unsigned char * data, size_t len)
|
||||
{
|
||||
assert(hf_private->parse_cursor <= len);
|
||||
int ret = 0;
|
||||
|
||||
/* Caculate the memory zones to scan. The zone from data to data + cursor has been scaned
|
||||
* at last construct procedure, so we don't need to scan again. */
|
||||
@@ -387,29 +453,92 @@ int hf_private_parse(struct http_half_private * hf_private, const unsigned char
|
||||
size_t sz_parsed = http_parser_execute(hf_private->parse_object,
|
||||
&__http_half_parse_setting, __data_with_offset, __len_with_offset);
|
||||
|
||||
/* The paused parsar indicate that some kind of boundary has been touched,
|
||||
* we should return and call user's data callback. resume it to normal status */
|
||||
if (sz_parsed && HTTP_PARSER_ERRNO(hf_private->parse_object) == HPE_PAUSED)
|
||||
/* Nothing happended */
|
||||
if (sz_parsed == len)
|
||||
{
|
||||
hf_private->parse_cursor += sz_parsed;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The paused parsar indicate the message boundary has been touched, we should return.
|
||||
* resume it to normal status */
|
||||
if (HTTP_PARSER_ERRNO(hf_private->parse_object) == HPE_PAUSED)
|
||||
{
|
||||
http_parser_pause(hf_private->parse_object, 0);
|
||||
hf_private->parse_cursor += sz_parsed;
|
||||
|
||||
if (data[hf_private->parse_cursor] == '\r' && data[hf_private->parse_cursor + 1] == '\n')
|
||||
{
|
||||
hf_private->parse_cursor++;
|
||||
}
|
||||
|
||||
hf_private->parse_cursor += sz_parsed + 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Some kind of exception happend */
|
||||
if (sz_parsed != __len_with_offset)
|
||||
{
|
||||
hf_private->parse_errno = HTTP_PARSER_ERRNO(hf_private->parse_object);
|
||||
ret = -1; goto __out;
|
||||
}
|
||||
|
||||
__out:
|
||||
hf_private->parse_cursor += sz_parsed;
|
||||
return ret;
|
||||
hf_private->parse_errno = HTTP_PARSER_ERRNO(hf_private->parse_object);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void hs_ops_detach(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_detach(hs_private->ht_frame);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void hs_ops_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
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 tfe_http_half * hs_ops_response_create(struct tfe_http_session * session, int resp_code)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
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)
|
||||
{
|
||||
struct http_session_private * __hs_private = ALLOC(struct http_session_private, 1);
|
||||
|
||||
/* HS-PUBLIC */
|
||||
__hs_private->hs_public.ops = &__http_session_ops;
|
||||
__hs_private->hs_public.req = hf_private_req != NULL ? to_hf_public(hf_private_req) : NULL;
|
||||
__hs_private->hs_public.resp = hf_private_req != NULL ? to_hf_public(hf_private_resp) : NULL;
|
||||
__hs_private->hs_public.session_id = hc_private->session_id_counter++;
|
||||
|
||||
/* HS-PRIVATE*/
|
||||
__hs_private->hc_private = hc_private;
|
||||
return __hs_private;
|
||||
}
|
||||
|
||||
void hs_private_destory(struct http_session_private * hs_private)
|
||||
{
|
||||
free(hs_private);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user