Browse Source

refactoring curl http headers

dmuratshin 9 years ago
parent
commit
0bc7be2ad1

+ 4 - 8
oxygine/src/HttpRequestTask.cpp

@@ -27,9 +27,6 @@ namespace oxygine
     HttpRequestTask::~HttpRequestTask()
     HttpRequestTask::~HttpRequestTask()
     {
     {
         log::messageln("~HttpRequestTask");
         log::messageln("~HttpRequestTask");
-        if(!_headers.empty()) {
-            _headers.clear();
-        }
     }
     }
 
 
     void HttpRequestTask::setCustomRequests(createHttpRequestCallback cb)
     void HttpRequestTask::setCustomRequests(createHttpRequestCallback cb)
@@ -63,11 +60,10 @@ namespace oxygine
 
 
     void HttpRequestTask::addHeader(const std::string& key, const std::string& value)
     void HttpRequestTask::addHeader(const std::string& key, const std::string& value)
     {
     {
-        OX_ASSERT(!"Implmenting");
-        if(!key.empty()) {            
-            _headers.push_back(std::make_pair(key,value));          
-            _addHeader(key, value);            
-        }
+        OX_ASSERT(!key.empty());
+
+        _headers.push_back(std::make_pair(key, value));
+        _addHeader(key, value);
     }
     }
 
 
     const std::vector<unsigned char>&   HttpRequestTask::getPostData() const
     const std::vector<unsigned char>&   HttpRequestTask::getPostData() const

+ 4 - 2
oxygine/src/HttpRequestTask.h

@@ -16,7 +16,7 @@ namespace oxygine
         static void init();
         static void init();
         static void release();
         static void release();
 
 
-        typedef std::vector<std::pair<std::string,std::string>>  headers_pair;
+
         /**dispatching AsyncTask events*/
         /**dispatching AsyncTask events*/
         enum
         enum
         {
         {
@@ -76,6 +76,8 @@ namespace oxygine
         bool _cacheEnabled;
         bool _cacheEnabled;
         std::vector<unsigned char> _response;
         std::vector<unsigned char> _response;
         std::vector<unsigned char> _postData;
         std::vector<unsigned char> _postData;
-        headers_pair _headers;
+
+        typedef std::vector< std::pair<std::string, std::string> >  headers;
+        headers _headers;
     };
     };
 }
 }

+ 10 - 31
oxygine/src/core/curl/HttpRequestCurlTask.cpp

@@ -197,7 +197,7 @@ namespace oxygine
         return size;
         return size;
     }
     }
 
 
-    HttpRequestTaskCURL::HttpRequestTaskCURL() : _easy(0), _handle(0), _httpHeaders(NULL)
+    HttpRequestTaskCURL::HttpRequestTaskCURL() : _easy(0), _handle(0), _httpHeaders(0)
     {
     {
         _easy = curl_easy_init();
         _easy = curl_easy_init();
     }
     }
@@ -211,12 +211,12 @@ namespace oxygine
         if (_easy)
         if (_easy)
             curl_easy_cleanup(_easy);
             curl_easy_cleanup(_easy);
         _easy = 0;
         _easy = 0;
-    }
 
 
-    void _addHeader(const std::string& key, const std::string& value) {        
-       _httpHeaders = curl_slist_append(_httpHeaders, key + ": " + value);
+        if (_httpHeaders)
+            curl_slist_free_all(_httpHeaders);
     }
     }
 
 
+
     void HttpRequestTaskCURL::_run()
     void HttpRequestTaskCURL::_run()
     {
     {
         curl_easy_setopt(_easy, CURLOPT_URL, _url.c_str());
         curl_easy_setopt(_easy, CURLOPT_URL, _url.c_str());
@@ -228,38 +228,17 @@ namespace oxygine
         curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, true);
         curl_easy_setopt(_easy, CURLOPT_FOLLOWLOCATION, true);
         curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0);
         curl_easy_setopt(_easy, CURLOPT_NOPROGRESS, 0);
 
 
-        //curl_slist *header = curl_slist_append(0, "hello");
-        //curl_easy_setopt(_easy, CURLOPT_HEADER, header);
-        if(!_httpHeaders) {
-            httpHeaders = curl_slist_append(_httpHeaders, "Content-Type: text/plain");
-        } 
-
-        curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, _httpHeaders);        
-
-        if (!_postData.empty()){
-            // curl_slist* headers = NULL; // init to NULL is important
-            //headers = curl_slist_append(headers, "Accept:")            
-
-            
 
 
-            //curl_easy_setopt(_easy, CURLOPT_PORT, 4002);
-
-
-            //curl_easy_setopt(_easy, CURLOPT_VERBOSE, 1);
-            //curl_easy_setopt(_easy, CURLOPT_POST, 1);
-            //_sendPostData.push_back(0);
-            //char *p = curl_escape(&_postData.front(), _postData.size());
-            //log::messageln("data: %s", p);
-            //log::messageln("data: %s", &_sendPostData.front());
-            //curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, p);
-            //curl_easy_setopt(_easy, CURLOPT_TIMEOUT, 9999);
+        if (!_postData.empty())
+        {
             curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, &_postData.front());
             curl_easy_setopt(_easy, CURLOPT_POSTFIELDS, &_postData.front());
             curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, _postData.size());
             curl_easy_setopt(_easy, CURLOPT_POSTFIELDSIZE, _postData.size());
-
-            //curl_easy_setopt(_easy, CURLOPT_TCP_KEEPALIVE, 1);
         }
         }
 
 
-        curl_easy_setopt(_easy, CURLOPT_HEADER, 0);
+        for (size_t i = 0; i < _headers.size(); ++i)
+            _httpHeaders = curl_slist_append(_httpHeaders, (_headers[i].first + ": " + _headers[i].second).c_str());
+
+        curl_easy_setopt(_easy, CURLOPT_HTTPHEADER, _httpHeaders);
 
 
         addRef();
         addRef();
         _messages.post(0, _easy, 0);
         _messages.post(0, _easy, 0);