Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
httpclient: Add ability to pass custom headers
Browse files Browse the repository at this point in the history
This allows custom users of libaktualizr to pass custom headers
with HTTP requests sent to the server.

Signed-off-by: Andy Doan <andy@foundries.io>
  • Loading branch information
doanac committed Jul 11, 2019
1 parent a4d36a9 commit 76f3b6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/libaktualizr/http/httpclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ static size_t writeString(void* contents, size_t size, size_t nmemb, void* userp
return size * nmemb;
}

HttpClient::HttpClient() : user_agent(std::string("Aktualizr/") + aktualizr_version()) {
HttpClient::HttpClient(const std::vector<std::string>* extra_headers)
: user_agent(std::string("Aktualizr/") + aktualizr_version()) {
curl = curl_easy_init();
if (curl == nullptr) {
throw std::runtime_error("Could not initialize curl");
Expand All @@ -65,6 +66,12 @@ HttpClient::HttpClient() : user_agent(std::string("Aktualizr/") + aktualizr_vers

headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Accept: */*");

if (extra_headers != nullptr) {
for (const auto& header : *extra_headers) {
headers = curl_slist_append(headers, header.c_str());
}
}
curlEasySetoptWrapper(curl, CURLOPT_HTTPHEADER, headers);
curlEasySetoptWrapper(curl, CURLOPT_USERAGENT, user_agent.c_str());
}
Expand Down
2 changes: 1 addition & 1 deletion src/libaktualizr/http/httpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CurlGlobalInitWrapper {

class HttpClient : public HttpInterface {
public:
HttpClient();
HttpClient(const std::vector<std::string> *extra_headers = nullptr);
HttpClient(const HttpClient & /*curl_in*/);
~HttpClient() override;
HttpResponse get(const std::string &url, int64_t maxsize) override;
Expand Down

0 comments on commit 76f3b6a

Please sign in to comment.