]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/httpfetch.cpp
Merge branch 'master' of https://github.com/EliasFleckenstein03/dragonfireclient
[dragonfireclient.git] / src / httpfetch.cpp
index 6a9a5e235a7860b8fe0d20bd48431550d2889808..65202ce3ebac5ff72c6d14777b2e94cdc0aee35b 100644 (file)
@@ -245,13 +245,18 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-       curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);
+       curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);
+       curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
 
        std::string bind_address = g_settings->get("bind_address");
        if (!bind_address.empty()) {
                curl_easy_setopt(curl, CURLOPT_INTERFACE, bind_address.c_str());
        }
 
+       if (!g_settings->getBool("enable_ipv6")) {
+               curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
+       }
+
 #if LIBCURL_VERSION_NUM >= 0x071304
        // Restrict protocols so that curl vulnerabilities in
        // other protocols don't affect us.
@@ -289,13 +294,11 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
        }
 
-       // Set POST (or GET) data
-       if (request.post_fields.empty() && request.post_data.empty()) {
-               curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
-       } else if (request.multipart) {
+       // Set data from fields or raw_data
+       if (request.multipart) {
                curl_httppost *last = NULL;
-               for (StringMap::iterator it = request.post_fields.begin();
-                               it != request.post_fields.end(); ++it) {
+               for (StringMap::iterator it = request.fields.begin();
+                               it != request.fields.end(); ++it) {
                        curl_formadd(&post, &last,
                                        CURLFORM_NAMELENGTH, it->first.size(),
                                        CURLFORM_PTRNAME, it->first.c_str(),
@@ -306,28 +309,42 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
                curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
                // request.post_fields must now *never* be
                // modified until CURLOPT_HTTPPOST is cleared
-       } else if (request.post_data.empty()) {
-               curl_easy_setopt(curl, CURLOPT_POST, 1);
-               std::string str;
-               for (auto &post_field : request.post_fields) {
-                       if (!str.empty())
-                               str += "&";
-                       str += urlencode(post_field.first);
-                       str += "=";
-                       str += urlencode(post_field.second);
-               }
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
-                               str.size());
-               curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS,
-                               str.c_str());
        } else {
-               curl_easy_setopt(curl, CURLOPT_POST, 1);
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
-                               request.post_data.size());
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
-                               request.post_data.c_str());
-               // request.post_data must now *never* be
-               // modified until CURLOPT_POSTFIELDS is cleared
+               switch (request.method) {
+               case HTTP_GET:
+                       curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
+                       break;
+               case HTTP_POST:
+                       curl_easy_setopt(curl, CURLOPT_POST, 1);
+                       break;
+               case HTTP_PUT:
+                       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
+                       break;
+               case HTTP_DELETE:
+                       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
+                       break;
+               }
+               if (request.method != HTTP_GET) {
+                       if (!request.raw_data.empty()) {
+                               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
+                                               request.raw_data.size());
+                               curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
+                                               request.raw_data.c_str());
+                       } else if (!request.fields.empty()) {
+                               std::string str;
+                               for (auto &field : request.fields) {
+                                       if (!str.empty())
+                                               str += "&";
+                                       str += urlencode(field.first);
+                                       str += "=";
+                                       str += urlencode(field.second);
+                               }
+                               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
+                                               str.size());
+                               curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS,
+                                               str.c_str());
+                       }
+               }
        }
        // Set additional HTTP headers
        for (const std::string &extra_header : request.extra_headers) {
@@ -642,8 +659,6 @@ class CurlFetchThread : public Thread
 
        void *run()
        {
-               DSTACK(FUNCTION_NAME);
-
                CurlHandlePool pool;
 
                m_multi = curl_multi_init();