Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_http_client][feat]add set get http headers #469

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading