# HG changeset patch # User Sebastien Jodogne # Date 1461597787 -7200 # Node ID 99b2498670520b8e40b594e521370387884f0bbd # Parent 3fcea6dc151d0d4aa95906292120884c02cefaeb HTTP headers in HttpClient diff -r 3fcea6dc151d -r 99b249867052 Core/HttpClient.cpp --- a/Core/HttpClient.cpp Mon Apr 25 14:41:53 2016 +0200 +++ b/Core/HttpClient.cpp Mon Apr 25 17:23:07 2016 +0200 @@ -80,7 +80,8 @@ struct HttpClient::PImpl { CURL* curl_; - struct curl_slist *postHeaders_; + struct curl_slist *defaultPostHeaders_; + struct curl_slist *userHeaders_; }; @@ -135,8 +136,9 @@ void HttpClient::Setup() { - pimpl_->postHeaders_ = NULL; - if ((pimpl_->postHeaders_ = curl_slist_append(pimpl_->postHeaders_, "Expect:")) == NULL) + pimpl_->userHeaders_ = NULL; + pimpl_->defaultPostHeaders_ = NULL; + if ((pimpl_->defaultPostHeaders_ = curl_slist_append(pimpl_->defaultPostHeaders_, "Expect:")) == NULL) { throw OrthancException(ErrorCode_NotEnoughMemory); } @@ -144,7 +146,7 @@ pimpl_->curl_ = curl_easy_init(); if (!pimpl_->curl_) { - curl_slist_free_all(pimpl_->postHeaders_); + curl_slist_free_all(pimpl_->defaultPostHeaders_); throw OrthancException(ErrorCode_NotEnoughMemory); } @@ -191,7 +193,8 @@ HttpClient::~HttpClient() { curl_easy_cleanup(pimpl_->curl_); - curl_slist_free_all(pimpl_->postHeaders_); + curl_slist_free_all(pimpl_->defaultPostHeaders_); + ClearHeaders(); } @@ -210,6 +213,33 @@ } + void HttpClient::AddHeader(const std::string& key, + const std::string& value) + { + if (key.empty()) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + std::string s = key + ": " + value; + + if ((pimpl_->userHeaders_ = curl_slist_append(pimpl_->userHeaders_, s.c_str())) == NULL) + { + throw OrthancException(ErrorCode_NotEnoughMemory); + } + } + + + void HttpClient::ClearHeaders() + { + if (pimpl_->userHeaders_ != NULL) + { + curl_slist_free_all(pimpl_->userHeaders_); + pimpl_->userHeaders_ = NULL; + } + } + + bool HttpClient::Apply(std::string& answer) { answer.clear(); @@ -232,7 +262,7 @@ #endif // Reset the parameters from previous calls to Apply() - CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, NULL)); + CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->userHeaders_)); CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPGET, 0L)); CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 0L)); CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_NOBODY, 0L)); @@ -271,7 +301,12 @@ case HttpMethod_Post: CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_POST, 1L)); - CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); + + if (pimpl_->userHeaders_ == NULL) + { + CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->defaultPostHeaders_)); + } + break; case HttpMethod_Delete: @@ -286,7 +321,12 @@ // CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_PUT, 1L)); curl_easy_setopt(pimpl_->curl_, CURLOPT_CUSTOMREQUEST, "PUT"); /* !!! */ - CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->postHeaders_)); + + if (pimpl_->userHeaders_ == NULL) + { + CheckCode(curl_easy_setopt(pimpl_->curl_, CURLOPT_HTTPHEADER, pimpl_->defaultPostHeaders_)); + } + break; default: diff -r 3fcea6dc151d -r 99b249867052 Core/HttpClient.h --- a/Core/HttpClient.h Mon Apr 25 14:41:53 2016 +0200 +++ b/Core/HttpClient.h Mon Apr 25 17:23:07 2016 +0200 @@ -125,6 +125,11 @@ return isVerbose_; } + void AddHeader(const std::string& key, + const std::string& value); + + void ClearHeaders(); + bool Apply(std::string& answer); bool Apply(Json::Value& answer);