diff --git a/plugin/business/pangu-http/src/pattern_replace.cpp b/plugin/business/pangu-http/src/pattern_replace.cpp index 48ed794..95e3fed 100644 --- a/plugin/business/pangu-http/src/pattern_replace.cpp +++ b/plugin/business/pangu-http/src/pattern_replace.cpp @@ -333,7 +333,7 @@ size_t insert_string(char * in, size_t in_sz, const char *insert_on, const char offset+=concat_len; memcpy(target+offset, insert_from, in_sz-(insert_from-in)); offset+=in_sz-(insert_from-in); - assert(target_size==offset+1); + assert(target_size==offset); free(concat_style); concat_style = NULL; diff --git a/plugin/protocol/http/src/http_convert.cpp b/plugin/protocol/http/src/http_convert.cpp index c8d548e..8e82070 100644 --- a/plugin/protocol/http/src/http_convert.cpp +++ b/plugin/protocol/http/src/http_convert.cpp @@ -280,7 +280,7 @@ static int __hf_content_compress_write_zlib(struct hf_content_compress * cv_obje /* Reserve the space, because the length of the compressed data will be short * than uncompressed data in usually, we set the reserve space as much as sz_in_data */ - size_t __sz_reserve_chunk = sz_in_data > SZ_RESERVE_SPACE ? sz_in_data : SZ_RESERVE_SPACE; + size_t __sz_reserve_chunk = MAX(sz_in_data, SZ_RESERVE_SPACE); int iov_count = evbuffer_reserve_space(out_ev_buf, __sz_reserve_chunk, v, 1); if (iov_count != 1) return -1; diff --git a/plugin/protocol/http2/include/internal/http2_common.h b/plugin/protocol/http2/include/internal/http2_common.h index 0e8ade1..fa6d787 100644 --- a/plugin/protocol/http2/include/internal/http2_common.h +++ b/plugin/protocol/http2/include/internal/http2_common.h @@ -24,7 +24,7 @@ typedef struct RTLogInit2Data_ { typedef struct Http2Plugin_{ struct event * event[TFE_THREAD_MAX]; - struct tfe_session_info_t session_info[TFE_THREAD_MAX]; + struct tfe_h2_stream h2_stream[TFE_THREAD_MAX]; } Http2Plugin; typedef enum { diff --git a/plugin/protocol/http2/include/internal/http2_stream.h b/plugin/protocol/http2/include/internal/http2_stream.h index 6794691..3882d08 100644 --- a/plugin/protocol/http2/include/internal/http2_stream.h +++ b/plugin/protocol/http2/include/internal/http2_stream.h @@ -2,7 +2,7 @@ > File Name: http_stream.h > Author: > Mail: - > Created Time: 2018年09月12日 星期三 15时56分05秒 + > Created Time: 2018�09�12� 星期� 15�56�05� ************************************************************************/ #ifndef _HTTP_STREAM_H @@ -14,41 +14,40 @@ #include #include -typedef int (*event_cb)(struct http2_half_private * hf_private, +typedef int (*event_cb)(struct tfe_h2_half_private * hf_private, tfe_http_event ev, const unsigned char * data, size_t len, void * user); -enum nghttp2_manage_stage +enum h2_read_state { - MANAGE_STAGE_INIT, - MANAGE_STAGE_READING, - MANAGE_STAGE_COMPLETE, + H2_READ_STATE_BEGIN, + H2_READ_STATE_READING, + H2_READ_STATE_COMPLETE, }; -struct data_t +struct tfe_h2_payload { int gzip; uint8_t flags; ssize_t padlen; - struct z_stream_st *inflater; + struct z_stream_st *inflate; struct z_stream_st *deflate; struct evbuffer * evbuf_body; }; -struct header_data{ +struct tfe_h2_field{ + TAILQ_ENTRY(tfe_h2_field) next; nghttp2_nv nv; - struct http_field_name field; - struct header_data *prev, *next; + struct http_field_name *field; }; -struct http2_headers{ - int nvlen; - int complete; - uint8_t flag; - struct header_data *head, *tail; +struct tfe_h2_header{ + TAILQ_HEAD(h2_field_list_head, tfe_h2_field) h2_field_list; + int nvlen; + uint8_t flag; }; -struct http2_half_private +struct tfe_h2_half_private { /* PUBLIC STRUCTURE */ struct tfe_http_half half_public; @@ -60,19 +59,20 @@ struct http2_half_private int by_stream; char * url_storage; - struct data_t body; - struct http2_headers headers; - struct http2_headers promised; + struct tfe_h2_payload body; + struct tfe_h2_header header; + struct tfe_h2_header promised; /*Read Cache**/ int32_t stream_id; nghttp2_session *session; + struct tfe_h2_session *father_session; - enum nghttp2_manage_stage body_state; - enum nghttp2_manage_stage message_state; + enum h2_read_state body_state; + enum h2_read_state message_state; /* Callback Function */ - int (*event_cb)(struct http2_half_private * half_private, tfe_http_event ev, + int (*event_cb)(struct tfe_h2_half_private * half_private, tfe_http_event ev, const unsigned char * data, size_t len, void * user); /* Callback Function User Pointer */ void * event_cb_user; @@ -84,35 +84,32 @@ struct cache_trapper_t{ int spd_set; int spd_valid; int rse_set; - //int set_cnt; - //int flag_end; int spd_set_cnt; int spd_cnt; tfe_http_event spd_event; }; -struct h2_stream_data_t{ +struct tfe_h2_session{ struct tfe_http_session tfe_session; - TAILQ_ENTRY(h2_stream_data_t) next; + TAILQ_ENTRY(tfe_h2_session) next; const struct tfe_stream *tf_stream; - int32_t stream_id; - nghttp2_session *session; + int32_t ngh2_stream_id; + nghttp2_session *session; + struct tfe_h2_stream *father_stream; struct cache_trapper_t cache; struct http_frame_session_ctx *frame_ctx; - struct http2_half_private *req, *pangu_req; - struct http2_half_private *resp, *pangu_resp; + struct tfe_h2_half_private *req, *plugin_built_req; + struct tfe_h2_half_private *resp, *plugin_built_resp; }; -struct tfe_session_info_t +struct tfe_h2_stream { - TAILQ_HEAD(list_head, h2_stream_data_t) list; + TAILQ_HEAD(list_head, tfe_h2_session) h2_session_list; int goaway; - int32_t stream_id; - enum tfe_stream_action stream_action; unsigned int thread_id; @@ -130,67 +127,52 @@ struct stream_tap_info_t /** IS PREEMPTED */ unsigned int preempted; /** sess manage */ - struct tfe_session_info_t *session_info; + struct tfe_h2_stream *h2_stream_info; }; -/*STREAM STATE **/ -#define TFE_NGHTTP2_DATA 0x00000001 -#define TFE_NGHTTP2_HEADERS 0x00000002 -#define TFE_NGHTTP2_PRIORITY 0x00000020 -#define TFE_NGHTTP2_RST_STREAM 0x00000040 -#define TFE_NGHTTP2_SETTINGS 0x00000080 -#define TFE_NGHTTP2_PUSH_PROMISE 0x00000100 -#define TFE_NGHTTP2_PING 0x00000200 -#define TFE_NGHTTP2_GOAWAY 0x00000400 -#define TFE_NGHTTP2_WINDOW_UPDATE 0x00000800 -#define TFE_NGHTTP2_CONTINUATION 0x00001000 -#define TFE_NGHTTP2_ALTSVC 0x00002000 -#define TFE_NGHTTP2_RESPONSE 0x00004000 - #define ACTION_USER_DATA 3 -#define NGHTTP2_NO_USER_ERROR 0x0e -static inline const struct http2_half_private * +static inline const struct tfe_h2_half_private * nghttp2_to_half_private(const struct tfe_http_half * half_public) { if(!half_public) return NULL; - return container_of(half_public, struct http2_half_private, half_public); + return container_of(half_public, struct tfe_h2_half_private, half_public); } -static inline struct http2_half_private * +static inline struct tfe_h2_half_private * nghttp2_to_half_private(struct tfe_http_half * half_public) { if(!half_public) return NULL; - return container_of(half_public, struct http2_half_private, half_public); + return container_of(half_public, struct tfe_h2_half_private, half_public); } -static inline struct h2_stream_data_t * +static inline struct tfe_h2_session * nghttp2_to_stream_data(struct tfe_http_session * hs_public) { if (hs_public == NULL) return NULL; - return container_of(hs_public, struct h2_stream_data_t, tfe_session); + return container_of(hs_public, struct tfe_h2_session, tfe_session); } -extern struct tfe_session_info_t* tfe_session_info_init(); -extern void sess_data_ctx_fini(struct tfe_session_info_t *session_info, const struct tfe_stream * stream, +extern struct tfe_h2_stream* tfe_session_info_init(); +extern void sess_data_ctx_fini(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream * stream, unsigned int thread_id); extern enum tfe_stream_action -detect_down_stream_protocol(struct tfe_session_info_t *session_info, const struct tfe_stream *tfe_stream, +detect_down_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream, unsigned int thread_id, const unsigned char *data, size_t len); extern enum tfe_stream_action -detect_up_stream_protocol(struct tfe_session_info_t *session_info, const struct tfe_stream *tfe_stream, +detect_up_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream, unsigned int thread_id, const unsigned char *data, size_t len); enum tfe_stream_action -nghttp2_client_mem_send(struct tfe_session_info_t *session_info); +nghttp2_client_mem_send(struct tfe_h2_stream *h2_stream_info); enum tfe_stream_action -nghttp2_server_mem_send(struct tfe_session_info_t *session_info); +nghttp2_server_mem_send(struct tfe_h2_stream *h2_stream_info); /*for test **/ diff --git a/plugin/protocol/http2/src/http2_common.cpp b/plugin/protocol/http2/src/http2_common.cpp index bd3e368..c58eb9c 100644 --- a/plugin/protocol/http2/src/http2_common.cpp +++ b/plugin/protocol/http2/src/http2_common.cpp @@ -72,7 +72,7 @@ str_to_val_idx(const char *val, const struct value_string *vs) i++; } } - return -1; + return 0; } const char* @@ -257,7 +257,7 @@ int deflate_init(struct z_stream_st **strm, int gzip) return ret; } -int defalta_br_write(struct z_stream_st **strm, +int deflate_br_write(struct z_stream_st **strm, const unsigned char * source, size_t slen, struct evbuffer * evbuf, int end) { @@ -363,7 +363,7 @@ int deflate_write(struct z_stream_st **strm, const uint8_t *source, ret = defalta_gzip_write(strm, source, slen, evbuf, end); } if (gzip == HTTP2_CONTENT_ENCODING_BR){ - ret = defalta_br_write(strm, source, slen, evbuf, end); + ret = deflate_br_write(strm, source, slen, evbuf, end); } return ret; diff --git a/plugin/protocol/http2/src/http2_plugin.cpp b/plugin/protocol/http2/src/http2_plugin.cpp index 2827ebd..3f1c30f 100644 --- a/plugin/protocol/http2/src/http2_plugin.cpp +++ b/plugin/protocol/http2/src/http2_plugin.cpp @@ -61,8 +61,8 @@ void load_logging_conf(const char *config) UNUSED static void http2_plugin_timer_gc_cb(evutil_socket_t fd, short what, void * arg) { - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)arg; - sess_data_ctx_fini(session_info, NULL, session_info->thread_id); + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)arg; + sess_data_ctx_fini(h2_stream_info, NULL, h2_stream_info->thread_id); } static int @@ -76,15 +76,15 @@ http2_plugin_init(struct tfe_proxy * proxy) for (unsigned int thread_id = 0; thread_id < thread_nu; thread_id++){ #if 0 - struct tfe_session_info_t session_info = plugin->session_info[thread_id]; - TAILQ_INIT(&(session_info.list)); - session_info.thread_id = thread_id; - session_info.as_client = 0; + struct tfe_session_info_t h2_stream_info = plugin->h2_stream_info[thread_id]; + TAILQ_INIT(&(h2_stream_info.list)); + h2_stream_info.thread_id = thread_id; + h2_stream_info.as_client = 0; struct event_base * ev_base = tfe_proxy_get_work_thread_evbase(thread_id); struct timeval timer = {0, 500 * 1000}; - struct event * event = event_new(ev_base, -1, EV_PERSIST, http2_plugin_timer_gc_cb, &session_info); + struct event * event = event_new(ev_base, -1, EV_PERSIST, http2_plugin_timer_gc_cb, &h2_stream_info); if (unlikely(event == NULL)){ TFE_LOG_ERROR(logger()->handle, "Create timer error, init failed!"); @@ -113,7 +113,7 @@ http2_stream_open(const struct tfe_stream *stream, unsigned int thread_id, memset(tapinfo, 0, sizeof(struct stream_tap_info_t)); tapinfo->preempted = 0; - tapinfo->session_info = tfe_session_info_init(); + tapinfo->h2_stream_info = tfe_session_info_init(); *pme = (void *)tapinfo; @@ -180,8 +180,8 @@ http2_stream_data(const struct tfe_stream * stream, unsigned int thread_id, tapinfo->preempted = 1; } - return (dir == CONN_DIR_DOWNSTREAM) ? detect_down_stream_protocol(tapinfo->session_info, stream, thread_id, - data, len) : detect_up_stream_protocol(tapinfo->session_info, stream, thread_id, data, len); + return (dir == CONN_DIR_DOWNSTREAM) ? detect_down_stream_protocol(tapinfo->h2_stream_info, stream, thread_id, + data, len) : detect_up_stream_protocol(tapinfo->h2_stream_info, stream, thread_id, data, len); detach: tfe_stream_detach(stream); @@ -195,10 +195,10 @@ http2_stream_close(const struct tfe_stream * stream, unsigned int thread_id, { struct stream_tap_info_t *tapinfo = (struct stream_tap_info_t *)(*pme); - sess_data_ctx_fini(tapinfo->session_info, stream, thread_id); + sess_data_ctx_fini(tapinfo->h2_stream_info, stream, thread_id); - free(tapinfo->session_info); - tapinfo->session_info = NULL; + free(tapinfo->h2_stream_info); + tapinfo->h2_stream_info = NULL; free(tapinfo); tapinfo = NULL; @@ -213,7 +213,7 @@ static struct tfe_plugin __nghttp2_plugin_info = .symbol = "HTTP2", .type = TFE_PLUGIN_TYPE_PROTOCOL, .on_init = http2_plugin_init, - .on_deinit = http2_plugin_deinit, + .on_deinit = http2_plugin_deinit, .on_open = http2_stream_open, .on_data = http2_stream_data, .on_close = http2_stream_close diff --git a/plugin/protocol/http2/src/http2_stream.cpp b/plugin/protocol/http2/src/http2_stream.cpp index f12215f..7ebc7db 100644 --- a/plugin/protocol/http2/src/http2_stream.cpp +++ b/plugin/protocol/http2/src/http2_stream.cpp @@ -50,7 +50,7 @@ static const struct value_string method_vals[] = {NGHTTP2_METHOD_UNKNOWN, "unknown"}, }; -static const struct value_string headers_vals[] = +static const struct value_string header_vals[] = { {TFE_HTTP_UNKNOWN_FIELD, "unkown"}, {TFE_HTTP_HOST, ":authority"}, @@ -99,79 +99,50 @@ struct user_event_dispatch }; /*up stream */ -static struct h2_stream_data_t * -TAILQ_LIST_FIND(struct tfe_session_info_t *session_info, int32_t stream_id) +static struct tfe_h2_session * +TAILQ_LIST_FIND(struct tfe_h2_stream *h2_stream_info, int32_t stream_id) { - struct h2_stream_data_t *stream = NULL, *_next_stream = NULL; + struct tfe_h2_session *stream = NULL, *_next_stream = NULL; - TAILQ_FOREACH_SAFE(stream, &session_info->list, next, _next_stream) + TAILQ_FOREACH_SAFE(stream, &h2_stream_info->h2_session_list, next, _next_stream) { - if (stream->stream_id == stream_id){ + if (stream->ngh2_stream_id == stream_id){ break; } } return stream; } -/** begin */ -/** headers list, After the test is completed, you can delete it */ -#define foreach_headers(list, entry) \ - for(entry = ((struct http2_headers*)(list))->head; entry; entry = entry->next) - -static struct header_data* -headers_del(struct http2_headers *list, struct header_data *entry) +static void +tfe_h2_header_add_field(struct tfe_h2_header *h2_header, const struct http_field_name * field, const char * value, int at_tail) { - if (entry->prev) - entry->prev->next = entry->next; - else - list->head = entry->next; - - if (entry->next) - entry->next->prev = entry->prev; - else - list->tail = entry->prev; - - entry->next = entry->prev = NULL; - list->nvlen--; - - return entry; -} - -void -headers_add_head(struct http2_headers *list, struct header_data *entry) -{ - if (list->head){ - entry->next = list->head; - list->head->prev = entry; - }else{ - list->tail = entry; + struct tfe_h2_field *peer_h2_field = ALLOC(struct tfe_h2_field, 1); + peer_h2_field->field = http_field_name_duplicate(field); + if (peer_h2_field->field->field_id == TFE_HTTP_UNKNOWN_FIELD) + { + peer_h2_field->nv.name = (uint8_t *)peer_h2_field->field->field_name; + peer_h2_field->nv.namelen = strlen(peer_h2_field->field->field_name); + }else + { + const char *std_name = val_to_str(field->field_id, header_vals); + peer_h2_field->nv.name = (uint8_t *)tfe_strdup((const char *)std_name); + peer_h2_field->nv.namelen = strlen(std_name); } - list->head = entry; - list->nvlen++; -} - -void -headers_add_tail(struct http2_headers *list, struct header_data *entry) -{ - entry->next = NULL; - entry->prev = list->tail; - - if (list->tail) - list->tail->next = entry; - else - list->head = entry; - - list->tail = entry; - list->nvlen++; + peer_h2_field->nv.value = (uint8_t *)tfe_strdup((const char *)value);; + peer_h2_field->nv.valuelen = strlen(value); + h2_header->nvlen++; + if (at_tail) + TAILQ_INSERT_TAIL(&h2_header->h2_field_list, peer_h2_field, next); + else + TAILQ_INSERT_HEAD(&h2_header->h2_field_list, peer_h2_field, next); } static inline void -headers_init(struct http2_headers *headers) +headers_init(struct tfe_h2_header *header) { - memset(headers, 0, sizeof(struct http2_headers)); - headers->nvlen = 0; - headers->flag = 0; - headers->head = headers->tail = 0; + header->nvlen = 0; + header->flag = 0; + TAILQ_INIT(&header->h2_field_list); } static int @@ -198,30 +169,41 @@ method_to_str_idx(const char * method) } static nghttp2_nv* -nghttp2_nv_packet(struct http2_headers *headers, nghttp2_nv *hdrs) +nghttp2_nv_packet(struct tfe_h2_header *header, nghttp2_nv *hdrs) { int nvlen = 0; - struct header_data *header = NULL; + struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL; - foreach_headers(headers, header){ - hdrs[nvlen].name = header->nv.name; - hdrs[nvlen].namelen = header->nv.namelen; - hdrs[nvlen].value = header->nv.value; - hdrs[nvlen].valuelen = header->nv.valuelen; - hdrs[nvlen].flags = header->nv.flags; + TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field){ + hdrs[nvlen].name = h2_field->nv.name; + hdrs[nvlen].namelen = h2_field->nv.namelen; + hdrs[nvlen].value = h2_field->nv.value; + hdrs[nvlen].valuelen = h2_field->nv.valuelen; + hdrs[nvlen].flags = h2_field->nv.flags; nvlen++; } return hdrs; } static enum tfe_http_std_method -nghttp2_get_method(struct http2_half_private *half_private) +nghttp2_get_method(struct tfe_h2_half_private *half_private) { struct tfe_http_req_spec *req_spec = &(half_private->half_public.req_spec); return req_spec->method; } +static nghttp2_session * tfe_h2_stream_get_nghttp2_session(struct tfe_h2_stream *connection, enum tfe_conn_dir dir) +{ + return (dir==CONN_DIR_UPSTREAM?connection->as_server:connection->as_client); +} + +static nghttp2_session * tfe_h2_stream_get_nghttp2_peer_session(struct tfe_h2_stream *connection, enum tfe_conn_dir dir) +{ + return (dir==CONN_DIR_UPSTREAM?connection->as_client: connection->as_server); +} + + static nghttp2_settings_entry* nghttp2_iv_packet(nghttp2_settings settings, nghttp2_settings_entry *out_iv) @@ -238,28 +220,33 @@ nghttp2_iv_packet(nghttp2_settings settings, } static void -delete_nv_packet_data(struct http2_headers *headers) +delete_nv_packet_data(struct tfe_h2_header *header) { - struct header_data *header = NULL; + struct tfe_h2_field *h2_filed=NULL, *peer_h2_filed=NULL; - foreach_headers(headers, header){ - free(header->nv.name); - header->nv.name = NULL; - header->nv.namelen = 0; + TAILQ_FOREACH_SAFE(h2_filed, &header->h2_field_list, next, peer_h2_filed) + { + TAILQ_REMOVE(&header->h2_field_list, h2_filed, next); - free(header->nv.value); - header->nv.value = NULL; + free(h2_filed->nv.name); + h2_filed->nv.name = NULL; + h2_filed->nv.namelen = 0; - header->nv.valuelen = 0; - header->nv.flags = 0; - headers_del(headers, header); + free(h2_filed->nv.value); + h2_filed->nv.value = NULL; + h2_filed->nv.valuelen = 0; + + free(h2_filed->field); + h2_filed->field = NULL; + + free(h2_filed); + h2_filed = NULL; } - headers->nvlen = 0; - headers->flag = 0; - headers->head = headers->tail = NULL; + header->nvlen = 0; + header->flag = 0; } -static int event_dispatch_cb(struct http2_half_private * half_private, +static int event_dispatch_cb(struct tfe_h2_half_private * half_private, enum tfe_http_event ev, const unsigned char * data, size_t len, void * user) { struct user_event_dispatch *event = (struct user_event_dispatch *)user; @@ -271,131 +258,114 @@ static int event_dispatch_cb(struct http2_half_private * half_private, return 0; } -void half_set_callback(struct http2_half_private * half_private, +void half_set_callback(struct tfe_h2_half_private * half_private, void * user, void (* user_deleter)(void *)) { - if (half_private->event_cb == NULL) - half_private->event_cb = event_dispatch_cb; + half_private->event_cb = event_dispatch_cb; half_private->event_cb_user = user; half_private->event_cb_user_deleter = user_deleter; } -static const char * -half_ops_field_read(const struct tfe_http_half * half, const struct http_field_name * field) -{ - struct header_data *header = NULL; - const struct http2_half_private *half_private = nghttp2_to_half_private(half); +const char * h2_half_ops_field_read(const struct tfe_http_half * half, const struct http_field_name * field) +{ + const struct tfe_h2_half_private *half_private = nghttp2_to_half_private(half); if (unlikely(half_private == NULL)) - return NULL; - - foreach_headers(&half_private->headers, header){ - if (http_field_name_compare(&header->field, field) != 0) continue; - break; + return NULL; + + struct tfe_h2_field *h2_field=NULL, *peer_h2_field=NULL; + const struct tfe_h2_header *h2_header =&(half_private->header); + + TAILQ_FOREACH(h2_field, &h2_header->h2_field_list, next) + { + if (http_field_name_compare(h2_field->field, field) != 0) continue; + peer_h2_field = h2_field; + break; } - return header != NULL ? (const char *)header->nv.value : NULL; + + return peer_h2_field != NULL ? (const char *)peer_h2_field->nv.value : NULL; } -static int -half_ops_field_write(struct tfe_http_half * half, const struct http_field_name * name, const char * value) +int h2_half_ops_field_write(struct tfe_http_half * half, const struct http_field_name * field, const char * value) { - int is_exist = 0; - struct header_data *header = NULL; - struct http2_half_private *half_private = nghttp2_to_half_private(half); - struct http2_headers *headers = &half_private->headers; + struct tfe_h2_half_private *half_private = nghttp2_to_half_private(half); + struct tfe_h2_header *h2_header = &(half_private->header); + struct tfe_h2_field *h2_field=NULL,*peer_h2_field=NULL; - if (unlikely(half_private == NULL) || unlikely(headers == NULL)) - goto finish; + TAILQ_FOREACH(h2_field, &h2_header->h2_field_list, next) + { + if (http_field_name_compare(h2_field->field, field) != 0) continue; + peer_h2_field = h2_field; + break; + } - foreach_headers(headers, header){ - if (header->field.field_id == TFE_HTTP_UNKNOWN_FIELD) - continue; - if (header->field.field_id == name->field_id){ - if (value){ - free(header->nv.value); - header->nv.value = (uint8_t*)tfe_strdup(value); - header->nv.valuelen = strlen(value); - header->field.field_name = (const char *)header->nv.name; - is_exist = 1; - }else{ - headers_del(headers, header); - } - break; - } + if (peer_h2_field != NULL && value != NULL) + { + FREE(&(peer_h2_field->nv.value)); + peer_h2_field->nv.value = (uint8_t*)tfe_strdup(value); + peer_h2_field->nv.valuelen = strlen(value); } - if (is_exist == 0){ - if (value == NULL){ - goto finish; - } - header = ALLOC(struct header_data, 1); - memset(header, 0, sizeof(struct header_data)); - - if (name->field_id == TFE_HTTP_UNKNOWN_FIELD){ - header->field.field_id = TFE_HTTP_UNKNOWN_FIELD; - header->nv.name = (uint8_t *)tfe_strdup(name->field_name); - header->nv.namelen = strlen(name->field_name); - header->field.field_name = (const char *)header->nv.name;; - }else{ - const char *std_name = val_to_str(name->field_id, headers_vals); - header->field.field_id = name->field_id; - header->field.field_name = NULL; - header->nv.name = (uint8_t *)tfe_strdup((const char *)std_name); - header->nv.namelen = strlen(std_name); - } - header->nv.value = (uint8_t *)tfe_strdup((const char *)value);; - header->nv.valuelen = strlen(value); - headers_add_tail(headers, header); + else if (peer_h2_field != NULL && value == NULL) + { + TAILQ_REMOVE(&h2_header->h2_field_list, peer_h2_field, next); + free(peer_h2_field->nv.name); + free(peer_h2_field->nv.value); + free(peer_h2_field); + h2_header->nvlen--; + } + else if (peer_h2_field == NULL && value != NULL) + { + tfe_h2_header_add_field(h2_header, field, value, 1); + } + else + { + return -1; } -finish: return 0; } static struct tfe_http_half * -half_ops_allow_write(const struct tfe_http_half * half) +h2_half_ops_allow_write(const struct tfe_http_half * half) { return (struct tfe_http_half *) half; } -static const char * -half_ops_field_iterate(const struct tfe_http_half * half, void ** iter, struct http_field_name * field) +const char * h2_half_ops_field_iterate(const struct tfe_http_half * half, void ** iter, struct http_field_name * field) { - const char *value = NULL; - struct header_data **head = (struct header_data **)iter; - const struct http2_half_private *half_private = nghttp2_to_half_private(half); + struct tfe_h2_field **h2_filed = (struct tfe_h2_field **)iter; + const struct tfe_h2_half_private *half_private = nghttp2_to_half_private(half); + const struct tfe_h2_header *header = &half_private->header; - if (unlikely(half_private == NULL)) - goto finish; - - if (NULL == *head){ - *head = half_private->headers.head; - }else{ - *head = (*head)->next; + if (*h2_filed == NULL) + { + *h2_filed = TAILQ_FIRST(&header->h2_field_list); + } + else + { + *h2_filed = TAILQ_NEXT(*h2_filed, next); } - if (NULL == *head) - goto finish; + if (*h2_filed == NULL) return NULL; - field->field_id = (*head)->field.field_id; - field->field_name = (*head)->field.field_name; - value = (const char *)(*head)->nv.value; - -finish: - return value; + /* Reference of inner data, user should copy it */ + field->field_id = (*h2_filed)->field->field_id; + field->field_name = (*h2_filed)->field->field_name; + return (const char *)(*h2_filed)->nv.value; } static int -half_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag) +h2_half_ops_append_body(struct tfe_http_half * half, char * buff, size_t size, int flag) { int xret = -1; - struct http2_half_private * resp = nghttp2_to_half_private(half); - struct data_t *body = &resp->body; + struct tfe_h2_half_private * resp = nghttp2_to_half_private(half); + struct tfe_h2_payload *body = &resp->body; if (buff == NULL && size == 0){ if (body->gzip != HTTP2_CONTENT_ENCODING_NONE){ xret = deflate_write(&body->deflate, NULL, 0, resp->body.evbuf_body, body->gzip, 1); } - resp->message_state = MANAGE_STAGE_COMPLETE; + resp->message_state = H2_READ_STATE_COMPLETE; goto finish; } @@ -414,14 +384,14 @@ finish: return xret; } -void delete_stream_half_data(struct http2_half_private **data, +void delete_stream_half_data(struct tfe_h2_half_private **data, int body_flag) { if (*data){ - struct data_t *body = &((*data)->body); + struct tfe_h2_payload *body = &((*data)->body); - inflate_finished(&body->inflater); + inflate_finished(&body->inflate); deflate_finished(&body->deflate); if (body->evbuf_body && body_flag){ evbuffer_free(body->evbuf_body); @@ -429,7 +399,7 @@ void delete_stream_half_data(struct http2_half_private **data, } if ((*data)->url_storage) FREE(&((*data)->url_storage)); - delete_nv_packet_data(&((*data)->headers)); + delete_nv_packet_data(&((*data)->header)); if((*data)->event_cb_user_deleter != NULL) (*data)->event_cb_user_deleter((*data)->event_cb_user); free(*data); @@ -438,9 +408,9 @@ void delete_stream_half_data(struct http2_half_private **data, return; } -void half_ops_free(struct tfe_http_half * half) +void h2_half_ops_free(struct tfe_http_half * half) { - struct http2_half_private * h2_private = nghttp2_to_half_private(half); + struct tfe_h2_half_private * h2_private = nghttp2_to_half_private(half); delete_stream_half_data(&h2_private, 1); @@ -452,22 +422,22 @@ void half_ops_free(struct tfe_http_half * half) int h2_half_ops_body_begin(struct tfe_http_half * half, int by_stream) { - struct http2_half_private * resp = nghttp2_to_half_private(half); - struct data_t *body = &resp->body; + struct tfe_h2_half_private * resp = nghttp2_to_half_private(half); + struct tfe_h2_payload *body = &resp->body; assert(body->evbuf_body == NULL); if (by_stream) { - if (body->inflater){ - inflate_finished(&body->inflater); + if (body->inflate){ + inflate_finished(&body->inflate); } if (body->deflate){ deflate_finished(&body->deflate); } body->gzip = HTTP2_CONTENT_ENCODING_NONE; - resp->message_state = MANAGE_STAGE_READING; + resp->message_state = H2_READ_STATE_READING; resp->by_stream = by_stream; } @@ -475,94 +445,55 @@ int h2_half_ops_body_begin(struct tfe_http_half * half, int by_stream) return 0; } -static tfe_stream_action -cache_frame_submit_data(int32_t stream_id, nghttp2_session *session); +uint32_t tfe_h2_half_get_ngh2_stream_id(struct tfe_h2_half_private *h2_half) +{ + struct tfe_h2_session* father_session=h2_half->father_session; + return father_session->ngh2_stream_id; +} -static enum tfe_stream_action -cache_frame_submit_header(nghttp2_session *as_server, - int32_t stream_id); - -int h2_half_ops_body_data(struct tfe_http_half * half, const unsigned char * data, size_t sz_data) +int h2_half_ops_body_data(struct tfe_http_half * h2_response, const unsigned char * data, size_t sz_data) { int xret = -1; - enum tfe_stream_action stream_action = ACTION_DROP_DATA; - - struct http2_half_private * resp = nghttp2_to_half_private(half); - struct data_t *body = &resp->body; - + struct tfe_h2_half_private * h2_resp_priv = nghttp2_to_half_private(h2_response); + struct tfe_h2_payload *body = &h2_resp_priv->body; + if (body->gzip != HTTP2_CONTENT_ENCODING_NONE){ xret = deflate_write(&body->deflate, (const uint8_t *)data, sz_data, - resp->body.evbuf_body, body->gzip, 0); + h2_resp_priv->body.evbuf_body, body->gzip, 0); }else{ - xret = evbuffer_add(resp->body.evbuf_body, data, sz_data); + xret = evbuffer_add(h2_resp_priv->body.evbuf_body, data, sz_data); } - - if (resp->by_stream){ - stream_action = cache_frame_submit_header(resp->session, resp->stream_id); - if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(resp->session); - if (xret != 0) { - stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal downstream send error: %s\n", - nghttp2_strerror(xret)); - } - } - stream_action = cache_frame_submit_data(resp->stream_id, resp->session); - if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(resp->session); - if (xret != 0) { - stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); - } - } - } - return xret; } int h2_half_ops_body_end(struct tfe_http_half * half) { - int xret = -1; - struct http2_half_private * resp = nghttp2_to_half_private(half); + struct tfe_h2_half_private * resp = nghttp2_to_half_private(half); - enum tfe_stream_action stream_action = ACTION_DROP_DATA; - - resp->body_state = MANAGE_STAGE_COMPLETE; - resp->message_state = MANAGE_STAGE_COMPLETE; - - if (resp->by_stream){ - resp->body.flags |= NGHTTP2_FLAG_END_STREAM; - stream_action = cache_frame_submit_data(resp->stream_id, resp->session); - if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(resp->session); - if (xret != 0) { - stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); - } - } - } + resp->body_state = H2_READ_STATE_COMPLETE; + resp->message_state = H2_READ_STATE_COMPLETE; return 0; } struct tfe_http_half_ops h2_half_ops = { - .ops_http_field_read = half_ops_field_read, - .ops_http_field_write = half_ops_field_write, - .ops_http_allow_write = half_ops_allow_write, - .ops_http_field_iterate = half_ops_field_iterate, - .ops_append_body = half_ops_append_body, - .ops_body_begin = h2_half_ops_body_begin, - .ops_body_data = h2_half_ops_body_data, - .ops_body_end = h2_half_ops_body_end, - .ops_free = half_ops_free + .ops_http_field_read = h2_half_ops_field_read, + .ops_http_field_write = h2_half_ops_field_write, + .ops_http_allow_write = h2_half_ops_allow_write, + .ops_http_field_iterate = h2_half_ops_field_iterate, + .ops_append_body = h2_half_ops_append_body, + .ops_body_begin = h2_half_ops_body_begin, + .ops_body_data = h2_half_ops_body_data, + .ops_body_end = h2_half_ops_body_end, + .ops_free = h2_half_ops_free }; static struct tfe_http_session* h2_ops_allow_write(const struct tfe_http_session * session) { - struct h2_stream_data_t *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); + struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); if ( http_frame_currect_plugin_preempt(stream_data->frame_ctx) == 0){ return (struct tfe_http_session *)session; @@ -573,7 +504,7 @@ h2_ops_allow_write(const struct tfe_http_session * session) void h2_ops_detach(const struct tfe_http_session * session) { - struct h2_stream_data_t *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); + struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); return http_frame_currect_plugin_detach(stream_data->frame_ctx); } @@ -586,7 +517,7 @@ void h2_ops_drop(struct tfe_http_session * session) void h2_ops_suspend(struct tfe_http_session * session) { #ifdef TFE_CACHE - struct h2_stream_data_t *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); + struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); stream_data->cache.spd_set = 1; #endif } @@ -594,7 +525,7 @@ void h2_ops_suspend(struct tfe_http_session * session) void h2_ops_resume(struct tfe_http_session * session) { #ifdef TFE_CACHE - struct h2_stream_data_t *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); + struct tfe_h2_session *stream_data = nghttp2_to_stream_data((struct tfe_http_session *)session); if (stream_data->cache.spd_valid){ tfe_stream_resume(stream_data->tf_stream); @@ -605,34 +536,35 @@ void h2_ops_resume(struct tfe_http_session * session) void h2_ops_request_set(struct tfe_http_session * session, struct tfe_http_half * req_user) { - struct h2_stream_data_t *stream_data = nghttp2_to_stream_data(session); - struct http2_half_private *half_user = nghttp2_to_half_private(req_user); + struct tfe_h2_session *stream_data = nghttp2_to_stream_data(session); + struct tfe_h2_half_private *half_user = nghttp2_to_half_private(req_user); - stream_data->pangu_req = half_user; + stream_data->plugin_built_req = half_user; } void h2_ops_response_set(struct tfe_http_session * session, struct tfe_http_half * resp) { - struct h2_stream_data_t *stream_data = nghttp2_to_stream_data(session); - struct http2_half_private *half_user = nghttp2_to_half_private(resp); + struct tfe_h2_session *stream_data = nghttp2_to_stream_data(session); + struct tfe_h2_half_private *half_user = nghttp2_to_half_private(resp); - stream_data->pangu_resp = half_user; + stream_data->plugin_built_resp = half_user; } -static struct http2_half_private* +static struct tfe_h2_half_private* tfe_half_private_init(enum tfe_http_direction direction, int32_t stream_id, nghttp2_session *session) { - struct http2_half_private *half_private = ALLOC(struct http2_half_private, 1); + struct tfe_h2_half_private *half_private = ALLOC(struct tfe_h2_half_private, 1); assert(half_private); - memset(half_private, 0, sizeof(struct http2_half_private)); + memset(half_private, 0, sizeof(struct tfe_h2_half_private)); half_private->half_public.direction = direction; half_private->half_public.major_version = 2; half_private->half_public.ops = &h2_half_ops; - headers_init(&half_private->headers); + headers_init(&half_private->header); + headers_init(&half_private->promised); half_private->body.evbuf_body = evbuffer_new(); half_private->body.gzip = HTTP2_CONTENT_ENCODING_NONE; half_private->body.padlen = 0; @@ -640,8 +572,8 @@ tfe_half_private_init(enum tfe_http_direction direction, int32_t stream_id, half_private->stream_id = stream_id; half_private->session = session; - half_private->body_state = MANAGE_STAGE_INIT; - half_private->message_state = MANAGE_STAGE_INIT; + half_private->body_state = H2_READ_STATE_BEGIN; + half_private->message_state = H2_READ_STATE_BEGIN; return half_private; } @@ -649,7 +581,7 @@ tfe_half_private_init(enum tfe_http_direction direction, int32_t stream_id, struct tfe_http_half * h2_ops_request_create(struct tfe_http_session * session, enum tfe_http_std_method method, const char * uri) { - struct http2_half_private * req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL); + struct tfe_h2_half_private * req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL); req->method_or_status = method; req->url_storage = tfe_strdup(uri); @@ -659,8 +591,8 @@ struct tfe_http_half * h2_ops_request_create(struct tfe_http_session * session, struct tfe_http_half * h2_ops_response_create(struct tfe_http_session * session, int resp_code) { - struct h2_stream_data_t *stream = nghttp2_to_stream_data(session); - struct http2_half_private * resp = tfe_half_private_init(TFE_HTTP_RESPONSE, stream->stream_id, + struct tfe_h2_session *stream = nghttp2_to_stream_data(session); + struct tfe_h2_half_private * resp = tfe_half_private_init(TFE_HTTP_RESPONSE, stream->ngh2_stream_id, stream->session); resp->method_or_status = resp_code; @@ -677,16 +609,16 @@ void h2_ops_kill(struct tfe_http_session * session) struct tfe_http_session_ops nghttp2_session_ops = { - .ops_allow_write = h2_ops_allow_write, - .ops_detach = h2_ops_detach, - .ops_drop = h2_ops_drop, - .ops_suspend = h2_ops_suspend, - .ops_resume = h2_ops_resume, - .ops_kill = h2_ops_kill, - .ops_request_set = h2_ops_request_set, - .ops_response_set = h2_ops_response_set, - .ops_request_create = h2_ops_request_create, - .ops_response_create = h2_ops_response_create + .ops_allow_write = h2_ops_allow_write, + .ops_detach = h2_ops_detach, + .ops_drop = h2_ops_drop, + .ops_suspend = h2_ops_suspend, + .ops_resume = h2_ops_resume, + .ops_kill = h2_ops_kill, + .ops_request_set = h2_ops_request_set, + .ops_response_set = h2_ops_response_set, + .ops_request_create = h2_ops_request_create, + .ops_response_create = h2_ops_response_create }; static ssize_t @@ -696,55 +628,41 @@ upstream_read_callback(nghttp2_session *session, int32_t stream_id, nghttp2_data_source *source, void *user_data) { -#define TFE_MAX_PAYLOADLEN 8192 - int datalen = 0; + unsigned char *input = NULL; + ssize_t datalen = 0, inputlen=0; - struct data_t *body = (struct data_t *)source->ptr; - if (!body || NULL == body->evbuf_body){ + struct tfe_h2_payload *to_send_body = (struct tfe_h2_payload *)source->ptr; + if (!to_send_body->evbuf_body || !(input = evbuffer_pullup(to_send_body->evbuf_body, -1)) + || 0==(inputlen = evbuffer_get_length(to_send_body->evbuf_body))) + { *data_flags |= NGHTTP2_DATA_FLAG_EOF; - return datalen; - } - - size_t inputlen = evbuffer_get_length(body->evbuf_body); - unsigned char *input = evbuffer_pullup(body->evbuf_body, -1); - - if (input == NULL || inputlen == 0){ - *data_flags |= NGHTTP2_DATA_FLAG_EOF; - goto finish; - } - - if (inputlen > TFE_MAX_PAYLOADLEN){ - datalen = TFE_MAX_PAYLOADLEN; - }else{ - datalen = inputlen; + return 0; } + datalen=MIN(length, inputlen); memcpy(buf, input, datalen); - evbuffer_drain(body->evbuf_body, datalen); - -finish: + evbuffer_drain(to_send_body->evbuf_body, datalen); return datalen; } static enum tfe_stream_action -nghttp2_server_frame_submit_response(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) +nghttp2_server_frame_submit_response(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { int rv = -1; - nghttp2_nv hdrs[128] = {0}; - struct http2_headers *headers = NULL; - struct http2_half_private *pangu_resp = NULL; + struct tfe_h2_header *h2_header = NULL; + struct tfe_h2_half_private *pangu_resp = NULL; - pangu_resp = h2_stream->pangu_resp; + pangu_resp = h2_session->plugin_built_resp; - if (pangu_resp->message_state != MANAGE_STAGE_COMPLETE){ + if (pangu_resp->message_state != H2_READ_STATE_COMPLETE){ return (enum tfe_stream_action)ACTION_USER_DATA; } - headers = &pangu_resp->headers; - if (headers->nvlen <= 0) + h2_header = &pangu_resp->header; + if (h2_header->nvlen <= 0) return ACTION_FORWARD_DATA; - struct data_t *body = &pangu_resp->body; + struct tfe_h2_payload *body = &pangu_resp->body; char str_sz_evbuf_body[TFE_STRING_MAX]; snprintf(str_sz_evbuf_body, sizeof(str_sz_evbuf_body) - 1, "%lu", evbuffer_get_length(body->evbuf_body)); @@ -754,84 +672,49 @@ nghttp2_server_frame_submit_response(struct tfe_session_info_t *session_info, nghttp2_data_provider data_prd; data_prd.source.ptr = (void *)body; data_prd.read_callback = upstream_read_callback; - - rv = nghttp2_submit_response(session_info->as_server, h2_stream->stream_id, nghttp2_nv_packet(headers, hdrs), - headers->nvlen, &data_prd); + + nghttp2_nv hdrs[h2_header->nvlen]; + rv = nghttp2_submit_response(h2_stream_info->as_server, h2_session->ngh2_stream_id, nghttp2_nv_packet(h2_header, hdrs), + h2_header->nvlen, &data_prd); if (rv != 0){ return ACTION_FORWARD_DATA; } - delete_nv_packet_data(headers); + delete_nv_packet_data(h2_header); return ACTION_DROP_DATA; } -static tfe_stream_action -cache_frame_submit_data(int32_t stream_id, nghttp2_session *session) -{ - int rv = -1; - struct http2_headers *headers = NULL; - struct h2_stream_data_t *h2_stream = NULL; - struct http2_half_private *pangu_resp = NULL; - - enum tfe_stream_action stream_action = ACTION_DROP_DATA; - - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session, stream_id); - if (!h2_stream){ - stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Cache id %d, Flow is empty", stream_id); - return stream_action; - } - pangu_resp = h2_stream->pangu_resp; - - headers = &pangu_resp->headers; - if (headers->nvlen > 0){ - return ACTION_FORWARD_DATA; - } - - struct data_t *body = &pangu_resp->body; - nghttp2_data_provider data_prd; - data_prd.source.ptr = (void *)body; - data_prd.read_callback = upstream_read_callback; - - rv = nghttp2_submit_data(session, body->flags, - stream_id, &data_prd); - if (rv != 0){ - printf("Fatal data error: %s\n", nghttp2_strerror(rv)); - } - return stream_action; -} - static enum tfe_stream_action -server_frame_submit_data(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream, +server_frame_submit_data(struct tfe_h2_stream *connection, + struct tfe_h2_session *h2_session, enum tfe_conn_dir dir) { enum tfe_stream_action stream_action = ACTION_DROP_DATA; - struct http2_half_private *resp = (dir == CONN_DIR_UPSTREAM) ? h2_stream->resp : h2_stream->req; - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; + struct tfe_h2_half_private *resp = (dir == CONN_DIR_UPSTREAM) ? h2_session->resp : h2_session->req; + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); - if (h2_stream->pangu_resp){ - stream_action = nghttp2_server_frame_submit_response(session_info, h2_stream); + if (h2_session->plugin_built_resp){ + stream_action = nghttp2_server_frame_submit_response(connection, h2_session); }else{ int rv = -1; - struct data_t *body = &resp->body; + struct tfe_h2_payload *body = &resp->body; - nghttp2_data_provider data_prd; - data_prd.source.ptr = (void *)body; - data_prd.read_callback = upstream_read_callback; + nghttp2_data_provider upstream_data_provider; + upstream_data_provider.source.ptr = (void *)body; + upstream_data_provider.read_callback = upstream_read_callback; - rv = nghttp2_submit_data(session, body->flags, - h2_stream->stream_id, &data_prd); + rv = nghttp2_submit_data(ngh2_session, body->flags, + h2_session->ngh2_stream_id, &upstream_data_provider); if (rv != 0){ stream_action = ACTION_FORWARD_DATA; - printf("Fatal data error: %s\n", nghttp2_strerror(rv)); + //printf("Fatal server submit data error: %s\n", nghttp2_strerror(rv)); } } return stream_action; } -typedef int (*nghttp2_frame_callback) (struct tfe_session_info_t *, const nghttp2_frame *, +typedef int (*nghttp2_frame_callback) (struct tfe_h2_stream *, const nghttp2_frame *, enum tfe_conn_dir dir); typedef int (*nghttp2_callback) (nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name, @@ -839,75 +722,65 @@ typedef int (*nghttp2_callback) (nghttp2_session *session, const nghttp2_frame * size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir); static int -nghttp2_submit_frame_priority(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_priority(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1; enum tfe_stream_action stream_action = ACTION_DROP_DATA; const nghttp2_priority *priority = &frame->priority; - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; - int rv = nghttp2_submit_priority(session, priority->hd.flags, priority->hd.stream_id, + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); + + int rv = nghttp2_submit_priority(ngh2_session, priority->hd.flags, priority->hd.stream_id, &(priority->pri_spec)); if (rv != 0){ stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit ping error: %s\n", dir, nghttp2_strerror(rv)); - goto finish; + return 0; } - xret = nghttp2_session_send(session); + xret = nghttp2_session_send(ngh2_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret)); } -finish: - - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit priority, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); - #endif - session_info->stream_action = stream_action; + connection->stream_action = stream_action; return 0; } static int -nghttp2_submit_frame_rst_stream(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_rst_stream(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1; enum tfe_stream_action stream_action = ACTION_DROP_DATA; const nghttp2_rst_stream *rst_stream = &frame->rst_stream; - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; - int rv = nghttp2_submit_rst_stream(session, rst_stream->hd.flags, + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); + + int rv = nghttp2_submit_rst_stream(ngh2_session, rst_stream->hd.flags, rst_stream->hd.stream_id, rst_stream->error_code); if (rv != 0){ stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit rst error: %s\n", dir, nghttp2_strerror(rv)); - goto finish; + return 0; } - xret = nghttp2_session_send(session); + xret = nghttp2_session_send(ngh2_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret)); } -finish: - session_info->stream_action = stream_action; - - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit rst stream, stream_id:%d, action:%d, error_code = %d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action, rst_stream->error_code); - #endif + connection->stream_action = stream_action; return 0; } static int -nghttp2_submit_frame_settings(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_settings(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1, rv = -1; @@ -915,83 +788,77 @@ nghttp2_submit_frame_settings(struct tfe_session_info_t *session_info,const nght enum tfe_stream_action stream_action = ACTION_DROP_DATA; nghttp2_settings settings = frame->settings; - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; - nghttp2_session *_session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_client : session_info->as_server; + + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); + nghttp2_session *ngh2_peer_session = tfe_h2_stream_get_nghttp2_peer_session(connection, dir); if(settings.hd.flags == NGHTTP2_FLAG_ACK){ - xret = nghttp2_session_send(_session); + xret = nghttp2_session_send(ngh2_peer_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); + TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s, %d\n",nghttp2_strerror(xret), __LINE__); } stream_action = ACTION_DROP_DATA; - goto finish; + return 0; } - rv = nghttp2_submit_settings(session, settings.hd.flags, + rv = nghttp2_submit_settings(ngh2_session, settings.hd.flags, nghttp2_iv_packet(settings, iv), settings.niv); if (rv != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit settings error: %s\n", dir, nghttp2_strerror(rv)); - goto finish; + return 0; } - xret = nghttp2_session_send(session); + xret = nghttp2_session_send(ngh2_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret)); } -finish: - session_info->stream_action = stream_action; - + connection->stream_action = stream_action; #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit setting, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); + TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit setting, stream_id:%d, action:%d", connection->tf_stream->str_stream_info, + dir, frame->hd.stream_id, connection->stream_action); #endif return 0; } static int -nghttp2_submit_frame_ping(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_ping(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1 ,rv = -1; enum tfe_stream_action stream_action = ACTION_DROP_DATA; const nghttp2_ping *ping = &frame->ping; - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; - rv = nghttp2_submit_ping(session, ping->hd.flags, + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); + + rv = nghttp2_submit_ping(ngh2_session, ping->hd.flags, ping->opaque_data); if (rv != 0){ stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit ping error: %s\n", dir, nghttp2_strerror(rv)); - goto finish; + return 0; } - xret = nghttp2_session_send(session); + xret = nghttp2_session_send(ngh2_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret)); } -finish: - session_info->stream_action = stream_action; - - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit ping, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); - #endif + connection->stream_action = stream_action; return 0; } void -nghttp2_write_access_log(struct h2_stream_data_t *h2_stream, const char * str_stream_info) +nghttp2_write_access_log(struct tfe_h2_session *h2_session, const char * str_stream_info) { /* Request */ - struct http2_half_private *req = h2_stream->req; + struct tfe_h2_half_private *req = h2_session->req; /* Response */ - struct http2_half_private *resp = h2_stream->resp; + struct tfe_h2_half_private *resp = h2_session->resp; /* Req-Public */ struct tfe_http_req_spec *req_spec = req ? &(req->half_public.req_spec) : NULL; /* Resp-Public */ @@ -1008,58 +875,58 @@ nghttp2_write_access_log(struct h2_stream_data_t *h2_stream, const char * str_st const char * cont_type = resp_spec ? resp_spec->content_type != NULL ? resp_spec->content_type : "-" : "-"; const char * cont_encoding = resp_spec ? resp_spec->content_encoding != NULL ? resp_spec->content_encoding : "-" : "-"; - const char * pangu_req = h2_stream->pangu_req ? "USER/REQ" : "-"; - const char * pangu_resp = h2_stream->pangu_resp ? "USER/RESP" : "-"; - //const char * __str_suspend = h2_stream->suspend_counter > 0 ? "SUSPEND" : "-"; + const char * pangu_req = h2_session->plugin_built_req ? "USER/REQ" : "-"; + const char * pangu_resp = h2_session->plugin_built_resp ? "USER/RESP" : "-"; + //const char * __str_suspend = h2_session->suspend_counter > 0 ? "SUSPEND" : "-"; char *access_log; - asprintf(&access_log, "%s %d %s %s HTTP2.0 %s %s %s %s %s", str_stream_info, h2_stream->tfe_session.session_id, + asprintf(&access_log, "%s %d %s %s HTTP2.0 %s %s %s %s %s", str_stream_info, h2_session->tfe_session.session_id, method, url, resp_code, cont_type, cont_encoding, pangu_req, pangu_resp); TFE_LOG_INFO(logger()->handle, "%s", access_log); free(access_log); } -void delete_http2_stream_data(struct h2_stream_data_t *h2_stream, +void delete_http2_stream_data(struct tfe_h2_session *h2_session, const struct tfe_stream *tf_stream, int body_flag) { - delete_stream_half_data(&h2_stream->req, body_flag); + delete_stream_half_data(&h2_session->req, body_flag); - delete_stream_half_data(&h2_stream->resp, body_flag); + delete_stream_half_data(&h2_session->resp, body_flag); } -void nghttp2_disect_goaway(struct tfe_session_info_t *session_info) +void nghttp2_disect_goaway(struct tfe_h2_stream *h2_stream_info) { - unsigned int thread_id = session_info->thread_id; - const struct tfe_stream * stream = session_info->tf_stream; + unsigned int thread_id = h2_stream_info->thread_id; + const struct tfe_stream * stream = h2_stream_info->tf_stream; - struct h2_stream_data_t *h2_stream = NULL; - struct h2_stream_data_t *_h2_stream = NULL; + struct tfe_h2_session *h2_session = NULL; + struct tfe_h2_session *peer_h2_stream = NULL; - TAILQ_FOREACH_SAFE(h2_stream, &session_info->list, next, _h2_stream){ - TAILQ_REMOVE(&session_info->list, h2_stream, next); - if (h2_stream->frame_ctx){ - http_frame_raise_session_end(h2_stream->frame_ctx, stream, &h2_stream->tfe_session, + TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream){ + TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next); + if (h2_session->frame_ctx){ + http_frame_raise_session_end(h2_session->frame_ctx, stream, &h2_session->tfe_session, thread_id); - h2_stream->frame_ctx = NULL; + h2_session->frame_ctx = NULL; } - delete_http2_stream_data(h2_stream, session_info->tf_stream, 1); - free(h2_stream); - h2_stream = NULL; + delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1); + free(h2_session); + h2_session = NULL; } - if (session_info->as_client){ - nghttp2_session_del(session_info->as_client); - session_info->as_client = NULL; + if (h2_stream_info->as_client){ + nghttp2_session_del(h2_stream_info->as_client); + h2_stream_info->as_client = NULL; } - if (session_info->as_server){ - nghttp2_session_del(session_info->as_server); - session_info->as_server = NULL; + if (h2_stream_info->as_server){ + nghttp2_session_del(h2_stream_info->as_server); + h2_stream_info->as_server = NULL; } } static int -nghttp2_submit_frame_goaway(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_goaway(struct tfe_h2_stream *connection, const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1; @@ -1067,9 +934,9 @@ nghttp2_submit_frame_goaway(struct tfe_session_info_t *session_info,const nghttp char error[1024] = {0}; const nghttp2_goaway *goaway = &frame->goaway; - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); - int rv = nghttp2_submit_goaway(session, goaway->hd.flags, goaway->last_stream_id, + int rv = nghttp2_submit_goaway(ngh2_session, goaway->hd.flags, goaway->last_stream_id, goaway->error_code, goaway->opaque_data, goaway->opaque_data_len); if (rv != 0){ stream_action = ACTION_FORWARD_DATA; @@ -1077,7 +944,7 @@ nghttp2_submit_frame_goaway(struct tfe_session_info_t *session_info,const nghttp dir, nghttp2_strerror(rv)); goto finish; } - xret = nghttp2_session_send(session); + xret = nghttp2_session_send(ngh2_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", @@ -1085,70 +952,62 @@ nghttp2_submit_frame_goaway(struct tfe_session_info_t *session_info,const nghttp } finish: snprintf(error, goaway->opaque_data_len, "%s", goaway->opaque_data); - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit goaway, stream_id:%d, action:%d, errod_code:%d, data:%s", session_info->tf_stream->str_stream_info, - dir, goaway->last_stream_id, session_info->stream_action, goaway->error_code, goaway->opaque_data); + TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit goaway, stream_id:%d, action:%d, errod_code:%d, data:%s", connection->tf_stream->str_stream_info, + dir, goaway->last_stream_id, connection->stream_action, goaway->error_code, goaway->opaque_data); - session_info->goaway = 1; - session_info->stream_action = stream_action; + connection->goaway = 1; + connection->stream_action = stream_action; return 0; } static int -nghttp2_submit_frame_window_update(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_window_update(struct tfe_h2_stream *connection,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1; enum tfe_stream_action stream_action = ACTION_DROP_DATA; const nghttp2_window_update *window_update = &(frame->window_update); - nghttp2_session *session = (dir == CONN_DIR_UPSTREAM) ? session_info->as_server : session_info->as_client; - int rv = nghttp2_submit_window_update(session, window_update->hd.flags,window_update->hd.stream_id, + nghttp2_session *ngh2_session = tfe_h2_stream_get_nghttp2_session(connection, dir); + + int rv = nghttp2_submit_window_update(ngh2_session, window_update->hd.flags,window_update->hd.stream_id, window_update->window_size_increment); if (rv != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Submit window error: %s\n", dir, nghttp2_strerror(rv)); - goto finish; + return 0; } - xret = nghttp2_session_send(session); + xret = nghttp2_session_send(ngh2_session); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret)); } -finish: - session_info->stream_action = stream_action; - - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit window update, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); - #endif + connection->stream_action = stream_action; return 0; } static int -nghttp2_submit_end_header(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) +nghttp2_submit_end_header(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { int xret = -1; int32_t stream_id = 0; - nghttp2_nv hdrs[128] = {0}; - struct http2_headers headers; - struct http2_half_private *resp = h2_stream->resp; + struct tfe_h2_header headers; + struct tfe_h2_half_private *resp = h2_session->resp; enum tfe_stream_action stream_action = ACTION_DROP_DATA; - headers = resp->headers; - if (headers.nvlen <= 0){ - goto finish; + headers = resp->header; + if (headers.nvlen <= 0 || ((headers.flag & NGHTTP2_FLAG_END_STREAM)!=1) ){ + return 0; } - if ((headers.flag & NGHTTP2_FLAG_END_STREAM) != 1 ){ - goto finish; - } - - stream_id = nghttp2_submit_headers(session_info->as_server, headers.flag, - h2_stream->stream_id, NULL, nghttp2_nv_packet(&headers, hdrs), - headers.nvlen, h2_stream); + + nghttp2_nv hdrs[headers.nvlen]; + stream_id = nghttp2_submit_headers(h2_stream_info->as_server, headers.flag, + h2_session->ngh2_stream_id, NULL, nghttp2_nv_packet(&headers, hdrs), + headers.nvlen, h2_session); if (stream_id < 0){ printf("Fatal headers error: %s\n", nghttp2_strerror(stream_id)); stream_action = ACTION_FORWARD_DATA; @@ -1156,27 +1015,26 @@ nghttp2_submit_end_header(struct tfe_session_info_t *session_info, delete_nv_packet_data(&headers); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_server); + xret = nghttp2_session_send(h2_stream_info->as_server); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); + TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s, %d\n",nghttp2_strerror(xret), __LINE__); } } -finish: - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; return 0; } static int -nghttp2_submit_end_data(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) +nghttp2_submit_end_stream_payload(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { int xret = -1; enum tfe_stream_action stream_action = ACTION_DROP_DATA; - struct http2_half_private *resp = h2_stream->resp; + struct tfe_h2_half_private *resp = h2_session->resp; - if (resp->body_state != MANAGE_STAGE_INIT){ + if (resp->body_state != H2_READ_STATE_BEGIN){ if (resp->event_cb) { resp->event_cb(resp, EV_HTTP_RESP_BODY_END, NULL, 0, resp->event_cb_user); @@ -1186,150 +1044,144 @@ nghttp2_submit_end_data(struct tfe_session_info_t *session_info, resp->event_cb_user); } } - struct data_t *body = &resp->body; - body->flags |= NGHTTP2_FLAG_END_STREAM; - resp->body_state = MANAGE_STAGE_COMPLETE; - resp->message_state = MANAGE_STAGE_COMPLETE; + struct tfe_h2_payload *payload = &resp->body; + payload->flags |= NGHTTP2_FLAG_END_STREAM; + resp->body_state = H2_READ_STATE_COMPLETE; + resp->message_state = H2_READ_STATE_COMPLETE; - stream_action = server_frame_submit_data(session_info, h2_stream, CONN_DIR_UPSTREAM); + stream_action = server_frame_submit_data(h2_stream_info, h2_session, CONN_DIR_UPSTREAM); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_server); + xret = nghttp2_session_send(h2_stream_info->as_server); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); + TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s %d\n",nghttp2_strerror(xret), __LINE__); } } - - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, 1, End of stream submit, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - h2_stream->stream_id, session_info->stream_action); - #endif if (stream_action == ACTION_USER_DATA) stream_action = ACTION_DROP_DATA; - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; return 1; } static int -nghttp2_submit_frame_data(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_data(struct tfe_h2_stream *h2_stream_info,const nghttp2_frame *frame, enum tfe_conn_dir dir) { - struct http2_half_private *resp = NULL; - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_half_private *resp = NULL; + struct tfe_h2_session *h2_session = NULL; if (dir == CONN_DIR_DOWNSTREAM){ goto finish; } if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM){ - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session_info->as_client, + h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->as_client, frame->hd.stream_id); - if (!h2_stream){ + if (!h2_session){ TFE_LOG_ERROR(logger()->handle, "Upstream id %d, can't find stream information(addr = %p)", - frame->hd.stream_id, session_info); + frame->hd.stream_id, h2_stream_info); goto finish; } - resp = h2_stream->resp; + resp = h2_session->resp; resp->body.padlen = frame->data.padlen; - if (resp->body_state != MANAGE_STAGE_COMPLETE){ - nghttp2_submit_end_data(session_info, h2_stream); + if (resp->body_state != H2_READ_STATE_COMPLETE){ + nghttp2_submit_end_stream_payload(h2_stream_info, h2_session); } } finish: return 0; } -static int tfe_half_session_init(struct h2_stream_data_t *h2_stream, int32_t stream_id, +static int tfe_half_session_init(struct tfe_h2_session *h2_session, int32_t stream_id, enum tfe_http_direction direction) { - struct tfe_http_session *tfe_session = &h2_stream->tfe_session; + struct tfe_http_session *tfe_session = &h2_session->tfe_session; if (direction == TFE_HTTP_REQUEST){ - struct http2_half_private *req = h2_stream->req; + struct tfe_h2_half_private *req = h2_session->req; tfe_session->ops = &nghttp2_session_ops; tfe_session->req = &req->half_public; tfe_session->session_id = stream_id; } if (direction == TFE_HTTP_RESPONSE){ - struct http2_half_private *resp = h2_stream->resp; + struct tfe_h2_half_private *resp = h2_session->resp; tfe_session->resp = &resp->half_public; } return 0; } static void -upstream_create_req(struct tfe_session_info_t *session_info, nghttp2_session *as_server, - struct h2_stream_data_t *h2_stream, int32_t stream_id) +upstream_create_req(struct tfe_h2_stream *h2_stream_info, nghttp2_session *as_server, + struct tfe_h2_session *h2_session, int32_t stream_id) { struct user_event_dispatch *event = NULL; - struct http2_half_private *half_private = NULL; + struct tfe_h2_half_private *half_private = NULL; - h2_stream->stream_id = stream_id; + h2_session->ngh2_stream_id = stream_id; - h2_stream->req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL); - tfe_half_session_init(h2_stream, stream_id, TFE_HTTP_REQUEST); + h2_session->req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL); + tfe_half_session_init(h2_session, stream_id, TFE_HTTP_REQUEST); event = ALLOC(struct user_event_dispatch, 1); assert(event); - event->thread_id = session_info->thread_id; - event->tf_stream = session_info->tf_stream; - event->tfe_session = &h2_stream->tfe_session; + event->thread_id = h2_stream_info->thread_id; + event->tf_stream = h2_stream_info->tf_stream; + event->tfe_session = &h2_session->tfe_session; - half_set_callback(h2_stream->req, event, NULL); + half_set_callback(h2_session->req, event, NULL); /* Call business plugin */ - half_private = h2_stream->req; + half_private = h2_session->req; half_private->frame_ctx = http_frame_alloc(); if (half_private->frame_ctx == NULL){ - TFE_STREAM_LOG_ERROR(h2_stream, "Failed at raising session begin event. "); + TFE_STREAM_LOG_ERROR(h2_session, "Failed at raising session begin event. "); goto finish; } - http_frame_raise_session_begin(half_private->frame_ctx, session_info->tf_stream, - &h2_stream->tfe_session, session_info->thread_id); - h2_stream->frame_ctx = half_private->frame_ctx; + http_frame_raise_session_begin(half_private->frame_ctx, h2_stream_info->tf_stream, + &h2_session->tfe_session, h2_stream_info->thread_id); + h2_session->frame_ctx = half_private->frame_ctx; - TAILQ_INSERT_TAIL(&session_info->list, h2_stream, next); - nghttp2_session_set_stream_user_data(as_server, stream_id, h2_stream); + TAILQ_INSERT_TAIL(&h2_stream_info->h2_session_list, h2_session, next); + nghttp2_session_set_stream_user_data(as_server, stream_id, h2_session); finish: return; } static enum tfe_stream_action -nghttp2_server_frame_submit_push_promise(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) +nghttp2_server_frame_submit_push_promise(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { int32_t stream_id = -1; - nghttp2_nv hdrs[128] = {0}; - struct http2_headers *headers = NULL; - struct h2_stream_data_t *_h2_stream = NULL; - struct http2_half_private *resp = NULL; + struct tfe_h2_header *headers = NULL; + struct tfe_h2_session *peer_h2_stream = NULL; + struct tfe_h2_half_private *resp = NULL; enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - resp = h2_stream->resp; + resp = h2_session->resp; if (resp == NULL) - goto finish; + return stream_action; headers = &resp->promised; if (headers->nvlen <= 0) - goto finish; + return stream_action; /* Create s' half req*/ - _h2_stream = (struct h2_stream_data_t *)ALLOC(struct h2_stream_data_t, 1); - assert(_h2_stream); - memset(_h2_stream, 0, sizeof(struct h2_stream_data_t)); - - stream_id = nghttp2_submit_push_promise(session_info->as_server, headers->flag, - h2_stream->stream_id, nghttp2_nv_packet(headers, hdrs), - headers->nvlen, _h2_stream); + peer_h2_stream = (struct tfe_h2_session *)ALLOC(struct tfe_h2_session, 1); + assert(peer_h2_stream); + + nghttp2_nv hdrs[headers->nvlen]; + stream_id = nghttp2_submit_push_promise(h2_stream_info->as_server, headers->flag, + h2_session->ngh2_stream_id, nghttp2_nv_packet(headers, hdrs), + headers->nvlen, peer_h2_stream); if (stream_id < 0){ - free(_h2_stream); - _h2_stream = NULL; - TFE_STREAM_LOG_ERROR(h2_stream, "Failed to submit push promise: %s", nghttp2_strerror(stream_id)); + free(peer_h2_stream); + peer_h2_stream = NULL; + TFE_STREAM_LOG_ERROR(h2_session, "Failed to submit push promise: %s", nghttp2_strerror(stream_id)); goto finish; } - upstream_create_req(session_info, session_info->as_server, _h2_stream, stream_id); + upstream_create_req(h2_stream_info, h2_stream_info->as_server, peer_h2_stream, stream_id); /*clean header message **/ delete_nv_packet_data(headers); stream_action = ACTION_DROP_DATA; @@ -1338,63 +1190,63 @@ finish: } static int -nghttp2_submit_frame_push_promise(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_push_promise(struct tfe_h2_stream *h2_stream_info,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1; - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_session *h2_session = NULL; enum tfe_stream_action stream_action = ACTION_DROP_DATA; if (dir == CONN_DIR_DOWNSTREAM) goto finish; - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session_info->as_client, + h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->as_client, frame->hd.stream_id); - if (!h2_stream){ + if (!h2_session){ TFE_LOG_ERROR(logger()->handle, "Upstream id %d, can't find stream information(addr = %p)", - frame->hd.stream_id, session_info); + frame->hd.stream_id, h2_stream_info); goto finish; } - stream_action = nghttp2_server_frame_submit_push_promise(session_info, h2_stream); + stream_action = nghttp2_server_frame_submit_push_promise(h2_stream_info, h2_session); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_server); + xret = nghttp2_session_send(h2_stream_info->as_server); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "dir(%d), Fatal send error: %s\n", dir, nghttp2_strerror(xret)); } } - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; finish: #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit push promise, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); + TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit push promise, stream_id:%d, action:%d", h2_stream_info->tf_stream->str_stream_info, + dir, frame->hd.stream_id, h2_stream_info->stream_action); #endif return 0; } #ifdef TFE_CACHE static int -suspend_start(struct h2_stream_data_t *h2_stream, - struct http2_half_private *half, const struct tfe_stream *stream) +suspend_start(struct tfe_h2_session *h2_session, + struct tfe_h2_half_private *half, const struct tfe_stream *stream) { - if (h2_stream->cache.spd_valid != 1){ + if (h2_session->cache.spd_valid != 1){ return 0; } tfe_stream_resume(stream); - enum tfe_http_event spd_event = h2_stream->cache.spd_event; + enum tfe_http_event spd_event = h2_session->cache.spd_event; - h2_stream->cache.spd_event = (enum tfe_http_event)0; - h2_stream->cache.spd_valid = 0; - h2_stream->cache.spd_set_cnt--; - h2_stream->cache.spd_cnt++; + h2_session->cache.spd_event = (enum tfe_http_event)0; + h2_session->cache.spd_valid = 0; + h2_session->cache.spd_set_cnt--; + h2_session->cache.spd_cnt++; /* Call user callback, tell user we resume from suspend */ - h2_stream->cache.rse_set = 0; + h2_session->cache.rse_set = 0; half->event_cb(half, spd_event, NULL, 0, half->event_cb_user); return 1; @@ -1402,65 +1254,41 @@ suspend_start(struct h2_stream_data_t *h2_stream, #endif static void -fill_resp_spec_from_handle(struct http2_half_private *half_private) +fill_resp_spec_from_handle(struct tfe_h2_half_private *half_private) { - struct header_data *head = NULL; + struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL; + struct tfe_h2_header *header = &half_private->header; struct tfe_http_resp_spec *resp_spec = &(half_private->half_public.resp_spec); - foreach_headers(&half_private->headers, head){ - if (!strncmp((char *)(head->nv.name), ":status", strlen(":status"))){ - resp_spec->resp_code = atoi((const char *)head->nv.value); + TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field){ + if (!strncmp((char *)(h2_field->nv.name), ":status", strlen(":status"))){ + resp_spec->resp_code = atoi((const char *)h2_field->nv.value); continue; } - if (!strncmp((char *)(head->nv.name), "content-type", strlen("content-type"))){ - //resp_spec->content_type = tfe_strdup((const char *)(head->nv.value)); - resp_spec->content_type = (const char *)(head->nv.value); + if (!strncmp((char *)(h2_field->nv.name), "content-type", strlen("content-type"))){ + resp_spec->content_type = (const char *)(h2_field->nv.value); continue; } - if (!strncmp((char *)(head->nv.name), "content-encoding", strlen("content-encoding"))){ - //resp_spec->content_encoding = tfe_strdup((const char *)(head->nv.value)); - resp_spec->content_encoding = (const char *)(head->nv.value); + if (!strncmp((char *)(h2_field->nv.name), "content-encoding", strlen("content-encoding"))){ + resp_spec->content_encoding = (const char *)(h2_field->nv.value); continue; } - if (!strncmp((char *)(head->nv.name), "content-length", strlen("content-length"))){ - //resp_spec->content_length = tfe_strdup((const char *)(head->nv.value)); - resp_spec->content_length = (const char *)(head->nv.value); + if (!strncmp((char *)(h2_field->nv.name), "content-length", strlen("content-length"))){ + resp_spec->content_length = (const char *)(h2_field->nv.value); continue; } } return; } -static void -tfe_make_nv(struct http2_headers *headers, - const char *name,const char *value, int flag) -{ - /*Add head*/ - struct header_data *head = NULL; - - head = ALLOC(struct header_data, 1); - head->nv.name = (uint8_t *)tfe_strdup((const char *)name); - head->nv.namelen = strlen(name); - - head->nv.value = (uint8_t *)tfe_strdup((const char *)value);; - head->nv.valuelen = strlen(value); - - headers->flag = 0x04; - if (flag) - headers_add_head(headers, head); - else - headers_add_tail(headers, head); - -} - int -nghttp2_headers_write_log(struct h2_stream_data_t *h2_stream, const char * str_stream_info, +nghttp2_headers_write_log(struct tfe_h2_session *h2_session, const char * str_stream_info, int dir) { /* Request */ - struct http2_half_private *req = h2_stream->req; + struct tfe_h2_half_private *req = h2_session->req; /* Response */ - struct http2_half_private *resp = h2_stream->resp; + struct tfe_h2_half_private *resp = h2_session->resp; /* Req-Public */ struct tfe_http_req_spec *req_spec = req ? &(req->half_public.req_spec) : NULL; /* Resp-Public */ @@ -1480,14 +1308,14 @@ nghttp2_headers_write_log(struct h2_stream_data_t *h2_stream, const char * str_s const char *hmsg = (dir == CONN_DIR_UPSTREAM) ? "response" : "request"; - const char * panggu_req = h2_stream->pangu_req ? "USER/REQ" : "-"; - const char * panggu_resp = h2_stream->pangu_resp ? "USER/RESP" : "-"; + const char * panggu_req = h2_session->plugin_built_req ? "USER/REQ" : "-"; + const char * panggu_resp = h2_session->plugin_built_resp ? "USER/RESP" : "-"; /* SUSPEND */ - const char * suspend = h2_stream->cache.spd_cnt > 0 ? "SUSPEND" : "-"; + const char * suspend = h2_session->cache.spd_cnt > 0 ? "SUSPEND" : "-"; char *access_log; - asprintf(&access_log, "%s %d %s stream_id:%d %s %s HTTP2.0 %s %s %s %s %s %s", str_stream_info, dir, hmsg, h2_stream->tfe_session.session_id, + asprintf(&access_log, "%s %d %s stream_id:%d %s %s HTTP2.0 %s %s %s %s %s %s", str_stream_info, dir, hmsg, h2_session->tfe_session.session_id, method, url, resp_code, cont_type, cont_encoding, panggu_req, panggu_resp, suspend); TFE_LOG_INFO(logger()->handle, "%s", access_log); @@ -1496,160 +1324,109 @@ nghttp2_headers_write_log(struct h2_stream_data_t *h2_stream, const char * str_s } static enum tfe_stream_action -cache_frame_submit_header(nghttp2_session *as_server, - int32_t stream_id) +nghttp2_server_frame_submit_header(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { - nghttp2_nv hdrs[128] = {0}; - struct http2_headers *headers = NULL; - struct http2_half_private *pangu_resp = NULL; + int32_t xret = 0; + struct tfe_h2_header *headers = NULL; + struct tfe_h2_half_private *resp = NULL; enum tfe_stream_action stream_action = ACTION_DROP_DATA; - struct h2_stream_data_t *h2_stream = NULL; -#define VALUE_LEN 128 - char value[VALUE_LEN] = {0}; - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(as_server, stream_id); - if (!h2_stream){ - stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Cache id %d, Flow is empty", stream_id); + if (h2_session->plugin_built_resp != NULL){ + stream_action = (enum tfe_stream_action)ACTION_USER_DATA; return stream_action; } - pangu_resp = h2_stream->pangu_resp; - if (pangu_resp == NULL){ - return ACTION_FORWARD_DATA; - } - headers = &pangu_resp->headers; - if (headers->nvlen <= 0){ - return ACTION_FORWARD_DATA; - } - - snprintf(value, VALUE_LEN, "%d", pangu_resp->method_or_status); - tfe_make_nv(&pangu_resp->headers, ":status", (const char *)value, 1); - - snprintf(value, VALUE_LEN, "tfe/%s", tfe_version()); - tfe_make_nv(&pangu_resp->headers, "X-TG-Construct-By", (const char *)value, 0); - - stream_id = nghttp2_submit_headers(as_server, headers->flag, - h2_stream->stream_id, NULL, nghttp2_nv_packet(headers, hdrs), - headers->nvlen, h2_stream); - if (stream_id < 0){ - printf("Fatal headers error: %s\n", nghttp2_strerror(stream_id)); - } - delete_nv_packet_data(headers); - - nghttp2_headers_write_log(h2_stream, h2_stream->tf_stream->str_stream_info, CONN_DIR_UPSTREAM); - - return stream_action; - -} - -static enum tfe_stream_action -nghttp2_server_frame_submit_header(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) -{ - int32_t stream_id = 0; - nghttp2_nv hdrs[128] = {0}; - struct http2_headers *headers = NULL; - struct http2_half_private *resp = NULL; - enum tfe_stream_action stream_action = ACTION_DROP_DATA; - - if (h2_stream->pangu_resp != NULL){ - stream_action = (enum tfe_stream_action)ACTION_USER_DATA; - goto finish; - } - resp = h2_stream->resp; + resp = h2_session->resp; if (resp == NULL){ return ACTION_FORWARD_DATA; } - headers = &resp->headers; + headers = &resp->header; if (headers->nvlen <= 0){ return ACTION_FORWARD_DATA; - } - stream_id = nghttp2_submit_headers(session_info->as_server, headers->flag, - h2_stream->stream_id, NULL, nghttp2_nv_packet(headers, hdrs), - headers->nvlen, h2_stream); - if (stream_id < 0){ - printf("Fatal headers error: %s\n", nghttp2_strerror(stream_id)); + } + nghttp2_nv hdrs[headers->nvlen]; + xret = nghttp2_submit_headers(h2_stream_info->as_server, headers->flag, + h2_session->ngh2_stream_id, NULL, nghttp2_nv_packet(headers, hdrs), + headers->nvlen, h2_session); + if (xret < 0){ + printf("Fatal headers error: %s\n", nghttp2_strerror(xret)); } -#ifdef tfe_cache - if (headers->flag &NGHTTP2_FLAG_END_STREAM) -#endif - delete_nv_packet_data(headers); -finish: + delete_nv_packet_data(headers); + return stream_action; } static int -nghttp2_server_submit_header(struct tfe_session_info_t *session_info, int32_t stream_id) +nghttp2_server_submit_header(struct tfe_h2_stream *h2_stream_info, int32_t stream_id) { int xret = -1; - struct http2_half_private *resp = NULL; - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_half_private *resp = NULL; + struct tfe_h2_session *h2_session = NULL; enum tfe_stream_action stream_action = ACTION_DROP_DATA; - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session_info->as_client, + h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->as_client, stream_id); - if (!h2_stream){ + if (!h2_session) + { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "Upstream id %d, can't find stream information(addr = %p)", - stream_id, session_info); + stream_id, h2_stream_info); goto finish; } - resp = h2_stream->resp; - fill_resp_spec_from_handle(h2_stream->resp); + resp = h2_session->resp; + fill_resp_spec_from_handle(h2_session->resp); resp->event_cb(resp, EV_HTTP_RESP_HDR, NULL, 0, resp->event_cb_user); - if (h2_stream->cache.spd_set){ - h2_stream->cache.spd_event = EV_HTTP_RESP_HDR; - h2_stream->cache.spd_valid = 1; - h2_stream->cache.spd_set = 0; - h2_stream->cache.spd_set_cnt++; - h2_stream->cache.spd_cnt++; - tfe_stream_suspend(session_info->tf_stream, CONN_DIR_UPSTREAM); + if (h2_session->cache.spd_set){ + h2_session->cache.spd_event = EV_HTTP_RESP_HDR; + h2_session->cache.spd_valid = 1; + h2_session->cache.spd_set = 0; + h2_session->cache.spd_set_cnt++; + h2_session->cache.spd_cnt++; + tfe_stream_suspend(h2_stream_info->tf_stream, CONN_DIR_UPSTREAM); - session_info->stream_id = stream_id; stream_action = ACTION_DEFER_DATA; goto finish; } - nghttp2_headers_write_log(h2_stream,session_info->tf_stream->str_stream_info, CONN_DIR_UPSTREAM); + nghttp2_headers_write_log(h2_session,h2_stream_info->tf_stream->str_stream_info, CONN_DIR_UPSTREAM); - stream_action = nghttp2_server_frame_submit_header(session_info, h2_stream); + stream_action = nghttp2_server_frame_submit_header(h2_stream_info, h2_session); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_server); + xret = nghttp2_session_send(h2_stream_info->as_server); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); + TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n", nghttp2_strerror(xret)); } } if (stream_action == ACTION_USER_DATA) stream_action = ACTION_DROP_DATA; finish: - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; return 0; } static void -fill_req_spec_from_handle(struct http2_half_private *half_private) +fill_req_spec_from_handle(struct tfe_h2_half_private *half_private) { int urllen = 0; - struct header_data *head = NULL; + struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL; + struct tfe_h2_header *header = &half_private->header; struct tfe_http_req_spec *req_spec = &(half_private->half_public.req_spec); - foreach_headers(&half_private->headers, head){ - if (!strncmp((char *)(head->nv.name), ":method", strlen(":method"))){ - req_spec->method = (enum tfe_http_std_method)str_to_val((const char *)(head->nv.value), method_vals); + TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field){ + if (!strncmp((char *)(h2_field->nv.name), ":method", strlen(":method"))){ + req_spec->method = (enum tfe_http_std_method)str_to_val((const char *)(h2_field->nv.value), method_vals); continue; } - if (!strncmp((char *)(head->nv.name), ":authority", strlen(":authority"))){ - //req_spec->host = tfe_strdup((const char *)(head->nv.value)); - req_spec->host = (const char *)(head->nv.value); - urllen += head->nv.valuelen; + if (!strncmp((char *)(h2_field->nv.name), ":authority", strlen(":authority"))){ + req_spec->host = (const char *)(h2_field->nv.value); + urllen += h2_field->nv.valuelen; continue; } - if (!strncmp((char *)(head->nv.name), ":path", strlen(":path"))){ - //req_spec->uri = tfe_strdup((const char*)(head->nv.value)); - req_spec->uri = (const char*)(head->nv.value); - urllen += head->nv.valuelen; + if (!strncmp((char *)(h2_field->nv.name), ":path", strlen(":path"))){ + req_spec->uri = (const char*)(h2_field->nv.value); + urllen += h2_field->nv.valuelen; continue; } } @@ -1665,14 +1442,14 @@ fill_req_spec_from_handle(struct http2_half_private *half_private) #ifdef TFE_CACHE static int -suspend_stop(struct h2_stream_data_t *h2_stream, +suspend_stop(struct tfe_h2_session *h2_session, const struct tfe_stream *tf_stream, enum tfe_conn_dir dir) { int xret = -1; - if (h2_stream->cache.spd_set){ - h2_stream->cache.spd_valid = 1; - h2_stream->cache.spd_set = 0; + if (h2_session->cache.spd_set){ + h2_session->cache.spd_valid = 1; + h2_session->cache.spd_set = 0; tfe_stream_suspend(tf_stream, dir); xret = 0; } @@ -1681,24 +1458,28 @@ suspend_stop(struct h2_stream_data_t *h2_stream, #endif static enum tfe_stream_action -tfe_submit_response(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) +nghttp2_submit_built_response(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { - int xret = -1; + int xret = -1; + char value[128] = {0}; enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - struct http2_half_private *resp = h2_stream->pangu_resp; -#define VALUE_LEN 128 - char value[VALUE_LEN] = {0}; + struct tfe_h2_half_private *resp = h2_session->plugin_built_resp; - snprintf(value, VALUE_LEN, "%d", resp->method_or_status); - tfe_make_nv(&resp->headers, ":status", (const char *)value, 1); + struct http_field_name field; + field.field_id = TFE_HTTP_UNKNOWN_FIELD; + field.field_name = "X-TG-Construct-By"; + snprintf(value, sizeof(value), "tfe/%s", tfe_version()); + tfe_h2_header_add_field(&resp->header, &field, (const char *)value, 0); - snprintf(value, VALUE_LEN, "tfe/%s", tfe_version()); - tfe_make_nv(&resp->headers, "X-TG-Construct-By", (const char *)value, 0); + field.field_id = TFE_HTTP_UNKNOWN_FIELD; + field.field_name = ":status"; + snprintf(value, sizeof(value), "%d", resp->method_or_status); + tfe_h2_header_add_field(&resp->header, &field, (const char *)value, 0); - stream_action = nghttp2_server_frame_submit_response(session_info, h2_stream); + stream_action = nghttp2_server_frame_submit_response(h2_stream_info, h2_session); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_server); + xret = nghttp2_session_send(h2_stream_info->as_server); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "Fatal downstream send error: %s\n", @@ -1712,124 +1493,107 @@ tfe_submit_response(struct tfe_session_info_t *session_info, } static void -downstream_create_resp(struct h2_stream_data_t *h2_stream, nghttp2_session *as_client, +downstream_create_resp(struct tfe_h2_session *h2_session, nghttp2_session *as_client, nghttp2_session *as_server, const struct tfe_stream *tf_stream, unsigned int thread_id) { struct user_event_dispatch *event = NULL; - if (h2_stream->resp) + if (h2_session->resp) goto finish; - h2_stream->resp = tfe_half_private_init(TFE_HTTP_RESPONSE, h2_stream->stream_id, as_server); - tfe_half_session_init(h2_stream, h2_stream->stream_id, TFE_HTTP_RESPONSE); + h2_session->resp = tfe_half_private_init(TFE_HTTP_RESPONSE, h2_session->ngh2_stream_id, as_server); + tfe_half_session_init(h2_session, h2_session->ngh2_stream_id, TFE_HTTP_RESPONSE); event = ALLOC(struct user_event_dispatch, 1); assert(event); event->thread_id = thread_id; event->tf_stream = tf_stream; - event->tfe_session = &h2_stream->tfe_session; + event->tfe_session = &h2_session->tfe_session; - half_set_callback(h2_stream->resp, event, NULL); + half_set_callback(h2_session->resp, event, NULL); - h2_stream->resp->frame_ctx = h2_stream->frame_ctx; + h2_session->resp->frame_ctx = h2_session->frame_ctx; - nghttp2_session_set_stream_user_data(as_client, h2_stream->stream_id, h2_stream); + nghttp2_session_set_stream_user_data(as_client, h2_session->ngh2_stream_id, h2_session); finish: return; } static enum tfe_stream_action -nghttp2_client_frame_submit_header(struct tfe_session_info_t *session_info, - struct h2_stream_data_t *h2_stream) +nghttp2_client_frame_submit_header(struct tfe_h2_stream *h2_stream_info, + struct tfe_h2_session *h2_session) { int32_t stream_id = -1; - nghttp2_nv hdrs[128] = {0}; - struct http2_headers *headers = NULL; - struct http2_half_private *req = NULL; + struct tfe_h2_header *headers = NULL; + struct tfe_h2_half_private *req = NULL; enum tfe_http_std_method method = (enum tfe_http_std_method)NGHTTP2_METHOD_UNKNOWN; enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - req = h2_stream->pangu_req != NULL ? h2_stream->pangu_req : h2_stream->req; + req = h2_session->plugin_built_req != NULL ? h2_session->plugin_built_req : h2_session->req; if (req == NULL){ - stream_action = ACTION_FORWARD_DATA; - goto finish; + return ACTION_FORWARD_DATA; } - if (h2_stream->pangu_resp){ - stream_action = tfe_submit_response(session_info, h2_stream); - goto finish; + if (h2_session->plugin_built_resp){ + stream_action = nghttp2_submit_built_response(h2_stream_info, h2_session); + return stream_action; } - headers = &req->headers; + headers = &req->header; if (headers->nvlen <= 0){ - stream_action = ACTION_FORWARD_DATA; - goto finish; + return ACTION_FORWARD_DATA; } /*Create C' half_private_resp**/ - downstream_create_resp(h2_stream, session_info->as_client, session_info->as_server, - session_info->tf_stream, session_info->thread_id); - nghttp2_session_set_next_stream_id(session_info->as_client, h2_stream->stream_id); - - method = nghttp2_get_method(h2_stream->req); + downstream_create_resp(h2_session, h2_stream_info->as_client, h2_stream_info->as_server, + h2_stream_info->tf_stream, h2_stream_info->thread_id); + nghttp2_session_set_next_stream_id(h2_stream_info->as_client, h2_session->ngh2_stream_id); + + nghttp2_nv hdrs[headers->nvlen]; + method = nghttp2_get_method(h2_session->req); if (method == (enum tfe_http_std_method)NGHTTP2_METHOD_POST || method == (enum tfe_http_std_method)NGHTTP2_METHOD_PUT){ - stream_id = nghttp2_submit_headers(session_info->as_client, headers->flag, - -1, NULL, nghttp2_nv_packet(headers, hdrs), - headers->nvlen, h2_stream); + stream_id = nghttp2_submit_headers(h2_stream_info->as_client, headers->flag, + -1, NULL, nghttp2_nv_packet(headers, hdrs), + headers->nvlen, h2_session); }else{ - stream_id = nghttp2_submit_request(session_info->as_client, NULL, - nghttp2_nv_packet(headers, hdrs), - headers->nvlen, NULL, h2_stream); + stream_id = nghttp2_submit_request(h2_stream_info->as_client, NULL, + nghttp2_nv_packet(headers, hdrs), + headers->nvlen, NULL, h2_session); } - if (stream_id < 0){ TFE_LOG_ERROR(logger()->handle, "Could not submit request: %s", nghttp2_strerror(stream_id)); stream_action = ACTION_FORWARD_DATA; goto finish; } - - /*clear headers data ***/ - delete_nv_packet_data(headers); - stream_action = ACTION_DROP_DATA; -finish: +finish: + delete_nv_packet_data(headers); return stream_action; } static int -nghttp2_client_submit_header(struct tfe_session_info_t *session_info, int32_t stream_id) +nghttp2_client_submit_header(struct tfe_h2_stream *h2_stream_info, int32_t stream_id) { int xret = -1; - struct http2_half_private *req = NULL; - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_half_private *req = NULL; + struct tfe_h2_session *h2_session = NULL; enum tfe_stream_action stream_action = ACTION_DROP_DATA; - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session_info->as_server, + h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(h2_stream_info->as_server, stream_id); - if (!h2_stream){ + if (!h2_session){ stream_action = ACTION_FORWARD_DATA; goto finish; } - req = h2_stream->req; - fill_req_spec_from_handle(h2_stream->req); + req = h2_session->req; + fill_req_spec_from_handle(h2_session->req); req->event_cb(req, EV_HTTP_REQ_HDR, NULL, 0, req->event_cb_user); - if (h2_stream->cache.spd_set){ - h2_stream->cache.spd_event = EV_HTTP_REQ_HDR; - h2_stream->cache.spd_valid = 1; - h2_stream->cache.spd_set = 0; - h2_stream->cache.spd_set_cnt++; - h2_stream->cache.spd_cnt++; - tfe_stream_suspend(session_info->tf_stream, CONN_DIR_DOWNSTREAM); - session_info->stream_id = stream_id; - stream_action = ACTION_DEFER_DATA; - goto finish; - } - nghttp2_headers_write_log(h2_stream, session_info->tf_stream->str_stream_info, CONN_DIR_DOWNSTREAM); - stream_action = nghttp2_client_frame_submit_header(session_info, h2_stream); + nghttp2_headers_write_log(h2_session, h2_stream_info->tf_stream->str_stream_info, CONN_DIR_DOWNSTREAM); + stream_action = nghttp2_client_frame_submit_header(h2_stream_info, h2_session); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_client); + xret = nghttp2_session_send(h2_stream_info->as_client); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "Fatal downstream send error: %s\n", @@ -1840,30 +1604,25 @@ nghttp2_client_submit_header(struct tfe_session_info_t *session_info, int32_t st stream_action = ACTION_DROP_DATA; finish: - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; return 0; } static int -nghttp2_submit_frame_header(struct tfe_session_info_t *session_info,const nghttp2_frame *frame, +nghttp2_submit_frame_header(struct tfe_h2_stream *h2_stream_info,const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = 0; - if (frame->hd.flags & NGHTTP2_FLAG_END_HEADERS){ - if (dir == CONN_DIR_UPSTREAM){ - xret = nghttp2_server_submit_header(session_info, frame->hd.stream_id); - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit response header, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); - #endif + if (frame->hd.flags & NGHTTP2_FLAG_END_HEADERS) + { + if (dir == CONN_DIR_UPSTREAM) + { + xret = nghttp2_server_submit_header(h2_stream_info, frame->hd.stream_id); } - if (dir == CONN_DIR_DOWNSTREAM){ - xret = nghttp2_client_submit_header(session_info, frame->hd.stream_id); - #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, %d, submit request header, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); - #endif + if (dir == CONN_DIR_DOWNSTREAM) + { + xret = nghttp2_client_submit_header(h2_stream_info, frame->hd.stream_id); } } return xret; @@ -1878,110 +1637,91 @@ nghttp2_frame_callback nghttp2_frame_callback_array[] = { [NGHTTP2_PUSH_PROMISE] = nghttp2_submit_frame_push_promise, [NGHTTP2_PING] = nghttp2_submit_frame_ping, [NGHTTP2_GOAWAY] = nghttp2_submit_frame_goaway, - [NGHTTP2_WINDOW_UPDATE] = nghttp2_submit_frame_window_update, + [NGHTTP2_WINDOW_UPDATE] = nghttp2_submit_frame_window_update, [NGHTTP2_CONTINUATION] = NULL, [NGHTTP2_ALTSVC] = NULL, }; static int -nghttp2_fill_up_header(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name, +nghttp2_fill_up_header(nghttp2_session *ngh2_session, const nghttp2_frame *frame, const uint8_t *name, size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir) { - enum tfe_http_std_field field_id = TFE_HTTP_UNKNOWN_FIELD; - struct header_data *head = NULL; - struct http2_headers *headers = NULL; + struct tfe_h2_header *h2_header = NULL; if (dir == CONN_DIR_DOWNSTREAM && frame->headers.cat != NGHTTP2_HCAT_REQUEST){ return 0; } - struct h2_stream_data_t *h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); - if (!h2_stream){ + struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(ngh2_session, frame->hd.stream_id); + if (!h2_session){ TFE_LOG_ERROR(logger()->handle, "Header stream id %d, can't find stream information", frame->hd.stream_id); return 0; } - struct http2_half_private *half = (dir == CONN_DIR_UPSTREAM) ? h2_stream->resp : h2_stream->req; - - head = ALLOC(struct header_data, 1); - head->nv.name = (uint8_t *)tfe_strdup((const char *)name); - head->nv.namelen = namelen; - head->nv.value = (uint8_t *)tfe_strdup((const char *)value);; - head->nv.valuelen = valuelen; - head->nv.flags = flags; - field_id = (enum tfe_http_std_field)str_to_val((const char *)name, headers_vals); - - if (field_id == -1){ - head->field.field_id = TFE_HTTP_UNKNOWN_FIELD; - head->field.field_name = (const char *)head->nv.name; - }else{ - if (field_id == TFE_HTTP_CONT_ENCODING){ - half->body.gzip = method_to_str_idx((const char *)value); - } - head->field.field_id = field_id; - head->field.field_name = NULL; + struct tfe_h2_half_private *half = (dir == CONN_DIR_UPSTREAM) ? h2_session->resp : h2_session->req; + + struct http_field_name field; + field.field_id = (enum tfe_http_std_field)str_to_val((const char *)name, header_vals); + if (field.field_id == TFE_HTTP_UNKNOWN_FIELD) + { + field.field_name = (const char *)name; } - headers = &half->headers; - headers->flag = frame->hd.flags; - headers_add_tail(headers, head); + if (field.field_id == TFE_HTTP_CONT_ENCODING) + { + half->body.gzip = method_to_str_idx((const char *)value); + } + h2_header = &half->header; + tfe_h2_header_add_field(h2_header, &field, (const char *)value, 1); + h2_header->flag = frame->hd.flags; return 0; } static int -nghttp2_fill_up_promise(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *name, +nghttp2_fill_up_promise(nghttp2_session *ngh2_session, const nghttp2_frame *frame, const uint8_t *name, size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir) { - struct header_data *head = NULL; - struct http2_headers *headers = NULL; - struct http2_half_private *resp = NULL; - enum tfe_http_std_field field_id = TFE_HTTP_UNKNOWN_FIELD; + struct tfe_h2_header *headers = NULL; + struct tfe_h2_half_private *resp = NULL; if (dir == CONN_DIR_DOWNSTREAM) return 0; - struct h2_stream_data_t *h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); - if (!h2_stream){ + struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(ngh2_session, frame->hd.stream_id); + if (!h2_session){ TFE_LOG_ERROR(logger()->handle, "Promise stream id %d, can't find stream information", frame->hd.stream_id); return 0; } - resp = h2_stream->resp; - head = ALLOC(struct header_data, 1); - head->nv.name = (uint8_t *)tfe_strdup((const char *)name); - head->nv.namelen = namelen; - head->nv.value = (uint8_t *)tfe_strdup((const char *)value); - head->nv.valuelen = valuelen; - head->nv.flags = flags; - field_id = (enum tfe_http_std_field)str_to_val((const char *)name, headers_vals); - if (field_id == -1){ - head->field.field_id = TFE_HTTP_UNKNOWN_FIELD; - head->field.field_name = (const char *)head->nv.name; - }else{ - if (field_id == TFE_HTTP_CONT_ENCODING){ - resp->body.gzip = method_to_str_idx((const char *)value); - } - head->field.field_id = field_id; - head->field.field_name = NULL; + resp = h2_session->resp; + struct http_field_name field; + field.field_id = (enum tfe_http_std_field)str_to_val((const char *)name, header_vals); + if (field.field_id == TFE_HTTP_UNKNOWN_FIELD) + { + field.field_name = (const char *)name; } + if (field.field_id == TFE_HTTP_CONT_ENCODING) + { + resp->body.gzip = method_to_str_idx((const char *)value); + } headers = &resp->promised; + tfe_h2_header_add_field(headers, &field, (const char *)value, 1); headers->flag = frame->hd.flags; - headers_add_tail(headers, head); return 0; } static int -nghttp2_data_send(nghttp2_session *session, const nghttp2_frame *frame, const uint8_t *data, +nghttp2_data_send(nghttp2_session *ngh2_session, const nghttp2_frame *frame, const uint8_t *data, size_t length, const uint8_t *value, size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir) { int ret = -1; - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; - ret = tfe_stream_write(session_info->tf_stream, dir, data, length); + ret = tfe_stream_write(h2_stream_info->tf_stream, dir, data, length); if (unlikely(ret < 0)){ assert(0); } @@ -1993,16 +1733,16 @@ nghttp2_on_stream_close(nghttp2_session *session, const nghttp2_frame *frame, co size_t namelen, const uint8_t *value, size_t valuelen, uint8_t flags, void *user_data, enum tfe_conn_dir dir) { - struct h2_stream_data_t *h2_stream = NULL; - struct http2_half_private *resp = NULL; + struct tfe_h2_session *h2_session = NULL; + struct tfe_h2_half_private *resp = NULL; int32_t stream_id = frame->hd.stream_id; uint32_t error_code = frame->goaway.error_code; - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; - h2_stream = TAILQ_LIST_FIND(session_info, stream_id); - if (!h2_stream) + h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id); + if (!h2_session) return 0; if (error_code != 0){ const char *str = (dir == CONN_DIR_UPSTREAM) ? "Upstream" : "Downstream"; @@ -2012,18 +1752,18 @@ nghttp2_on_stream_close(nghttp2_session *session, const nghttp2_frame *frame, co if (dir == CONN_DIR_DOWNSTREAM) goto finish; - resp = h2_stream->resp; - if (error_code == 0 && resp->body_state != MANAGE_STAGE_COMPLETE){ - if (resp->body_state == MANAGE_STAGE_INIT && - session_info->stream_action != ACTION_DEFER_DATA) - nghttp2_submit_end_header(session_info, h2_stream); + resp = h2_session->resp; + if (error_code == 0 && resp->body_state != H2_READ_STATE_COMPLETE){ + if (resp->body_state == H2_READ_STATE_BEGIN && + h2_stream_info->stream_action != ACTION_DEFER_DATA) + nghttp2_submit_end_header(h2_stream_info, h2_session); goto end; } finish: - TAILQ_REMOVE(&session_info->list, h2_stream, next); - delete_http2_stream_data(h2_stream, session_info->tf_stream, 1); - free(h2_stream); - h2_stream = NULL; + TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next); + delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1); + free(h2_session); + h2_session = NULL; end: return 0; } @@ -2037,7 +1777,7 @@ nghttp2_callback nghttp2_callback_array[] = { [NGHTTP2_PUSH_PROMISE] = nghttp2_fill_up_promise, [NGHTTP2_PING] = NULL, [NGHTTP2_GOAWAY] = NULL, - [NGHTTP2_WINDOW_UPDATE] = NULL, + [NGHTTP2_WINDOW_UPDATE] = NULL, [NGHTTP2_CONTINUATION] = NULL, [NGHTTP2_ALTSVC] = NULL, [NGHTTP2_USER_SEND] = nghttp2_data_send, @@ -2059,10 +1799,10 @@ static int nghttp2_client_on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) { - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; if ( nghttp2_frame_callback_array[frame->hd.type] != NULL){ - return nghttp2_frame_callback_array[frame->hd.type](session_info, frame, CONN_DIR_UPSTREAM); + return nghttp2_frame_callback_array[frame->hd.type](h2_stream_info, frame, CONN_DIR_UPSTREAM); } return 0; } @@ -2078,22 +1818,22 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, enum tfe_stream_action stream_action = ACTION_DROP_DATA; int uncompr_len = 0, __attribute__((__unused__))ret = 0; const unsigned char *data; - struct http2_half_private * resp = NULL; + struct tfe_h2_half_private * resp = NULL; - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; - struct h2_stream_data_t *h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session, stream_id); - if (!h2_stream){ + struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(session, stream_id); + if (!h2_session){ TFE_LOG_ERROR(logger()->handle, "On data callback can't get downstream information, id = %d", stream_id); goto finish; } - resp = h2_stream->resp; + resp = h2_session->resp; evbuffer_add(resp->body.evbuf_body, input, input_len); if (resp->body.gzip != HTTP2_CONTENT_ENCODING_NONE){ ret = inflate_read(input, input_len, &uncompr, &uncompr_len, - &resp->body.inflater, resp->body.gzip); + &resp->body.inflate, resp->body.gzip); if (((ret == Z_STREAM_END) || (ret == Z_OK)) && uncompr > 0){ input = (const uint8_t*)uncompr; input_len = uncompr_len; @@ -2102,7 +1842,7 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, data = input; len = input_len; - if (resp->body_state == MANAGE_STAGE_INIT){ + if (resp->body_state == H2_READ_STATE_BEGIN){ if (resp->event_cb) { resp->event_cb(resp, EV_HTTP_RESP_BODY_BEGIN, NULL, len, resp->event_cb_user); @@ -2112,9 +1852,9 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, }else{ resp->body.flags = flags; } - resp->body_state = MANAGE_STAGE_READING; + resp->body_state = H2_READ_STATE_READING; } - if (resp->body_state == MANAGE_STAGE_READING){ + if (resp->body_state == H2_READ_STATE_READING){ if (resp->event_cb) { resp->event_cb(resp, EV_HTTP_RESP_BODY_CONT, data, len, resp->event_cb_user); @@ -2126,9 +1866,9 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, } } - stream_action = server_frame_submit_data(session_info, h2_stream, CONN_DIR_UPSTREAM); + stream_action = server_frame_submit_data(h2_stream_info, h2_session, CONN_DIR_UPSTREAM); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_server); + xret = nghttp2_session_send(h2_stream_info->as_server); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; TFE_LOG_ERROR(logger()->handle, "Fatal upstream(%d) send error: %s\n",stream_id, nghttp2_strerror(xret)); @@ -2136,12 +1876,12 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, } #ifdef TFE_LOG_HTTP2 - TFE_LOG_DEBUG(logger()->handle, "%s, 1, submit data %d, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, + TFE_LOG_DEBUG(logger()->handle, "%s, 1, submit data %d, stream_id:%d, action:%d", h2_stream_info->tf_stream->str_stream_info, (int)input_len, stream_id, stream_action); #endif if (stream_action == ACTION_USER_DATA) stream_action = ACTION_DROP_DATA; - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; finish: return 0; } @@ -2176,53 +1916,53 @@ nghttp2_client_on_header(nghttp2_session *session, return 0; } -static struct h2_stream_data_t* +static struct tfe_h2_session* create_upstream_data(nghttp2_session *session, int32_t stream_id, - struct tfe_session_info_t *session_info) + struct tfe_h2_stream *h2_stream_info) { - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_session *h2_session = NULL; struct user_event_dispatch *event = NULL; - h2_stream = TAILQ_LIST_FIND(session_info, stream_id); - if (h2_stream == NULL){ + h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id); + if (h2_session == NULL){ /** todo:When the data of the reply is pushed as promised, there is no stream id at the reply end. to create it*/ goto finish; } - if (h2_stream->resp){ + if (h2_session->resp){ goto finish; } - h2_stream->resp = tfe_half_private_init(TFE_HTTP_RESPONSE, stream_id, session_info->as_server); - tfe_half_session_init(h2_stream, stream_id, TFE_HTTP_RESPONSE); + h2_session->resp = tfe_half_private_init(TFE_HTTP_RESPONSE, stream_id, h2_stream_info->as_server); + tfe_half_session_init(h2_session, stream_id, TFE_HTTP_RESPONSE); event = ALLOC(struct user_event_dispatch, 1); assert(event); - event->thread_id = session_info->thread_id; - event->tf_stream = session_info->tf_stream; - event->tfe_session = &h2_stream->tfe_session; + event->thread_id = h2_stream_info->thread_id; + event->tf_stream = h2_stream_info->tf_stream; + event->tfe_session = &h2_session->tfe_session; - half_set_callback(h2_stream->resp, event, NULL); + half_set_callback(h2_session->resp, event, NULL); - h2_stream->resp->frame_ctx = h2_stream->frame_ctx; + h2_session->resp->frame_ctx = h2_session->frame_ctx; - nghttp2_session_set_stream_user_data(session, stream_id, h2_stream); + nghttp2_session_set_stream_user_data(session, stream_id, h2_session); finish: - return h2_stream; + return h2_session; } static ssize_t nghttp2_client_select_padding_callback(nghttp2_session *session, const nghttp2_frame *frame, size_t max_payloadlen, void *user_data) { - struct http2_half_private *resp = NULL; - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_half_private *resp = NULL; + struct tfe_h2_session *h2_session = NULL; - h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); - if (!h2_stream) + h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(session, frame->hd.stream_id); + if (!h2_session) return frame->hd.length; - resp = h2_stream->resp; + resp = h2_session->resp; if (!resp) return frame->hd.length; @@ -2236,10 +1976,10 @@ nghttp2_client_on_begin_headers(nghttp2_session * session, { (void)session; - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; switch(frame->hd.type){ case NGHTTP2_HEADERS: - create_upstream_data(session, frame->hd.stream_id, session_info); + create_upstream_data(session, frame->hd.stream_id, h2_stream_info); break; default: break; @@ -2248,7 +1988,7 @@ nghttp2_client_on_begin_headers(nghttp2_session * session, } static -void client_session_init(struct tfe_session_info_t *session_info) +void client_session_init(struct tfe_h2_stream *h2_stream_info) { nghttp2_session_callbacks *callbacks; @@ -2275,7 +2015,7 @@ void client_session_init(struct tfe_session_info_t *session_info) nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, nghttp2_client_on_begin_headers); - nghttp2_session_client_new(&session_info->as_client, callbacks, session_info); + nghttp2_session_client_new(&h2_stream_info->as_client, callbacks, h2_stream_info); nghttp2_session_callbacks_del(callbacks); } @@ -2295,10 +2035,10 @@ static int nghttp2_server_on_frame_recv(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) { - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; if ( nghttp2_frame_callback_array[frame->hd.type] != NULL){ - return nghttp2_frame_callback_array[frame->hd.type](session_info, frame, CONN_DIR_DOWNSTREAM); + return nghttp2_frame_callback_array[frame->hd.type](h2_stream_info, frame, CONN_DIR_DOWNSTREAM); } return 0; @@ -2334,49 +2074,48 @@ nghttp2_server_on_header(nghttp2_session *session, const nghttp2_frame *frame, c static void create_serv_stream_data(nghttp2_session *session, int32_t stream_id, - struct tfe_session_info_t *session_info) + struct tfe_h2_stream *h2_stream_info) { - struct h2_stream_data_t *h2_stream = NULL; + struct tfe_h2_session *h2_session = NULL; struct user_event_dispatch *event = NULL; - struct http2_half_private *half_private = NULL; + struct tfe_h2_half_private *half_private = NULL; - h2_stream = TAILQ_LIST_FIND(session_info, stream_id); - if (h2_stream != NULL){ - nghttp2_session_set_stream_user_data(session, stream_id, h2_stream); + h2_session = TAILQ_LIST_FIND(h2_stream_info, stream_id); + if (h2_session != NULL){ + nghttp2_session_set_stream_user_data(session, stream_id, h2_session); goto finish; } - h2_stream = (struct h2_stream_data_t *)ALLOC(struct h2_stream_data_t, 1); - assert(h2_stream); - memset(h2_stream, 0, sizeof(struct h2_stream_data_t)); - h2_stream->stream_id = stream_id; - h2_stream->session = session_info->as_server; - h2_stream->tf_stream = session_info->tf_stream; + h2_session = (struct tfe_h2_session *)ALLOC(struct tfe_h2_session, 1); + assert(h2_session); + h2_session->ngh2_stream_id = stream_id; + h2_session->session = h2_stream_info->as_server; + h2_session->tf_stream = h2_stream_info->tf_stream; - h2_stream->req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL); - tfe_half_session_init(h2_stream, stream_id, TFE_HTTP_REQUEST); + h2_session->req = tfe_half_private_init(TFE_HTTP_REQUEST, 0, NULL); + tfe_half_session_init(h2_session, stream_id, TFE_HTTP_REQUEST); event = ALLOC(struct user_event_dispatch, 1); assert(event); - event->thread_id = session_info->thread_id; - event->tf_stream = session_info->tf_stream; - event->tfe_session = &h2_stream->tfe_session; + event->thread_id = h2_stream_info->thread_id; + event->tf_stream = h2_stream_info->tf_stream; + event->tfe_session = &h2_session->tfe_session; - half_set_callback(h2_stream->req, event, NULL); + half_set_callback(h2_session->req, event, NULL); /* Call business plugin */ - half_private = h2_stream->req; + half_private = h2_session->req; half_private->frame_ctx = http_frame_alloc(); if (half_private->frame_ctx == NULL){ TFE_LOG_ERROR(logger()->handle, "Failed at raising session begin event. "); goto finish; } - http_frame_raise_session_begin(half_private->frame_ctx, session_info->tf_stream, - &h2_stream->tfe_session, session_info->thread_id); - h2_stream->frame_ctx = half_private->frame_ctx; - TAILQ_INSERT_TAIL(&session_info->list, h2_stream, next); - nghttp2_session_set_stream_user_data(session, stream_id, h2_stream); + http_frame_raise_session_begin(half_private->frame_ctx, h2_stream_info->tf_stream, + &h2_session->tfe_session, h2_stream_info->thread_id); + h2_session->frame_ctx = half_private->frame_ctx; + TAILQ_INSERT_TAIL(&h2_stream_info->h2_session_list, h2_session, next); + nghttp2_session_set_stream_user_data(session, stream_id, h2_session); finish: return; } @@ -2392,23 +2131,23 @@ nghttp2_server_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, enum tfe_stream_action stream_action = ACTION_DROP_DATA; int uncompr_len = 0, __attribute__((__unused__))ret = 0; const unsigned char __attribute__((__unused__))*data; - struct http2_half_private * req = NULL; + struct tfe_h2_half_private * req = NULL; - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; - struct h2_stream_data_t *h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(session, stream_id); - if (!h2_stream){ + struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(session, stream_id); + if (!h2_session){ TFE_LOG_ERROR(logger()->handle, "On data callback can't get downstream information, id = %d", stream_id); goto finish; } - req = h2_stream->req; + req = h2_session->req; req->body.flags = flags; evbuffer_add(req->body.evbuf_body, input, input_len); if (req->body.gzip != HTTP2_CONTENT_ENCODING_NONE){ ret = inflate_read(input, input_len, &uncompr, &uncompr_len, - &req->body.inflater, req->body.gzip); + &req->body.inflate, req->body.gzip); if (((ret == Z_STREAM_END) || (ret == Z_OK)) && uncompr > 0){ input = (const uint8_t*)uncompr; input_len = uncompr_len; @@ -2417,22 +2156,22 @@ nghttp2_server_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, data = input; len = input_len; - stream_action = server_frame_submit_data(session_info, h2_stream, CONN_DIR_DOWNSTREAM); + stream_action = server_frame_submit_data(h2_stream_info, h2_session, CONN_DIR_DOWNSTREAM); if (stream_action == ACTION_DROP_DATA){ - xret = nghttp2_session_send(session_info->as_client); + xret = nghttp2_session_send(h2_stream_info->as_client); if (xret != 0) { stream_action = ACTION_FORWARD_DATA; - TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); + TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s, %d\n",nghttp2_strerror(xret), __LINE__); } } #ifdef TFE_LOG_HTTP2 - TFE_LOG_INFO(logger()->handle, "%s, %d, submit data %d, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - 0, (int)input_len, stream_id, session_info->stream_action); + TFE_LOG_INFO(logger()->handle, "%s, %d, submit data %d, stream_id:%d, action:%d", h2_stream_info->tf_stream->str_stream_info, + 0, (int)input_len, stream_id, h2_stream_info->stream_action); #endif if (stream_action == ACTION_USER_DATA) stream_action = ACTION_DROP_DATA; - session_info->stream_action = stream_action; + h2_stream_info->stream_action = stream_action; finish: return 0; } @@ -2442,19 +2181,19 @@ nghttp2_server_on_begin_headers(nghttp2_session *session, const nghttp2_frame *frame, void *user_data) { - struct tfe_session_info_t *session_info = (struct tfe_session_info_t *)user_data; + struct tfe_h2_stream *h2_stream_info = (struct tfe_h2_stream *)user_data; if (frame->hd.type != NGHTTP2_HEADERS || frame->headers.cat != NGHTTP2_HCAT_REQUEST) { return 0; } - create_serv_stream_data(session, frame->hd.stream_id, session_info); + create_serv_stream_data(session, frame->hd.stream_id, h2_stream_info); return 0; } static void -server_session_init(struct tfe_session_info_t *session_info) +server_session_init(struct tfe_h2_stream *h2_stream_info) { nghttp2_session_callbacks *callbacks; @@ -2477,67 +2216,68 @@ server_session_init(struct tfe_session_info_t *session_info) nghttp2_session_callbacks_set_on_begin_headers_callback( callbacks, nghttp2_server_on_begin_headers); - nghttp2_session_server_new(&session_info->as_server, callbacks, session_info); + nghttp2_session_server_new(&h2_stream_info->as_server, callbacks, h2_stream_info); nghttp2_session_callbacks_del(callbacks); } static void -delete_server_session_data(struct tfe_session_info_t *session_info) +delete_server_session_data(struct tfe_h2_stream *h2_stream_info) { - struct h2_stream_data_t *h2_stream; - struct h2_stream_data_t *_h2_stream; + struct tfe_h2_session *h2_session; + struct tfe_h2_session *peer_h2_stream; - nghttp2_session_del(session_info->as_server); - session_info->as_server = NULL; + nghttp2_session_del(h2_stream_info->as_server); + h2_stream_info->as_server = NULL; - TAILQ_FOREACH_SAFE(h2_stream, &session_info->list, next, _h2_stream){ - TAILQ_REMOVE(&session_info->list, h2_stream, next); - free(h2_stream); - h2_stream = NULL; + TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream) + { + TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next); + free(h2_session); + h2_session = NULL; } } static void -delete_client_session_data(struct tfe_session_info_t *session_info) +delete_client_session_data(struct tfe_h2_stream *h2_stream_info) { - struct h2_stream_data_t *h2_stream = NULL; - struct h2_stream_data_t *_h2_stream; + struct tfe_h2_session *h2_session = NULL; + struct tfe_h2_session *peer_h2_stream; - nghttp2_session_del(session_info->as_client); - session_info->as_client = NULL; + nghttp2_session_del(h2_stream_info->as_client); + h2_stream_info->as_client = NULL; - TAILQ_FOREACH_SAFE(h2_stream, &session_info->list, next, _h2_stream){ - TAILQ_REMOVE(&session_info->list, h2_stream, next); - free(h2_stream); - h2_stream = NULL; + TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream){ + TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next); + free(h2_session); + h2_session = NULL; } } enum tfe_stream_action -detect_up_stream_protocol(struct tfe_session_info_t *session_info, const struct tfe_stream *tfe_stream, +detect_up_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream, unsigned int thread_id, const unsigned char *data, size_t len) { int readlen = 0; enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - session_info->tf_stream = tfe_stream; - session_info->thread_id = thread_id; + h2_stream_info->tf_stream = tfe_stream; + h2_stream_info->thread_id = thread_id; - if (!session_info->as_server) + if (!h2_stream_info->as_server) goto forward; - readlen = nghttp2_session_mem_recv(session_info->as_client, data, len); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_client, data, len); if (readlen < 0){ TFE_LOG_ERROR(logger()->handle, "Failed to process server requests. Link message %s", tfe_stream->str_stream_info); - delete_client_session_data(session_info); + delete_client_session_data(h2_stream_info); goto forward; } - stream_action = session_info->stream_action; - session_info->stream_action = ACTION_DROP_DATA; - if (session_info->goaway){ - nghttp2_disect_goaway(session_info); - session_info->goaway = 0; + stream_action = h2_stream_info->stream_action; + h2_stream_info->stream_action = ACTION_DROP_DATA; + if (h2_stream_info->goaway){ + nghttp2_disect_goaway(h2_stream_info); + h2_stream_info->goaway = 0; } if (stream_action == ACTION_DROP_DATA){ tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len)); @@ -2553,30 +2293,30 @@ forward: } enum tfe_stream_action -detect_down_stream_protocol(struct tfe_session_info_t *session_info, const struct tfe_stream *tfe_stream, +detect_down_stream_protocol(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream *tfe_stream, unsigned int thread_id, const unsigned char *data, size_t len) { int readlen = 0; enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - session_info->tf_stream = tfe_stream; - session_info->thread_id = thread_id; + h2_stream_info->tf_stream = tfe_stream; + h2_stream_info->thread_id = thread_id; - if (!session_info->as_server) + if (!h2_stream_info->as_server) goto forward; - readlen = nghttp2_session_mem_recv(session_info->as_server, data, len); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, data, len); if (readlen < 0){ TFE_LOG_ERROR(logger()->handle, "Failed to process client requests. Link message %s", tfe_stream->str_stream_info); - delete_server_session_data(session_info); + delete_server_session_data(h2_stream_info); goto forward; } - stream_action = session_info->stream_action; - session_info->stream_action = ACTION_DROP_DATA; - if (session_info->goaway){ - nghttp2_disect_goaway(session_info); - session_info->goaway = 0; + stream_action = h2_stream_info->stream_action; + h2_stream_info->stream_action = ACTION_DROP_DATA; + if (h2_stream_info->goaway){ + nghttp2_disect_goaway(h2_stream_info); + h2_stream_info->goaway = 0; } if (stream_action == ACTION_DROP_DATA){ tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len)); @@ -2591,52 +2331,52 @@ forward: } void -sess_data_ctx_fini(struct tfe_session_info_t *session_info, +sess_data_ctx_fini(struct tfe_h2_stream *h2_stream_info, const struct tfe_stream * stream, unsigned int thread_id) { - struct h2_stream_data_t *h2_stream = NULL; - struct h2_stream_data_t *_h2_stream = NULL; + struct tfe_h2_session *h2_session = NULL; + struct tfe_h2_session *peer_h2_stream = NULL; - TAILQ_FOREACH_SAFE(h2_stream, &session_info->list, next, _h2_stream){ - TAILQ_REMOVE(&session_info->list, h2_stream, next); - if (h2_stream->frame_ctx){ - http_frame_raise_session_end(h2_stream->frame_ctx, stream, &h2_stream->tfe_session, + TAILQ_FOREACH_SAFE(h2_session, &h2_stream_info->h2_session_list, next, peer_h2_stream){ + TAILQ_REMOVE(&h2_stream_info->h2_session_list, h2_session, next); + if (h2_session->frame_ctx){ + http_frame_raise_session_end(h2_session->frame_ctx, stream, &h2_session->tfe_session, thread_id); - h2_stream->frame_ctx = NULL; + h2_session->frame_ctx = NULL; } - delete_http2_stream_data(h2_stream, session_info->tf_stream, 1); - free(h2_stream); - h2_stream = NULL; + delete_http2_stream_data(h2_session, h2_stream_info->tf_stream, 1); + free(h2_session); + h2_session = NULL; } - if (session_info->as_client){ - nghttp2_session_del(session_info->as_client); - session_info->as_client = NULL; + if (h2_stream_info->as_client){ + nghttp2_session_del(h2_stream_info->as_client); + h2_stream_info->as_client = NULL; } - if (session_info->as_server){ - nghttp2_session_del(session_info->as_server); - session_info->as_server = NULL; + if (h2_stream_info->as_server){ + nghttp2_session_del(h2_stream_info->as_server); + h2_stream_info->as_server = NULL; } } -struct tfe_session_info_t* tfe_session_info_init() +struct tfe_h2_stream* tfe_session_info_init() { - struct tfe_session_info_t *session_info = NULL; + struct tfe_h2_stream *h2_stream_info = NULL; - session_info = ALLOC(struct tfe_session_info_t, 1); - assert(session_info); + h2_stream_info = ALLOC(struct tfe_h2_stream, 1); + assert(h2_stream_info); - memset(session_info, 0, sizeof(struct tfe_session_info_t)); + memset(h2_stream_info, 0, sizeof(struct tfe_h2_stream)); - session_info->stream_action = ACTION_DROP_DATA; + h2_stream_info->stream_action = ACTION_DROP_DATA; - session_info->goaway = 0; + h2_stream_info->goaway = 0; - server_session_init(session_info); + server_session_init(h2_stream_info); - client_session_init(session_info); + client_session_init(h2_stream_info); - TAILQ_INIT(&session_info->list); + TAILQ_INIT(&h2_stream_info->h2_session_list); - return session_info; + return h2_stream_info; } diff --git a/plugin/protocol/http2/test/test_http2_stream.cpp b/plugin/protocol/http2/test/test_http2_stream.cpp index bdb5c03..2e29cc8 100644 --- a/plugin/protocol/http2/test/test_http2_stream.cpp +++ b/plugin/protocol/http2/test/test_http2_stream.cpp @@ -57,10 +57,10 @@ void UT_Parse_ReqHeaders(nghttp2_session *as_server) { int stream_id = 1; - struct h2_stream_data_t *h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(as_server, stream_id); - ASSERT_TRUE(h2_stream != NULL); + struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(as_server, stream_id); + ASSERT_TRUE(h2_session != NULL); - struct http2_half_private *half_private_req = h2_stream->req; + struct tfe_h2_half_private *half_private_req = h2_session->req; struct tfe_http_half *tfe_half = &half_private_req->half_public; struct tfe_http_req_spec *req_spec = &tfe_half->req_spec; @@ -124,10 +124,10 @@ void UT_Parse_RespHeaders(nghttp2_session *as_client) { int stream_id = 1; - struct h2_stream_data_t *h2_stream = (struct h2_stream_data_t *)nghttp2_session_get_stream_user_data(as_client, stream_id); - ASSERT_TRUE(h2_stream != NULL); + struct tfe_h2_session *h2_session = (struct tfe_h2_session *)nghttp2_session_get_stream_user_data(as_client, stream_id); + ASSERT_TRUE(h2_session != NULL); - struct http2_half_private *half_private_req = h2_stream->resp; + struct tfe_h2_half_private *half_private_req = h2_session->resp; struct tfe_http_half *tfe_half = &half_private_req->half_public; struct tfe_http_resp_spec *resp_spec = &tfe_half->resp_spec; @@ -181,16 +181,16 @@ TEST(Http2StreamParser, GetFrameWithMagic) struct stream_tap_info_t *tapinfo = ALLOC(struct stream_tap_info_t, 1); assert(tapinfo); - tapinfo->session_info = tfe_session_info_init(); - ASSERT_FALSE(tapinfo->session_info == NULL); + tapinfo->h2_stream_info = tfe_session_info_init(); + ASSERT_FALSE(tapinfo->h2_stream_info == NULL); - struct tfe_session_info_t *session_info = tapinfo->session_info; + struct tfe_h2_stream *h2_stream_info = tapinfo->h2_stream_info; /*Http2 protocol**/ EXPECT_EQ(memcmp(magic_headers, kMagicHello, MAGIC_FRAME_LENGTH), 0); /*Recv data magic**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, magic_headers, sizeof(magic_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, magic_headers, sizeof(magic_headers)); EXPECT_GT(readlen, 0); } @@ -200,20 +200,20 @@ TEST(Http2StreamParser, GetFrameWithHeader_01) struct stream_tap_info_t *tapinfo = ALLOC(struct stream_tap_info_t, 1); assert(tapinfo); - tapinfo->session_info = tfe_session_info_init(); - ASSERT_FALSE(tapinfo->session_info == NULL); + tapinfo->h2_stream_info = tfe_session_info_init(); + ASSERT_FALSE(tapinfo->h2_stream_info == NULL); - struct tfe_session_info_t *session_info = tapinfo->session_info; + struct tfe_h2_stream *h2_stream_info = tapinfo->h2_stream_info; /*Recv data magic**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, magic_headers, sizeof(magic_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, magic_headers, sizeof(magic_headers)); EXPECT_GT(readlen, 0); /*Recv request Headers**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, request_get_headers, sizeof(request_get_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, request_get_headers, sizeof(request_get_headers)); EXPECT_GT(readlen, 0); - UT_Parse_ReqHeaders(session_info->as_server); + UT_Parse_ReqHeaders(h2_stream_info->as_server); } TEST(Http2StreamParser, GetFrameWithHeader_02) @@ -222,22 +222,22 @@ TEST(Http2StreamParser, GetFrameWithHeader_02) struct stream_tap_info_t *tapinfo = ALLOC(struct stream_tap_info_t, 1); assert(tapinfo); - tapinfo->session_info = tfe_session_info_init(); - ASSERT_FALSE(tapinfo->session_info == NULL); + tapinfo->h2_stream_info = tfe_session_info_init(); + ASSERT_FALSE(tapinfo->h2_stream_info == NULL); - struct tfe_session_info_t *session_info = tapinfo->session_info; + struct tfe_h2_stream *h2_stream_info = tapinfo->h2_stream_info; /*Recv data magic**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, magic_headers, sizeof(magic_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, magic_headers, sizeof(magic_headers)); EXPECT_GT(readlen, 0); /*Recv request Headers**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, request_get_headers, sizeof(request_get_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, request_get_headers, sizeof(request_get_headers)); EXPECT_GT(readlen, 0); /*Send data message**/ //enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - //stream_action = nghttp2_client_mem_send(session_info); + //stream_action = nghttp2_client_mem_send(h2_stream_info); //EXPECT_EQ(stream_action, ACTION_DROP_DATA); } @@ -247,32 +247,32 @@ TEST(Http2StreamParser, RespFrameWithHead_01) struct stream_tap_info_t *tapinfo = ALLOC(struct stream_tap_info_t, 1); assert(tapinfo); - tapinfo->session_info = tfe_session_info_init(); - ASSERT_FALSE(tapinfo->session_info == NULL); + tapinfo->h2_stream_info = tfe_session_info_init(); + ASSERT_FALSE(tapinfo->h2_stream_info == NULL); - struct tfe_session_info_t *session_info = tapinfo->session_info; + struct tfe_h2_stream *h2_stream_info = tapinfo->h2_stream_info; /*Recv data magic**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, magic_headers, sizeof(magic_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, magic_headers, sizeof(magic_headers)); EXPECT_GT(readlen, 0); /*Recv request Headers**/ - readlen = nghttp2_session_mem_recv(session_info->as_server, request_get_headers, sizeof(request_get_headers)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_server, request_get_headers, sizeof(request_get_headers)); EXPECT_GT(readlen, 0); /*Send data head message**/ //enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; - //stream_action = nghttp2_client_mem_send(session_info); + //stream_action = nghttp2_client_mem_send(h2_stream_info); //EXPECT_EQ(stream_action, ACTION_DROP_DATA); /*Recv response settings*/ - readlen = nghttp2_session_mem_recv(session_info->as_client, response_settings, sizeof(response_settings)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_client, response_settings, sizeof(response_settings)); EXPECT_GT(readlen, 0); /*Recv response Header*/ - readlen = nghttp2_session_mem_recv(session_info->as_client, response_header2, sizeof(response_header2)); + readlen = nghttp2_session_mem_recv(h2_stream_info->as_client, response_header2, sizeof(response_header2)); EXPECT_GT(readlen, 0); - UT_Parse_RespHeaders(session_info->as_client); + UT_Parse_RespHeaders(h2_stream_info->as_client); } #endif @@ -291,18 +291,18 @@ TEST(Http2StreamParser, FrameWithJdData) struct stream_tap_info_t *tapinfo = ALLOC(struct stream_tap_info_t, 1); assert(tapinfo); - tapinfo->session_info = tfe_session_info_init(); - ASSERT_FALSE(tapinfo->session_info == NULL); + tapinfo->h2_stream_info = tfe_session_info_init(); + ASSERT_FALSE(tapinfo->h2_stream_info == NULL); - struct tfe_session_info_t *session_info = tapinfo->session_info; + struct tfe_h2_stream *h2_stream_info = tapinfo->h2_stream_info; for (len = 0; len <= 7; len++){ if (len > 0 && len < 4){ printf("Proc down stream(%d)\n", len); - action = detect_down_stream_protocol(session_info, NULL, 0, data[len], datalen[len]); + action = detect_down_stream_protocol(h2_stream_info, NULL, 0, data[len], datalen[len]); }else{ printf("Proc up stream(%d)\n", len); - action = detect_up_stream_protocol(session_info, NULL, 0, data[len], datalen[len]); + action = detect_up_stream_protocol(h2_stream_info, NULL, 0, data[len], datalen[len]); } } } @@ -310,12 +310,12 @@ TEST(Http2StreamParser, FrameWithJdData) TEST(UI_TEST_INFLATE_GZIP, inflate_01) { char *uncompr = NULL; - struct data_t body; + struct tfe_h2_payload body; int ret = 0, uncompr_len = 0; - memset(&body, 0, sizeof(struct data_t)); + memset(&body, 0, sizeof(struct tfe_h2_payload)); - ret = inflate_read(ut_gip_01, sizeof(ut_gip_01), &uncompr, &uncompr_len, &(body.inflater), 2); + ret = inflate_read(ut_gip_01, sizeof(ut_gip_01), &uncompr, &uncompr_len, &(body.inflate), 2); EXPECT_EQ(ret, Z_STREAM_END); EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01)); @@ -327,19 +327,19 @@ TEST(UI_TEST_INFLATE_GZIP, inflate_01) TEST(UI_TEST_INFLATE_GZIP, inflate_02) { - struct data_t body; + struct tfe_h2_payload body; int ret = 0, half = 0; int size = sizeof(ut_gip_01); half = size / 2; - body.inflater = NULL; + body.inflate = NULL; char *uncompr1 = NULL; int uncompr_len1 = 0; - ret = inflate_read(ut_gip_01, half, &uncompr1, &uncompr_len1, &(body.inflater), 2); + ret = inflate_read(ut_gip_01, half, &uncompr1, &uncompr_len1, &(body.inflate), 2); EXPECT_EQ(ret, Z_OK); char *uncompr2 = NULL; int uncompr_len2 = 0; - ret = inflate_read(ut_gip_01 + half, size - half, &uncompr2, &uncompr_len2, &(body.inflater), 2); + ret = inflate_read(ut_gip_01 + half, size - half, &uncompr2, &uncompr_len2, &(body.inflate), 2); EXPECT_EQ(ret, Z_STREAM_END); char uncompr[1024] = {0}; @@ -356,12 +356,12 @@ TEST(UI_TEST_INFLATE_GZIP, inflate_02) TEST(UI_TEST_DEFLATE_GZIP, deflate_01) { unsigned char *dest = NULL; - struct data_t body; + struct tfe_h2_payload body; int ret = 0, dlen = 0; struct evbuffer * buf = evbuffer_new(); int size = sizeof(ut_ungip_01); - memset(&body, 0, sizeof(struct data_t)); + memset(&body, 0, sizeof(struct tfe_h2_payload)); ret = deflate_write(&(body.deflate), ut_ungip_01, size, buf, 2, 1); EXPECT_EQ(ret, Z_OK); @@ -371,7 +371,7 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_01) char *uncompr = NULL; int uncompr_len = 0; - ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflater), 2); + ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflate), 2); EXPECT_EQ(ret, Z_STREAM_END); EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01)); EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0); @@ -383,7 +383,7 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_01) TEST(UI_TEST_DEFLATE_GZIP, deflate_02) { - struct data_t body; + struct tfe_h2_payload body; unsigned char *dest = NULL; int ret = 0, half = 0, dlen = 0; @@ -392,7 +392,7 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_02) struct evbuffer * buf = evbuffer_new(); - memset(&body, 0, sizeof(struct data_t)); + memset(&body, 0, sizeof(struct tfe_h2_payload)); /* First frag */ ret = deflate_write(&(body.deflate), ut_ungip_01, half, buf, 2, 0); @@ -411,7 +411,7 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_02) char *uncompr = NULL; int uncompr_len = 0; - ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflater), 2); + ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflate), 2); EXPECT_EQ(ret, Z_STREAM_END); EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01)); EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0); @@ -424,12 +424,12 @@ TEST(UI_TEST_DEFLATE_GZIP, deflate_02) TEST(UI_TEST_DEFLATE_BR, deflate_01) { unsigned char *dest = NULL; - struct data_t body; + struct tfe_h2_payload body; int ret = 0, dlen = 0; struct evbuffer * buf = evbuffer_new(); int size = sizeof(ut_ungip_01); - memset(&body, 0, sizeof(struct data_t)); + memset(&body, 0, sizeof(struct tfe_h2_payload)); ret = deflate_write(&(body.deflate), ut_ungip_01, size, buf, HTTP2_CONTENT_ENCODING_BR, 1); EXPECT_EQ(ret, Z_OK); @@ -439,7 +439,7 @@ TEST(UI_TEST_DEFLATE_BR, deflate_01) char *uncompr = NULL; int uncompr_len = 0; - ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflater), HTTP2_CONTENT_ENCODING_BR); + ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflate), HTTP2_CONTENT_ENCODING_BR); EXPECT_EQ(ret, Z_STREAM_END); EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01)); EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0); @@ -451,7 +451,7 @@ TEST(UI_TEST_DEFLATE_BR, deflate_01) TEST(UI_TEST_DEFLATE_BR, deflate_02) { - struct data_t body; + struct tfe_h2_payload body; unsigned char *dest = NULL; int ret = 0, half = 0, dlen = 0; @@ -460,7 +460,7 @@ TEST(UI_TEST_DEFLATE_BR, deflate_02) struct evbuffer * buf = evbuffer_new(); - memset(&body, 0, sizeof(struct data_t)); + memset(&body, 0, sizeof(struct tfe_h2_payload)); /* First frag */ ret = deflate_write(&(body.deflate), ut_ungip_01, half, buf, HTTP2_CONTENT_ENCODING_BR, 0); @@ -479,7 +479,7 @@ TEST(UI_TEST_DEFLATE_BR, deflate_02) char *uncompr = NULL; int uncompr_len = 0; - ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflater), HTTP2_CONTENT_ENCODING_BR); + ret = inflate_read(dest, dlen, &uncompr, &uncompr_len, &(body.inflate), HTTP2_CONTENT_ENCODING_BR); EXPECT_EQ(ret, Z_STREAM_END); EXPECT_EQ(uncompr_len, sizeof(ut_ungip_01)); EXPECT_EQ(memcmp(uncompr, ut_ungip_01, sizeof(ut_ungip_01)), 0);