Skip to content

Commit

Permalink
get/set http headers (qicosmos#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Sep 25, 2023
1 parent 2e4288f commit 905eee8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 12 additions & 4 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> 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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 905eee8

Please sign in to comment.