Skip to content

Commit

Permalink
add set get http headers (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Sep 26, 2023
1 parent 8620086 commit 2c47a66
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions include/ylt/thirdparty/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 @@ -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));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2c47a66

Please sign in to comment.