diff options
| author | fengweihao <[email protected]> | 2019-04-02 13:01:15 +0800 |
|---|---|---|
| committer | zhengchao <[email protected]> | 2019-05-24 18:45:45 +0800 |
| commit | a22e0ed630ffff436e15da663142161bc1abd69b (patch) | |
| tree | 376cdc69dac454c17483154ae6977f5a124e2817 /plugin/protocol/http2/src/http2_stream.cpp | |
| parent | ad004b29a6974bec925407a0a1dc04769178bb06 (diff) | |
*删除nghttp2中收到ping ack字符会ping
*由于命中匹配规则且返回数据中只有 header,造成数据丢失,在当前流close时添加header注册接口
Diffstat (limited to 'plugin/protocol/http2/src/http2_stream.cpp')
| -rw-r--r-- | plugin/protocol/http2/src/http2_stream.cpp | 136 |
1 files changed, 90 insertions, 46 deletions
diff --git a/plugin/protocol/http2/src/http2_stream.cpp b/plugin/protocol/http2/src/http2_stream.cpp index 5a47efd..4528f20 100644 --- a/plugin/protocol/http2/src/http2_stream.cpp +++ b/plugin/protocol/http2/src/http2_stream.cpp @@ -46,6 +46,7 @@ static const struct value_string method_vals[] = {NGHTTP2_METHOD_POST, "POST"}, {NGHTTP2_METHOD_PUT, "PUT"}, {NGHTTP2_METHOD_CONNECT, "CONNECT"}, + {NGHTTP2_METHOD_OPTIONS, "OPTIONS"}, {NGHTTP2_METHOD_UNKNOWN, "unknown"}, }; @@ -803,8 +804,8 @@ nghttp2_submit_frame_rst_stream(struct tfe_session_info_t *session_info,const ng } finish: session_info->stream_action = stream_action; - TFE_LOG_INFO(logger()->handle, "%s, %d, submit rst stream, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - dir, frame->hd.stream_id, session_info->stream_action); + TFE_LOG_INFO(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); return 0; } @@ -879,6 +880,41 @@ finish: return 0; } +void +nghttp2_write_access_log(struct h2_stream_data_t *h2_stream, const char * str_stream_info) +{ + /* Request */ + struct http2_half_private *req = h2_stream->req; + /* Response */ + struct http2_half_private *resp = h2_stream->resp; + /* Req-Public */ + struct tfe_http_req_spec *req_spec = req ? &(req->half_public.req_spec) : NULL; + /* Resp-Public */ + struct tfe_http_resp_spec *resp_spec = resp ? &(resp->half_public.resp_spec) : NULL; + + const char * method = req_spec ? val_to_str(req_spec->method, method_vals) : "-"; + const char * url = req_spec ? req_spec->url : "-"; + char resp_code[TFE_STRING_MAX]; + if (resp_spec) + snprintf(resp_code, sizeof(resp_code) - 1, "%d", resp_spec->resp_code); + else + snprintf(resp_code, sizeof(resp_code) - 1, "%s", "-"); + + 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" : "-"; + + 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, + 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, const struct tfe_stream *tf_stream, int body_flag) @@ -1268,6 +1304,42 @@ nghttp2_headers_write_log(struct h2_stream_data_t *h2_stream, const char * str_s } static int +nghttp2_end_submit_header2(struct tfe_session_info_t *session_info, + struct h2_stream_data_t *h2_stream) +{ + 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; + enum tfe_stream_action stream_action = ACTION_DROP_DATA; + + headers = resp->headers; + if (headers.nvlen <= 0){ + 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); + if (stream_id < 0){ + printf("Fatal headers error: %s\n", nghttp2_strerror(stream_id)); + stream_action = ACTION_FORWARD_DATA; + } + delete_nv_packet_data(&headers); + + if (stream_action == ACTION_DROP_DATA){ + xret = nghttp2_session_send(session_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)); + } + } +finish: + session_info->stream_action = stream_action; + return 0; +} + +static int nghttp2_server_submit_header(struct tfe_session_info_t *session_info, int32_t stream_id) { int xret = -1; @@ -1694,7 +1766,7 @@ nghttp2_data_send(nghttp2_session *session, const nghttp2_frame *frame, const ui return (ssize_t)length; } -static void +static int submit_end_data(struct tfe_session_info_t *session_info, struct h2_stream_data_t *h2_stream) { @@ -1702,6 +1774,12 @@ submit_end_data(struct tfe_session_info_t *session_info, enum tfe_stream_action stream_action = ACTION_DROP_DATA; struct http2_half_private *resp = h2_stream->resp; + + if (resp->body_state == MANAGE_STAGE_INIT){ + nghttp2_end_submit_header2(session_info, h2_stream); + //return 1; + } + if (resp->body_state != MANAGE_STAGE_INIT){ if (resp->event_cb) { resp->event_cb(resp, EV_HTTP_RESP_BODY_END, NULL, 0, @@ -1731,7 +1809,7 @@ submit_end_data(struct tfe_session_info_t *session_info, if (stream_action == ACTION_USER_DATA) stream_action = ACTION_DROP_DATA; session_info->stream_action = stream_action; - return; + return 1; } static int @@ -1739,6 +1817,7 @@ 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) { + int xret = -1; struct h2_stream_data_t *h2_stream = NULL; struct http2_half_private *resp = NULL; @@ -1760,8 +1839,9 @@ nghttp2_on_stream_close(nghttp2_session *session, const nghttp2_frame *frame, co resp = h2_stream->resp; if (error_code == 0 && resp->body_state != MANAGE_STAGE_COMPLETE){ - submit_end_data(session_info, h2_stream); - goto end; + xret = submit_end_data(session_info, h2_stream); + if (xret == 1) + goto end; } finish: TAILQ_REMOVE(&session_info->list, h2_stream, next); @@ -1788,41 +1868,6 @@ nghttp2_callback nghttp2_callback_array[] = { [NGHTTP2_USER_COLSE] = nghttp2_on_stream_close }; -void -nghttp2_write_access_log(struct h2_stream_data_t *h2_stream, const char * str_stream_info) -{ - /* Request */ - struct http2_half_private *req = h2_stream->req; - /* Response */ - struct http2_half_private *resp = h2_stream->resp; - /* Req-Public */ - struct tfe_http_req_spec *req_spec = req ? &(req->half_public.req_spec) : NULL; - /* Resp-Public */ - struct tfe_http_resp_spec *resp_spec = resp ? &(resp->half_public.resp_spec) : NULL; - - const char * method = req_spec ? val_to_str(req_spec->method, method_vals) : "-"; - const char * url = req_spec ? req_spec->url : "-"; - char resp_code[TFE_STRING_MAX]; - if (resp_spec) - snprintf(resp_code, sizeof(resp_code) - 1, "%d", resp_spec->resp_code); - else - snprintf(resp_code, sizeof(resp_code) - 1, "%s", "-"); - - 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" : "-"; - - 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, - method, url, resp_code, cont_type, cont_encoding, pangu_req, pangu_resp); - - TFE_LOG_INFO(logger()->handle, "%s", access_log); - free(access_log); -} - static ssize_t nghttp2_client_send(nghttp2_session *session, const uint8_t *data, size_t length, int flags, void *user_data) @@ -1914,12 +1959,12 @@ nghttp2_client_on_data_chunk_recv(nghttp2_session *session, uint8_t flags, TFE_LOG_ERROR(logger()->handle, "Fatal upstream send error: %s\n",nghttp2_strerror(xret)); } } + TFE_LOG_INFO(logger()->handle, "%s, 1, submit data %d, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, + (int)input_len, stream_id, stream_action); if (stream_action == ACTION_USER_DATA) stream_action = ACTION_DROP_DATA; session_info->stream_action = stream_action; finish: - TFE_LOG_INFO(logger()->handle, "%s, 1, submit data %d, stream_id:%d, action:%d", session_info->tf_stream->str_stream_info, - (int)input_len, stream_id, session_info->stream_action); return 0; } @@ -2319,7 +2364,7 @@ detect_up_stream_protocol(struct tfe_session_info_t *session_info, const struct session_info->goaway = 0; } if (stream_action == ACTION_DROP_DATA){ - tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len)); + tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len)); return ACTION_DROP_DATA; } @@ -2357,7 +2402,6 @@ detect_down_stream_protocol(struct tfe_session_info_t *session_info, const struc } stream_action = session_info->stream_action; session_info->stream_action = ACTION_DROP_DATA; -// printf("down stream_acion = %d\n", stream_action); if (stream_action == ACTION_FORWARD_DATA){ xret = nghttp2_session_send(session_info->as_server); if (xret != 0) { @@ -2371,7 +2415,7 @@ detect_down_stream_protocol(struct tfe_session_info_t *session_info, const struc session_info->goaway = 0; } if (stream_action == ACTION_DROP_DATA){ - tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len)); + tfe_stream_action_set_opt(tfe_stream, ACTION_OPT_DROP_BYTES, &len, sizeof(len)); return ACTION_DROP_DATA; } forward: |
