From 6a3499dbf3d5d8c656bd0e2125a20abe37752f88 Mon Sep 17 00:00:00 2001 From: fengweihao Date: Fri, 15 Apr 2022 15:26:53 +0800 Subject: [PATCH] =?UTF-8?q?TSG-10225=20=E4=BF=AE=E5=A4=8D=E5=BA=94?= =?UTF-8?q?=E7=AD=94=E7=A0=81=E4=B8=BA103=E6=97=B6=EF=BC=8C=E5=BA=94?= =?UTF-8?q?=E7=AD=94=E5=A4=B4=E9=87=8D=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../http2/include/internal/http2_common.h | 39 ++++++++----- plugin/protocol/http2/src/http2_stream.cpp | 56 +++++++++++++------ .../protocol/http2/test/test_http2_stream.cpp | 2 +- 3 files changed, 65 insertions(+), 32 deletions(-) diff --git a/plugin/protocol/http2/include/internal/http2_common.h b/plugin/protocol/http2/include/internal/http2_common.h index 18a8d7d..d34c26c 100644 --- a/plugin/protocol/http2/include/internal/http2_common.h +++ b/plugin/protocol/http2/include/internal/http2_common.h @@ -14,27 +14,40 @@ #include #include -typedef struct RTLogInit2Data_ { +typedef struct RTLogInit2Data_ +{ void *handle; } RTLogInit2Data; -typedef struct Http2Plugin_{ +typedef struct Http2Plugin_ +{ struct event * event[TFE_THREAD_MAX]; struct tfe_h2_stream h2_stream[TFE_THREAD_MAX]; } Http2Plugin; -typedef enum { - NGHTTP2_METHOD_DELETE = 0, - NGHTTP2_METHOD_GET = 1, - NGHTTP2_METHOD_HEAD = 2, - NGHTTP2_METHOD_POST = 3, - NGHTTP2_METHOD_PUT = 4, - NGHTTP2_METHOD_CONNECT = 5, - NGHTTP2_METHOD_OPTIONS = 6, - NGHTTP2_METHOD_UNKNOWN, -}nghttp2_method; +enum http_request_method +{ + HTTP_REQUEST_METHOD_DELETE = 0, + HTTP_REQUEST_METHOD_GET = 1, + HTTP_REQUEST_METHOD_HEAD = 2, + HTTP_REQUEST_METHOD_POST = 3, + HTTP_REQUEST_METHOD_PUT = 4, + HTTP_REQUEST_METHOD_CONNECT = 5, + HTTP_REQUEST_METHOD_OPTIONS = 6, + HTTP_REQUEST_METHOD_UNKNOWN, +}; -struct value_string { +enum http_resp_code +{ + HTTP_RESP_CODE_100 = 100, + HTTP_RESP_CODE_101 = 101, + HTTP_RESP_CODE_102 = 102, + HTTP_RESP_CODE_103 = 103, + HTTP_RESP_CODE_200 = 200, +}; + +struct value_string +{ unsigned int value; const char *strptr; }; diff --git a/plugin/protocol/http2/src/http2_stream.cpp b/plugin/protocol/http2/src/http2_stream.cpp index 0f296ed..a92bab7 100644 --- a/plugin/protocol/http2/src/http2_stream.cpp +++ b/plugin/protocol/http2/src/http2_stream.cpp @@ -41,14 +41,14 @@ static const struct value_string method_vals[] = { - {NGHTTP2_METHOD_DELETE, "DELETE"}, - {NGHTTP2_METHOD_GET, "GET"}, - {NGHTTP2_METHOD_HEAD, "HEAD"}, - {NGHTTP2_METHOD_POST, "POST"}, - {NGHTTP2_METHOD_PUT, "PUT"}, - {NGHTTP2_METHOD_CONNECT, "CONNECT"}, - {NGHTTP2_METHOD_OPTIONS, "OPTIONS"}, - {NGHTTP2_METHOD_UNKNOWN, "unknown"}, + {HTTP_REQUEST_METHOD_DELETE, "DELETE"}, + {HTTP_REQUEST_METHOD_GET, "GET"}, + {HTTP_REQUEST_METHOD_HEAD, "HEAD"}, + {HTTP_REQUEST_METHOD_POST, "POST"}, + {HTTP_REQUEST_METHOD_PUT, "PUT"}, + {HTTP_REQUEST_METHOD_CONNECT, "CONNECT"}, + {HTTP_REQUEST_METHOD_OPTIONS, "OPTIONS"}, + {HTTP_REQUEST_METHOD_UNKNOWN, "unknown"}, }; typedef enum { @@ -130,7 +130,7 @@ static void tfe_h2_header_add_field(struct tfe_h2_header *h2_header, const struc peer_h2_field->nv.name = (uint8_t *)tfe_strdup((const char *)std_name); peer_h2_field->nv.namelen = strlen(std_name); } - peer_h2_field->nv.value = (uint8_t *)tfe_strdup((const char *)value);; + 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) @@ -212,7 +212,8 @@ static nghttp2_nv* tfe_h2_header_convert_nv(struct tfe_h2_header *header, nghttp int nvlen = 0; struct tfe_h2_field *h2_field = NULL, *peer_h2_field = NULL; - TAILQ_FOREACH_SAFE(h2_field, &header->h2_field_list, next, peer_h2_field){ + 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; @@ -1090,8 +1091,8 @@ void http2_disect_goaway(struct tfe_h2_stream *h2_stream_info) static int http2_submit_frame_goaway(struct tfe_h2_stream *connection, const nghttp2_frame *frame, enum tfe_conn_dir dir) { int xret = -1; + const char *opaque_data = NULL; enum tfe_stream_action stream_action = ACTION_DROP_DATA; - char *error = NULL; size_t eroro_len=0; const nghttp2_goaway *goaway = &frame->goaway; nghttp2_session *ngh2_session = tfe_h2_stream_get_http2_session(connection, dir); @@ -1111,12 +1112,9 @@ static int http2_submit_frame_goaway(struct tfe_h2_stream *connection, const ngh dir, nghttp2_strerror(xret)); } finish: - eroro_len = goaway->opaque_data_len; - error = ALLOC(char, eroro_len + 1); - snprintf(error, eroro_len, "%s", goaway->opaque_data); + opaque_data = ((const char *)goaway->opaque_data) != NULL ? (char *)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); - FREE(&error); + dir, goaway->last_stream_id, connection->stream_action, goaway->error_code, opaque_data); connection->goaway = 1; connection->stream_action = stream_action; return 0; @@ -1513,6 +1511,26 @@ static enum tfe_stream_action http2_submit_built_response(struct tfe_h2_stream * return stream_action; } +static void h2_delete_resp_hdrs_filter(struct tfe_h2_header *headers, nghttp2_nv *hdrs) +{ + if((headers->nvlen > 0) && strncasecmp((char *)hdrs[0].name, ":status", strlen((char *)hdrs[0].name)) == 0) + { + int resp_code = atoi((const char *)hdrs[0].value); + switch(resp_code) + { + case HTTP_RESP_CODE_100: + case HTTP_RESP_CODE_101: + case HTTP_RESP_CODE_102: + case HTTP_RESP_CODE_103: + delete_nv_packet_data(headers); + break; + default: + break; + } + } + return; +} + static enum tfe_stream_action http2_server_frame_submit_header(struct tfe_h2_stream *h2_stream_info, struct tfe_h2_session *h2_session) { int32_t xret = 0; @@ -1548,6 +1566,8 @@ static enum tfe_stream_action http2_server_frame_submit_header(struct tfe_h2_str printf("Fatal headers error: %s\n", nghttp2_strerror(xret)); } + h2_delete_resp_hdrs_filter(headers, hdrs); + return stream_action; } @@ -1681,7 +1701,7 @@ static enum tfe_stream_action http2_client_frame_submit_header(struct tfe_h2_str int32_t stream_id = -1; 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_http_std_method method = (enum tfe_http_std_method)HTTP_REQUEST_METHOD_UNKNOWN; enum tfe_stream_action stream_action = ACTION_FORWARD_DATA; req = h2_session->plugin_built_req != NULL ? h2_session->plugin_built_req : h2_session->req; @@ -1704,7 +1724,7 @@ static enum tfe_stream_action http2_client_frame_submit_header(struct tfe_h2_str } method = http2_get_method(h2_session->req); - if (method == (enum tfe_http_std_method)NGHTTP2_METHOD_POST || method == (enum tfe_http_std_method)NGHTTP2_METHOD_PUT) + if (method == (enum tfe_http_std_method)HTTP_REQUEST_METHOD_POST || method == (enum tfe_http_std_method)HTTP_REQUEST_METHOD_PUT) { if (h2_session->plugin_built_req != NULL) { diff --git a/plugin/protocol/http2/test/test_http2_stream.cpp b/plugin/protocol/http2/test/test_http2_stream.cpp index e1b5160..64be21b 100644 --- a/plugin/protocol/http2/test/test_http2_stream.cpp +++ b/plugin/protocol/http2/test/test_http2_stream.cpp @@ -68,7 +68,7 @@ void UT_Parse_ReqHeaders(nghttp2_session *as_server) struct tfe_http_req_spec *req_spec = &tfe_half->req_spec; /* PUBLIC FIELD */ - EXPECT_EQ(req_spec->method, NGHTTP2_METHOD_GET); + EXPECT_EQ(req_spec->method, HTTP_REQUEST_METHOD_GET); EXPECT_STREQ(req_spec->uri, "/"); EXPECT_STREQ(req_spec->url, "www.jd.com/"); EXPECT_STREQ(req_spec->host, "www.jd.com");