From 0ae26605c9751cb8c3adf98b179139d1831f05df Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 26 Sep 2023 15:23:18 +0800 Subject: [PATCH] add set get http headers --- .../thirdparty/cinatra/coro_http_client.hpp | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/include/ylt/thirdparty/cinatra/coro_http_client.hpp b/include/ylt/thirdparty/cinatra/coro_http_client.hpp index 393f54d07..cd643d0d9 100644 --- a/include/ylt/thirdparty/cinatra/coro_http_client.hpp +++ b/include/ylt/thirdparty/cinatra/coro_http_client.hpp @@ -280,13 +280,16 @@ class coro_http_client { bool has_closed() { return socket_->has_closed_; } + const auto &get_headers() { return req_headers_; } + + void set_headers(std::unordered_map req_headers) { + req_headers_ = std::move(req_headers); + } + bool add_header(std::string key, std::string val) { if (key.empty()) return false; - if (key == "Host") - return false; - req_headers_[key] = std::move(val); return true; @@ -760,7 +763,12 @@ class coro_http_client { } } - add_header("Transfer-Encoding", "chunked"); + if (headers.empty()) { + add_header("Transfer-Encoding", "chunked"); + } + else { + headers.emplace("Transfer-Encoding", "chunked"); + } std::string header_str = build_request_header(u, method, ctx, true, std::move(headers)); @@ -1074,7 +1082,12 @@ class coro_http_client { req_str.append(" HTTP/1.1\r\n"); } else { - req_str.append(" HTTP/1.1\r\nHost:").append(u.host).append("\r\n"); + if (req_headers_.find("Host") == req_headers_.end()) { + req_str.append(" HTTP/1.1\r\nHost:").append(u.host).append("\r\n"); + } + else { + req_str.append(" HTTP/1.1\r\n"); + } } auto type_str = get_content_type_str(ctx.content_type);