From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 2935E278F8 for ; Thu, 21 Feb 2019 10:09:53 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8Y36BnPXxQg7 for ; Thu, 21 Feb 2019 10:09:51 -0500 (EST) Received: from smtp54.i.mail.ru (smtp54.i.mail.ru [217.69.128.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 76E7426984 for ; Thu, 21 Feb 2019 10:09:49 -0500 (EST) From: Kirill Shcherbatov Subject: [tarantool-patches] [PATCH v1 1/1] http: fix httpc auto-managed headers Date: Thu, 21 Feb 2019 18:09:45 +0300 Message-Id: <9099f8a339c278e3a5dac683923d13c7ee470ce9.1550761676.git.kshcherbatov@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org, v.shpilevoy@tarantool.org Cc: Kirill Shcherbatov The https library intelligently sets the headers "Accept", "Content-Length", "Connection", "Keep-Alive: timeout". However, when the user explicitly specified them in the header options section of the call argument, they could be written to the HTTP request twice. Was introduced service http c_managed_header_mask mask in which the bits corresponding to the http auto-manager headers, set 1 when referenced. When the bit is set, the new value is not applied. In this way, we prefer user-defined value for all httpc-managed headers. Closes #3955 https://github.com/tarantool/tarantool/tree/kshch/gh-3955-httpc-auto-managed-headers-redefine https://github.com/tarantool/tarantool/issues/3955 --- src/httpc.c | 41 +++++++++++++++++++++++++++++++ src/httpc.h | 8 ++++++ test/app-tap/http_client.test.lua | 12 ++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/httpc.c b/src/httpc.c index 950f8b32f..a04225d32 100644 --- a/src/httpc.c +++ b/src/httpc.c @@ -170,6 +170,42 @@ httpc_request_delete(struct httpc_request *req) mempool_free(&req->env->req_pool, req); } +/** + * Update bitmask of the http request headers that httpc may set + * automatically. In case of reserved pattern is found in header, + * routine sets corresponding bit in auto_headers_mask. + * Returns -1 when header is reserved and it's bit is already set + * in auto_headers_mask; 0 otherwise, +*/ +static int +httpc_managed_headers_mask_update(uint8_t *auto_headers_mask, + const char *header) +{ + static struct { + const char *header_name; + uint32_t header_name_len; + int8_t bit_index; + } httpc_managed_headers[] = { + {"Connection", 10, 1}, + {"Keep-Alive: timeout", 19, 2}, + {"Content-Length", 14, 4}, + {"Accept", 6, 8}, + }; + uint32_t httpc_managed_headers_cnt = + sizeof(httpc_managed_headers)/sizeof(httpc_managed_headers[0]); + for (uint32_t i = 0; i < httpc_managed_headers_cnt; i++) { + if (strncasecmp(header, httpc_managed_headers[i].header_name, + httpc_managed_headers[i].header_name_len) != 0) + continue; + if ((*auto_headers_mask & + httpc_managed_headers[i].bit_index) != 0) + return -1; + *auto_headers_mask |= httpc_managed_headers[i].bit_index; + return 0; + } + return 0; +} + int httpc_set_header(struct httpc_request *req, const char *fmt, ...) { @@ -178,6 +214,11 @@ httpc_set_header(struct httpc_request *req, const char *fmt, ...) const char *header = tt_vsprintf(fmt, ap); va_end(ap); + if (httpc_managed_headers_mask_update(&req->auto_headers_mask, + header) != 0) { + /* Do not add extra header to list. */ + return 0; + } struct curl_slist *l = curl_slist_append(req->headers, header); if (l == NULL) { diag_set(OutOfMemory, strlen(header), "curl", "http header"); diff --git a/src/httpc.h b/src/httpc.h index dc709e6fa..d30343be1 100644 --- a/src/httpc.h +++ b/src/httpc.h @@ -109,6 +109,14 @@ struct httpc_request { struct region resp_headers; /** buffer of body */ struct region resp_body; + /** + * A bitmask of the http request headers from among + * the headers that httpc can set automatically. + * This allows to prevent settings such headers twice. + * Read httpc_managed_headers_mask_update definition for + * more details. + */ + uint8_t auto_headers_mask; }; /** diff --git a/test/app-tap/http_client.test.lua b/test/app-tap/http_client.test.lua index 10a3728f8..8d1c50549 100755 --- a/test/app-tap/http_client.test.lua +++ b/test/app-tap/http_client.test.lua @@ -79,6 +79,14 @@ local function test_http_client(test, url, opts) test:is(r.status, 200, 'request') end +local function test_http_client_headers_redefine(test, url, opts) + test:plan(2) + opts.headers={['Connection'] = 'Keep-Alive'} + local r = client.get(url, opts) + test:is(r.status, 200, 'simple 200') + test:is(r.headers["connection"], 'Keep-Alive', 'redefined Connection header') +end + local function test_cancel_and_errinj(test, url, opts) test:plan(3) local ch = fiber.channel(1) @@ -397,9 +405,11 @@ local function test_concurrent(test, url, opts) end function run_tests(test, sock_family, sock_addr) - test:plan(9) + test:plan(10) local server, url, opts = start_server(test, sock_family, sock_addr) test:test("http.client", test_http_client, url, opts) + test:test("http.client headers redefine", test_http_client_headers_redefine, + url, opts) test:test("cancel and errinj", test_cancel_and_errinj, url .. 'long_query', opts) test:test("basic http post/get", test_post_and_get, url, opts) test:test("errors", test_errors) -- 2.20.1