变更HTTP业务层回调函数定义,增加session系列操作方法定义及虚接口实现。

* 不提供业务层针对单个数据包(段)的处理函数(返回值),业务层只能对单个session定义操作;
* 通过session的方法(函数)通知解析层对该session的处理方法。
This commit is contained in:
Lu Qiuwen
2018-09-12 15:29:35 +08:00
parent 74a4d38fba
commit 9e59110f8a
8 changed files with 447 additions and 165 deletions

View File

@@ -1,7 +1,13 @@
#pragma once
#include <tfe_future.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <tfe_utils.h>
#include <tfe_future.h>
#include <tfe_stream.h>
/* Copy from http_parser.h */
#define HTTP_METHOD_MAP(XX) \
@@ -84,82 +90,6 @@ enum tfe_http_std_field
TFE_HTTP_ACCEPT_ENCODING,
};
struct tfe_http_req_spec
{
int method;
char * uri;
char * host;
char * url; //uri+host
char * accept_encoding;
};
struct tfe_http_resp_spec
{
int resp_code;
char * content_encoding;
};
enum tfe_http_direction
{
TFE_HTTP_REQUEST = 1,
TFE_HTTP_RESPONSE = 2
};
struct tfe_http_half
{
enum tfe_http_direction direction;
short major_version; //HTTP/1.x or HTTP/ 2
short minor_version; //HTTP 1.1 or 1.0
union
{
struct tfe_http_req_spec req_spec;
struct tfe_http_resp_spec resp_spec;
};
unsigned int field_cnt;
uint64_t cont_len;
uint64_t cont_range_from;
uint64_t cont_range_to;
struct evbuffer * body;
};
struct tfe_http_session
{
int session_id;//?
int major_version;//1:HTTP 1.x, 2:HTTP 2
struct tfe_http_half * req;
struct tfe_http_half * resp;//value is NULL before response received.
};
struct http_field_name
{
enum tfe_http_std_field field_id;
char * field_name; //Non-NULL when field_id isHTTP_UNKNOWN_FIELD.
};
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);
//******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.
const char * tfe_http_field_iterate(const struct tfe_http_half * half, void * interator, struct http_field_name * name);
struct tfe_http_half * tfe_http_allow_write(const struct tfe_http_half * half);
//@param value: NULL for delete
//@param name: Could be CHUNK/ENCODING
int tfe_http_field_write(struct tfe_http_half * half, const struct http_field_name * name, const char * value);
//******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
{
REQ_HDR_BITNUM = 0,
@@ -172,45 +102,221 @@ enum http_ev_bit_number
RESP_BODY_END_BITNUM
};
#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
enum tfe_bussiness_action
enum tfe_http_event
{
BIZ_ACTION_FORWARD,
BIZ_ACTION_MODIFIED,
BIZ_ACTION_ANSWER,
BIZ_ACTION_DROP,
BIZ_ACTION_PASSTHROUGH,
EV_HTTP_REQ_HDR = 1ULL << REQ_HDR_BITNUM,
EV_HTTP_REQ_BODY_BEGIN = 1ULL << REQ_BODY_BEGIN_BITNUM,
EV_HTTP_REQ_BODY_CONT = 1ULL << REQ_BODY_CONT_BITNUM,
EV_HTTP_REQ_BODY_END = 1ULL << REQ_BODY_END_BITNUM,
EV_HTTP_REQ_BODY_FULL = EV_HTTP_REQ_BODY_BEGIN | EV_HTTP_REQ_BODY_CONT | EV_HTTP_REQ_BODY_END,
EV_HTTP_REQ_END = EV_HTTP_REQ_BODY_END,
EV_HTTP_RESP_HDR = 1ULL << RESP_HDR_BITNUM,
EV_HTTP_RESP_BODY_BEGIN = 1ULL << RESP_BODY_BEGIN_BITNUM,
EV_HTTP_RESP_BODY_CONT = 1ULL << RESP_BODY_CONT_BITNUM,
EV_HTTP_RESP_BODY_END = 1ULL << RESP_BODY_END_BITNUM,
EV_HTTP_RESP_END = EV_HTTP_RESP_BODY_END,
};
//@param event: bit AND of EV_HTTP_**
//@param body_frag: NULL for no body data.
typedef tfe_bussiness_action (* http_data_func)(const struct tfe_stream * stream,
const struct tfe_http_session * session, uint64_t event, struct evbuffer * body_frag, void ** pme);
struct tfe_http_req_spec
{
enum tfe_http_std_method method;
const char * uri;
const char * host;
const char * url; //uri+host
};
struct tfe_http_resp_spec
{
int resp_code;
const char * content_encoding;
};
enum tfe_http_direction
{
TFE_HTTP_REQUEST = 1,
TFE_HTTP_RESPONSE = 2
};
struct tfe_http_half_ops
{
const char * (* ops_http_field_read)(const struct tfe_http_half *, const struct http_field_name *);
int (* ops_http_field_write)(struct tfe_http_half *, const struct http_field_name *, const char *);
struct tfe_http_half * (* ops_http_allow_write)(const struct tfe_http_half *);
const char * (* ops_http_field_iterate)(const struct tfe_http_half *, void **, struct http_field_name *);
int (* ops_append_body)(struct tfe_http_half *, char *, size_t, int);
void (* ops_free)(struct tfe_http_half * half);
};
struct tfe_http_session_ops
{
struct tfe_http_session * (* ops_allow_write)(const struct tfe_http_session * session);
void (* ops_detach)(struct tfe_http_session * session);
void (* ops_drop)(struct tfe_http_session * session);
void (* ops_passthrough)(struct tfe_http_session * session);
void (* ops_request_set)(struct tfe_http_session * session, struct tfe_http_half * req);
void (* ops_response_set)(struct tfe_http_session * session, struct tfe_http_half * resp);
struct tfe_http_half * (* ops_request_create)(struct tfe_http_session * session,
enum tfe_http_std_method method, const char * uri);
struct tfe_http_half * (* ops_response_create)(struct tfe_http_session * session, int resp_code);
};
struct tfe_http_half
{
enum tfe_http_direction direction;
const struct tfe_http_half_ops * ops;
short major_version;
short minor_version;
union
{
struct tfe_http_req_spec req_spec;
struct tfe_http_resp_spec resp_spec;
};
};
struct tfe_http_session
{
unsigned int session_id;
struct tfe_http_session_ops * ops;
struct tfe_http_half * req;
struct tfe_http_half * resp;
};
struct http_field_name
{
/* Standard Field */
enum tfe_http_std_field field_id;
/* Non-NULL when field_id is HTTP_UNKNOWN_FIELD */
char * field_name;
};
typedef void (http_session_begin_cb)(const struct tfe_stream * stream,
const struct tfe_http_session * session, unsigned int thread_id, void ** pme);
typedef void (http_session_data_cb)(const struct tfe_stream * stream,
const struct tfe_http_session * session, enum tfe_http_event event, const unsigned char * data,
size_t datalen, unsigned int thread_id, void ** pme);
typedef void (http_session_end_cb)(const struct tfe_stream * stream,
const struct tfe_http_session * session, unsigned int thread_id, void ** pme);
static inline struct http_field_name * http_field_name_duplicate(const struct http_field_name * orig)
{
struct http_field_name * __duplicated = ALLOC(struct http_field_name, 1);
assert(__duplicated != NULL);
if (orig->field_id == TFE_HTTP_UNKNOWN_FIELD)
{
__duplicated->field_id = TFE_HTTP_UNKNOWN_FIELD;
__duplicated->field_name = tfe_strdup(orig->field_name);
}
else
{
__duplicated->field_id = orig->field_id;
__duplicated->field_name = NULL;
}
return __duplicated;
}
static inline int http_field_name_compare(const struct http_field_name * lvalue,
const struct http_field_name * rvalue)
{
if (lvalue->field_id != rvalue->field_id)
{
return (lvalue->field_id - rvalue->field_id);
}
/* unknown field, compare field_name in string */
if (lvalue->field_id == TFE_HTTP_UNKNOWN_FIELD)
{
return strcasecmp(lvalue->field_name, rvalue->field_name);
}
/* field_id is equal, but not unknown, hit */
return 0;
}
static inline const char * tfe_http_field_read(const struct tfe_http_half * half,
const struct http_field_name * name)
{
return half->ops->ops_http_field_read(half, name);
}
static inline int tfe_http_field_write(struct tfe_http_half * half,
const struct http_field_name * name, const char * value)
{
return half->ops->ops_http_field_write(half, name, value);
}
static inline struct tfe_http_half * tfe_http_allow_write(const struct tfe_http_half * half)
{
return half->ops->ops_http_allow_write(half);
}
static inline const char * tfe_http_field_iterate(const struct tfe_http_half * half,
void ** interator, struct http_field_name * name)
{
return half->ops->ops_http_field_iterate(half, interator, name);
}
static inline int tfe_http_half_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag)
{
return half->ops->ops_append_body(half, buff, size, flag);
}
static inline void tfe_http_half_free(struct tfe_http_half * half)
{
return half->ops->ops_free(half);
}
static inline struct tfe_http_session * tfe_http_session_allow_write(const struct tfe_http_session * session)
{
return session->ops->ops_allow_write(session);
}
static inline void tfe_http_session_detach(struct tfe_http_session * session)
{
return session->ops->ops_detach(session);
}
static inline void tfe_http_session_drop(struct tfe_http_session * session)
{
return session->ops->ops_drop(session);
}
static inline void tfe_http_session_passthrough(struct tfe_http_session * session)
{
return session->ops->ops_passthrough(session);
}
static inline void tfe_http_session_request_set(struct tfe_http_session * session, struct tfe_http_half * req)
{
return session->ops->ops_request_set(session, req);
}
static inline void tfe_http_session_response_set(struct tfe_http_session * session, struct tfe_http_half * resp)
{
return session->ops->ops_response_set(session, resp);
}
static inline struct tfe_http_half * tfe_http_session_request_create(struct tfe_http_session * session,
enum tfe_http_std_method method, const char * uri)
{
return session->ops->ops_request_create(session, method, uri);
}
static inline struct tfe_http_half * tfe_http_session_response_create(struct tfe_http_session * session, int resp_code)
{
return session->ops->ops_response_create(session, resp_code);
}
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);
//@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.
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);
//@param half: ownership has been transfered to session, do NOT free half after setting.
void tfe_http_session_set_half(const struct tfe_http_session * session, struct tfe_http_half * half);
/*
handler_
@@ -233,3 +339,15 @@ rpc_finish_cb_
tfe_http_write_finish();
};
*/
/* TODO: transfer these declaration to ht_frame.h */
struct ht_frame_session_ctx * http_frame_raise_session_begin(const tfe_stream * stream,
struct tfe_http_session * ht_session, unsigned int thread_id);
void http_frame_raise_session_end(struct ht_frame_session_ctx * ss_ctx, struct tfe_stream * stream,
struct tfe_http_session * ht_session, unsigned int thread_id);
void http_frame_raise_event(struct ht_frame_session_ctx * ht_frame,
const tfe_stream * stream, struct tfe_http_session * ht_session, enum tfe_http_event event,
const unsigned char * data, size_t datalen, unsigned int thread_id);