2018-09-06 10:12:08 +08:00
|
|
|
#pragma once
|
2018-09-12 15:29:35 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
#include <tfe_utils.h>
|
2018-08-20 15:46:35 +08:00
|
|
|
#include <tfe_future.h>
|
2018-09-12 15:29:35 +08:00
|
|
|
#include <tfe_stream.h>
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-06 10:12:08 +08:00
|
|
|
/* Copy from http_parser.h */
|
|
|
|
|
#define HTTP_METHOD_MAP(XX) \
|
|
|
|
|
XX(0, DELETE, DELETE) \
|
|
|
|
|
XX(1, GET, GET) \
|
|
|
|
|
XX(2, HEAD, HEAD) \
|
|
|
|
|
XX(3, POST, POST) \
|
|
|
|
|
XX(4, PUT, PUT) \
|
|
|
|
|
/* pathological */ \
|
|
|
|
|
XX(5, CONNECT, CONNECT) \
|
|
|
|
|
XX(6, OPTIONS, OPTIONS) \
|
|
|
|
|
XX(7, TRACE, TRACE) \
|
|
|
|
|
/* WebDAV */ \
|
|
|
|
|
XX(8, COPY, COPY) \
|
|
|
|
|
XX(9, LOCK, LOCK) \
|
|
|
|
|
XX(10, MKCOL, MKCOL) \
|
|
|
|
|
XX(11, MOVE, MOVE) \
|
|
|
|
|
XX(12, PROPFIND, PROPFIND) \
|
|
|
|
|
XX(13, PROPPATCH, PROPPATCH) \
|
|
|
|
|
XX(14, SEARCH, SEARCH) \
|
|
|
|
|
XX(15, UNLOCK, UNLOCK) \
|
|
|
|
|
XX(16, BIND, BIND) \
|
|
|
|
|
XX(17, REBIND, REBIND) \
|
|
|
|
|
XX(18, UNBIND, UNBIND) \
|
|
|
|
|
XX(19, ACL, ACL) \
|
|
|
|
|
/* subversion */ \
|
|
|
|
|
XX(20, REPORT, REPORT) \
|
|
|
|
|
XX(21, MKACTIVITY, MKACTIVITY) \
|
|
|
|
|
XX(22, CHECKOUT, CHECKOUT) \
|
|
|
|
|
XX(23, MERGE, MERGE) \
|
|
|
|
|
/* upnp */ \
|
|
|
|
|
XX(24, MSEARCH, M-SEARCH) \
|
|
|
|
|
XX(25, NOTIFY, NOTIFY) \
|
|
|
|
|
XX(26, SUBSCRIBE, SUBSCRIBE) \
|
|
|
|
|
XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
|
|
|
|
|
/* RFC-5789 */ \
|
|
|
|
|
XX(28, PATCH, PATCH) \
|
|
|
|
|
XX(29, PURGE, PURGE) \
|
|
|
|
|
/* CalDAV */ \
|
|
|
|
|
XX(30, MKCALENDAR, MKCALENDAR) \
|
|
|
|
|
/* RFC-2068, section 19.6.1.2 */ \
|
|
|
|
|
XX(31, LINK, LINK) \
|
|
|
|
|
XX(32, UNLINK, UNLINK) \
|
|
|
|
|
/* icecast */ \
|
|
|
|
|
XX(33, SOURCE, SOURCE) \
|
|
|
|
|
|
|
|
|
|
enum tfe_http_std_method
|
|
|
|
|
{
|
|
|
|
|
#define XX(num, name, string) TFE_HTTP_##name = num,
|
|
|
|
|
HTTP_METHOD_MAP(XX)
|
|
|
|
|
#undef XX
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum tfe_http_std_field
|
|
|
|
|
{
|
|
|
|
|
TFE_HTTP_UNKNOWN_FIELD = 0,
|
|
|
|
|
TFE_HTTP_HOST,
|
|
|
|
|
TFE_HTTP_REFERER,
|
|
|
|
|
TFE_HTTP_USER_AGENT,
|
|
|
|
|
TFE_HTTP_COOKIE,
|
|
|
|
|
TFE_HTTP_PROXY_AUTHORIZATION,
|
|
|
|
|
TFE_HTTP_AUTHORIZATION,
|
|
|
|
|
TFE_HTTP_LOCATION,
|
|
|
|
|
TFE_HTTP_SERVER,
|
|
|
|
|
TFE_HTTP_ETAG,
|
|
|
|
|
TFE_HTTP_DATE,
|
|
|
|
|
TFE_HTTP_TRAILER,
|
|
|
|
|
TFE_HTTP_TRANSFER_ENCODING,
|
|
|
|
|
TFE_HTTP_VIA,
|
|
|
|
|
TFE_HTTP_PRAGMA,
|
|
|
|
|
TFE_HTTP_CONNECTION,
|
|
|
|
|
TFE_HTTP_CONT_ENCODING,
|
|
|
|
|
TFE_HTTP_CONT_LANGUAGE,
|
|
|
|
|
TFE_HTTP_CONT_LOCATION,
|
|
|
|
|
TFE_HTTP_CONT_RANGE,
|
|
|
|
|
TFE_HTTP_CONT_LENGTH,
|
|
|
|
|
TFE_HTTP_CONT_TYPE,
|
|
|
|
|
TFE_HTTP_CONT_DISPOSITION,
|
|
|
|
|
TFE_HTTP_EXPIRES,
|
|
|
|
|
TFE_HTTP_ACCEPT_ENCODING,
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
enum http_ev_bit_number
|
|
|
|
|
{
|
|
|
|
|
REQ_HDR_BITNUM = 0,
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum tfe_http_event
|
|
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
struct tfe_http_req_spec
|
|
|
|
|
{
|
2018-09-12 15:29:35 +08:00
|
|
|
enum tfe_http_std_method method;
|
|
|
|
|
const char * uri;
|
|
|
|
|
const char * host;
|
|
|
|
|
const char * url; //uri+host
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
2018-09-06 10:12:08 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
struct tfe_http_resp_spec
|
|
|
|
|
{
|
|
|
|
|
int resp_code;
|
2018-09-12 15:29:35 +08:00
|
|
|
const 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-09-12 15:29:35 +08:00
|
|
|
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);
|
2018-09-12 15:56:44 +08:00
|
|
|
void (* ops_detach)(const struct tfe_http_session * session);
|
2018-09-12 15:29:35 +08:00
|
|
|
void (* ops_drop)(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);
|
|
|
|
|
};
|
|
|
|
|
|
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-09-12 15:29:35 +08:00
|
|
|
const struct tfe_http_half_ops * ops;
|
|
|
|
|
short major_version;
|
|
|
|
|
short minor_version;
|
2018-09-06 10:12:08 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
union
|
|
|
|
|
{
|
|
|
|
|
struct tfe_http_req_spec req_spec;
|
|
|
|
|
struct tfe_http_resp_spec resp_spec;
|
|
|
|
|
};
|
|
|
|
|
};
|
2018-08-20 15:46:35 +08:00
|
|
|
|
2018-08-01 17:47:38 +08:00
|
|
|
struct tfe_http_session
|
|
|
|
|
{
|
2018-09-12 15:29:35 +08:00
|
|
|
unsigned int session_id;
|
|
|
|
|
struct tfe_http_session_ops * ops;
|
2018-09-03 16:16:36 +08:00
|
|
|
struct tfe_http_half * req;
|
2018-09-12 15:29:35 +08:00
|
|
|
struct tfe_http_half * resp;
|
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
|
|
|
|
|
{
|
2018-09-12 15:29:35 +08:00
|
|
|
/* Standard Field */
|
2018-08-01 17:47:38 +08:00
|
|
|
enum tfe_http_std_field field_id;
|
2018-09-12 15:29:35 +08:00
|
|
|
/* Non-NULL when field_id is HTTP_UNKNOWN_FIELD */
|
|
|
|
|
char * field_name;
|
2018-08-01 17:47:38 +08:00
|
|
|
};
|
|
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
typedef void (http_session_begin_cb)(const struct tfe_stream * stream,
|
|
|
|
|
const struct tfe_http_session * session, unsigned int thread_id, void ** pme);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
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);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
typedef void (http_session_end_cb)(const struct tfe_stream * stream,
|
|
|
|
|
const struct tfe_http_session * session, unsigned int thread_id, void ** pme);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
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);
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
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;
|
|
|
|
|
}
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
return __duplicated;
|
|
|
|
|
}
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
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);
|
|
|
|
|
}
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
/* 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)
|
2018-08-01 17:47:38 +08:00
|
|
|
{
|
2018-09-12 15:29:35 +08:00
|
|
|
return half->ops->ops_http_field_read(half, name);
|
|
|
|
|
}
|
2018-09-03 16:16:36 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 15:56:44 +08:00
|
|
|
static inline void tfe_http_session_detach(const struct tfe_http_session * session)
|
2018-09-12 15:29:35 +08:00
|
|
|
{
|
|
|
|
|
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_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);
|
|
|
|
|
}
|
2018-08-01 17:47:38 +08:00
|
|
|
|
2018-09-12 15:29:35 +08:00
|
|
|
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);
|
|
|
|
|
}
|
2018-08-01 17:47:38 +08:00
|
|
|
|
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-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
|
|
|
*/
|
2018-09-12 15:29:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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);
|