changeset 1986:99b249867052

HTTP headers in HttpClient
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 25 Apr 2016 17:23:07 +0200
parents 3fcea6dc151d
children ce90d109bb64
files Core/HttpClient.cpp Core/HttpClient.h
diffstat 2 files changed, 53 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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:
--- 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);