From 905eee8490da7f3ce2b2d3d4697cce70507ad221 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 25 Sep 2023 17:33:59 +0800 Subject: [PATCH] get/set http headers (#413) --- include/cinatra/coro_http_client.hpp | 16 ++++++++++++---- tests/test_cinatra.cpp | 1 - 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 393f54d0..a3e67fb4 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/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; @@ -1074,7 +1077,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); diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index e81fb47e..9252b689 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -450,7 +450,6 @@ TEST_CASE("test bad uri") { CHECK(client.add_header("hello", "cinatra")); CHECK(client.add_header("hello", "cinatra")); CHECK(!client.add_header("", "cinatra")); - CHECK(!client.add_header("Host", "cinatra")); client.add_str_part("hello", "world"); auto result = async_simple::coro::syncAwait( client.async_upload_multipart("http://www.badurlrandom.org"));