2018-08-20 15:46:35 +08:00
|
|
|
|
|
|
|
|
#include <tfe_future.h>
|
2018-08-01 17:47:38 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
struct tfe_http_req_spec
|
|
|
|
|
{
|
|
|
|
|
int method;
|
2018-09-03 16:16:36 +08:00
|
|
|
char * uri;
|
|
|
|
|
char * host;
|
|
|
|
|
char * url; //uri+host
|
|
|
|
|
char * accept_encoding;
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
|
|
|
|
struct tfe_http_resp_spec
|
|
|
|
|
{
|
|
|
|
|
int resp_code;
|
2018-09-03 16:16:36 +08:00
|
|
|
int major_version; //HTTP/1.x or HTTP/ 2
|
|
|
|
|
int minor_version; //HTTP 1.1 or 1.0
|
|
|
|
|
char * content_encoding;
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-09-03 16:16:36 +08:00
|
|
|
enum tfe_http_direction
|
|
|
|
|
{
|
|
|
|
|
TFE_HTTP_REQUEST = 1,
|
|
|
|
|
TFE_HTTP_RESPONSE = 2
|
|
|
|
|
};
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
struct tfe_http_half
|
|
|
|
|
{
|
2018-09-03 16:16:36 +08:00
|
|
|
enum tfe_http_direction direction;
|
2018-08-01 17:47:38 +08:00
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
struct tfe_http_req_spec req_spec;
|
|
|
|
|
struct tfe_http_resp_spec resp_spec;
|
|
|
|
|
};
|
2018-09-03 16:16:36 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
size_t field_cnt;
|
|
|
|
|
uint64_t cont_len;
|
|
|
|
|
uint64_t cont_range_from;
|
|
|
|
|
uint64_t cont_range_to;
|
2018-09-03 16:16:36 +08:00
|
|
|
struct evbuffer * body;
|
|
|
|
|
void * fields; //hide by protocol layer
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
struct tfe_http_session
|
|
|
|
|
{
|
|
|
|
|
int session_sequence;//?
|
|
|
|
|
int major_version;//1:HTTP 1.x, 2:HTTP 2
|
2018-09-03 16:16:36 +08:00
|
|
|
struct tfe_http_half * req;
|
|
|
|
|
struct tfe_http_half * resp;//value is NULL before response received.
|
|
|
|
|
void * proto_spec;
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
enum tfe_http_std_field
|
|
|
|
|
{
|
2018-09-03 16:16:36 +08:00
|
|
|
HTTP_UNKNOWN_FIELD = 0,
|
2018-08-01 17:47:38 +08:00
|
|
|
HTTP_MESSAGE_URL,
|
|
|
|
|
HTTP_URI,
|
|
|
|
|
HTTP_HOST,
|
|
|
|
|
HTTP_REFERER,
|
|
|
|
|
HTTP_USER_AGENT,
|
|
|
|
|
HTTP_COOKIE,
|
|
|
|
|
HTTP_PROXY_AUTHORIZATION,
|
|
|
|
|
HTTP_AUTHORIZATION,
|
|
|
|
|
HTTP_LOCATION,
|
|
|
|
|
HTTP_SERVER,
|
2018-09-03 16:16:36 +08:00
|
|
|
HTTP_ETAG,
|
|
|
|
|
HTTP_DATE,
|
|
|
|
|
HTTP_TRAILER,
|
2018-08-01 17:47:38 +08:00
|
|
|
HTTP_TRANSFER_ENCODING,
|
|
|
|
|
HTTP_VIA,
|
|
|
|
|
HTTP_PRAGMA,
|
|
|
|
|
HTTP_CONNECTION,
|
|
|
|
|
HTTP_CONT_ENCODING,
|
|
|
|
|
HTTP_CONT_LANGUAGE,
|
|
|
|
|
HTTP_CONT_LOCATION,
|
|
|
|
|
HTTP_CONT_RANGE,
|
|
|
|
|
HTTP_CONT_LENGTH,
|
|
|
|
|
HTTP_CONT_TYPE,
|
|
|
|
|
HTTP_CONT_DISPOSITION,
|
|
|
|
|
HTTP_CHARSET,
|
|
|
|
|
HTTP_EXPIRES,
|
|
|
|
|
HTTP_X_FLASH_VERSION,
|
2018-09-03 16:16:36 +08:00
|
|
|
HTTP_TRANSFER_LENGTH
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
struct http_field_name
|
|
|
|
|
{
|
|
|
|
|
enum tfe_http_std_field field_id;
|
2018-09-03 16:16:36 +08:00
|
|
|
char * field_name; //Non-NULL when field_id isHTTP_UNKNOWN_FIELD.
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
|
|
|
|
|
2018-09-03 16:16:36 +08:00
|
|
|
const char * tfe_http_field_read(const struct tfe_http_half * half, const struct http_field_name * name);
|
|
|
|
|
int tfe_http_field_to_range(const char * field_value, long * range_from, long * range_to);
|
|
|
|
|
int tfe_http_field_to_digit(const char * field_value, long * digit);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
|
|
|
|
//******obsolete
|
|
|
|
|
//long tfe_http_read_num_field(const struct tfe_http_half* half, const struct http_field_name* name);
|
|
|
|
|
|
|
|
|
|
//@Input param interator: NULL to get the first field.
|
|
|
|
|
//@Output param name:
|
|
|
|
|
//@return: field value.
|
2018-09-03 16:16:36 +08:00
|
|
|
const char * tfe_http_field_iterate(const struct tfe_http_half * half, void * interator, struct http_field_name * name);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-03 16:16:36 +08:00
|
|
|
struct tfe_http_half * tfe_http_allow_write(const struct tfe_http_half * half);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
|
|
|
|
//@param value: NULL for delete
|
|
|
|
|
//@param name: Could be CHUNK/ENCODING
|
2018-09-03 16:16:36 +08:00
|
|
|
int tfe_http_field_write(struct tfe_http_half * half, const struct http_field_name * name, const char * value);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
|
|
|
|
//******obsolete
|
|
|
|
|
|
|
|
|
|
//int tfe_http_field_read_func(const struct tfe_http_half* half, const char* name, char* value, size_t size);
|
|
|
|
|
|
|
|
|
|
enum http_ev_bit_number
|
|
|
|
|
{
|
2018-09-03 16:16:36 +08:00
|
|
|
REQ_HDR_BITNUM = 0,
|
2018-08-01 17:47:38 +08:00
|
|
|
REQ_BODY_BEGIN_BITNUM,
|
|
|
|
|
REQ_BODY_CONT_BITNUM,
|
|
|
|
|
REQ_BODY_END_BITNUM,
|
|
|
|
|
RESP_HDR_BITNUM,
|
|
|
|
|
RESP_BODY_BEGIN_BITNUM,
|
|
|
|
|
RESP_BODY_CONT_BITNUM,
|
|
|
|
|
RESP_BODY_END_BITNUM
|
|
|
|
|
};
|
2018-09-03 16:16:36 +08:00
|
|
|
|
|
|
|
|
#define EV_HTTP_REQ_HDR ((uint64_t)1<<REQ_HDR_BITNUM)
|
|
|
|
|
#define EV_HTTP_SESSION_BEGIN EV_HTTP_REQ_HDR
|
|
|
|
|
#define EV_HTTP_REQ_BODY_BEGIN ((uint64_t)1<<REQ_HDR_BITNUM)
|
|
|
|
|
#define EV_HTTP_REQ_BODY_CONT ((uint64_t)1<<REQ_BODY_CONT_BITNUM)
|
|
|
|
|
#define EV_HTTP_REQ_BODY_END ((uint64_t)1<<REQ_BODY_END_BITNUM)
|
|
|
|
|
#define EV_HTTP_REQ_BODY_FULL (EV_HTTP_REQ_BODY_BEGIN|EV_HTTP_REQ_BODY_CONT|EV_HTTP_REQ_BODY_END)
|
|
|
|
|
#define EV_HTTP_REQ_END EV_HTTP_REQ_BODY_END
|
|
|
|
|
#define EV_HTTP_RESP_HDR ((uint64_t)1<<RESP_HDR_BITNUM)
|
|
|
|
|
#define EV_HTTP_RESP_BODY_BEGIN ((uint64_t)1<<RESP_BODY_BEGIN_BITNUM)
|
|
|
|
|
#define EV_HTTP_RESP_BODY_CONT ((uint64_t)1<<RESP_BODY_CONT_BITNUM)
|
|
|
|
|
#define EV_HTTP_RESP_BODY_END ((uint64_t)1<<RESP_BODY_END_BITNUM)
|
|
|
|
|
#define EV_HTTP_RESP_BODY_FULL (EV_HTTP_RESP_BODY_BEGIN|EV_HTTP_RESP_BODY_CONT|EV_HTTP_RESP_BODY_END)
|
|
|
|
|
#define EV_HTTP_RESP_END EV_HTTP_RESP_BODY_END
|
|
|
|
|
#define EV_HTTP_SESSION_END EV_HTTP_RESP_END
|
2018-08-01 17:47:38 +08:00
|
|
|
|
|
|
|
|
enum tfe_bussiness_action
|
|
|
|
|
{
|
|
|
|
|
BIZ_ACTION_FORWARD,
|
|
|
|
|
BIZ_ACTION_MODIFIED,
|
|
|
|
|
BIZ_ACTION_ANSWER,
|
|
|
|
|
BIZ_ACTION_DROP,
|
2018-08-20 15:46:35 +08:00
|
|
|
BIZ_ACTION_PASSTHROUGH,
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//@param event: bit AND of EV_HTTP_**
|
|
|
|
|
//@param body_frag: NULL for no body data.
|
2018-09-03 16:16:36 +08:00
|
|
|
typedef tfe_bussiness_action (* http_read_func)(const struct tfe_stream * stream,
|
|
|
|
|
const struct tfe_http_session * session, uint64_t event, struct evbuffer * body_frag, void ** pme);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-03 16:16:36 +08:00
|
|
|
struct tfe_http_half * tfe_http_request_create(int major_version, int method, const char * uri, const char * host);
|
|
|
|
|
struct tfe_http_half * tfe_http_response_create(int major_version, int resp_code);
|
2018-08-20 15:46:35 +08:00
|
|
|
|
|
|
|
|
//@flag EV_HTTP_RESP_BODY_END, EV_HTTP_RESP_BODY_FULL,
|
|
|
|
|
//suspend stream on EV_HTTP_REQ_BODY_BEGIN, resume when EV_HTTP_REQ_BODY_END.
|
2018-09-03 16:16:36 +08:00
|
|
|
int tfe_http_append_body(const struct tfe_http_half * half, char * buff, size_t size, int flag);
|
|
|
|
|
void tfe_http_half_free(struct tfe_http_half * half);
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
//@param half: ownership has been transfered to session, do NOT free half after setting.
|
2018-09-03 16:16:36 +08:00
|
|
|
void tfe_http_session_set_half(const struct tfe_http_session * session, struct tfe_http_half * half);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-08-20 15:46:35 +08:00
|
|
|
/*
|
|
|
|
|
handler_
|
|
|
|
|
{
|
|
|
|
|
tfe_http_response_frag_create;
|
|
|
|
|
tfe_http_session_set_half;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpc_cb_
|
|
|
|
|
{
|
|
|
|
|
tfe_http_set_field
|
|
|
|
|
tfe_http_append_body
|
|
|
|
|
|
|
|
|
|
tfe_http_write();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rpc_finish_cb_
|
|
|
|
|
{
|
|
|
|
|
tfe_http_write_finish();
|
|
|
|
|
};
|
2018-09-03 16:16:36 +08:00
|
|
|
*/
|