]> git.lizzy.rs Git - minetest.git/blobdiff - src/httpfetch.cpp
Copy zlib and freetype dll to windows package too
[minetest.git] / src / httpfetch.cpp
index 64e77bc543a11e59679b1aa557c3647519859992..69c366ee00734f0a529ab7bfb32983f28075259c 100644 (file)
@@ -17,6 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include "socket.h" // for select()
+#include "porting.h" // for sleep_ms(), get_sysinfo()
 #include "httpfetch.h"
 #include <iostream>
 #include <sstream>
@@ -30,11 +32,25 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "util/container.h"
 #include "util/thread.h"
-#include "socket.h" // for select()
+#include "version.h"
+#include "main.h"
+#include "settings.h"
 
 JMutex g_httpfetch_mutex;
 std::map<unsigned long, std::list<HTTPFetchResult> > g_httpfetch_results;
 
+HTTPFetchRequest::HTTPFetchRequest()
+{
+       url = "";
+       caller = HTTPFETCH_DISCARD;
+       request_id = 0;
+       timeout = g_settings->getS32("curl_timeout");
+       connect_timeout = timeout;
+
+       useragent = std::string("Minetest/") + minetest_version_hash + " (" + porting::get_sysinfo() + ")";
+}
+
+
 static void httpfetch_deliver_result(const HTTPFetchResult &fetchresult)
 {
        unsigned long caller = fetchresult.caller;
@@ -243,6 +259,10 @@ struct HTTPFetchOngoing
                                        request.extra_headers[i].c_str());
                        }
                        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, httpheader);
+
+                       if (!g_settings->getBool("curl_verify_cert")) {
+                               curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
+                       }
                }
        }
 
@@ -286,7 +306,7 @@ struct HTTPFetchOngoing
                }
 
                if (res != CURLE_OK) {
-                       infostream<<request.url<<" not found ("
+                       errorstream<<request.url<<" not found ("
                                <<curl_easy_strerror(res)<<")"
                                <<" (response code "<<result.response_code<<")"
                                <<std::endl;
@@ -319,7 +339,7 @@ struct HTTPFetchOngoing
        }
 };
 
-class CurlFetchThread : public SimpleThread
+class CurlFetchThread : public JThread
 {
 protected:
        enum RequestType {
@@ -520,19 +540,26 @@ class CurlFetchThread : public SimpleThread
                        select_timeout = timeout;
 
                if (select_timeout > 0) {
-                       select_tv.tv_sec = select_timeout / 1000;
-                       select_tv.tv_usec = (select_timeout % 1000) * 1000;
-                       int retval = select(max_fd + 1, &read_fd_set,
-                                       &write_fd_set, &exc_fd_set,
-                                       &select_tv);
-                       if (retval == -1) {
-                               #ifdef _WIN32
-                               errorstream<<"select returned error code "
-                                       <<WSAGetLastError()<<std::endl;
-                               #else
-                               errorstream<<"select returned error code "
-                                       <<errno<<std::endl;
-                               #endif
+                       // in Winsock it is forbidden to pass three empty
+                       // fd_sets to select(), so in that case use sleep_ms
+                       if (max_fd != -1) {
+                               select_tv.tv_sec = select_timeout / 1000;
+                               select_tv.tv_usec = (select_timeout % 1000) * 1000;
+                               int retval = select(max_fd + 1, &read_fd_set,
+                                               &write_fd_set, &exc_fd_set,
+                                               &select_tv);
+                               if (retval == -1) {
+                                       #ifdef _WIN32
+                                       errorstream<<"select returned error code "
+                                               <<WSAGetLastError()<<std::endl;
+                                       #else
+                                       errorstream<<"select returned error code "
+                                               <<errno<<std::endl;
+                                       #endif
+                               }
+                       }
+                       else {
+                               sleep_ms(select_timeout);
                        }
                }
        }
@@ -543,6 +570,8 @@ class CurlFetchThread : public SimpleThread
                log_register_thread("CurlFetchThread");
                DSTACK(__FUNCTION_NAME);
 
+               porting::setThreadName("CurlFetchThread");
+
                CurlHandlePool pool;
 
                m_multi = curl_multi_init();
@@ -553,7 +582,7 @@ class CurlFetchThread : public SimpleThread
 
                assert(m_all_ongoing.empty());
 
-               while (getRun()) {
+               while (!StopRequested()) {
                        BEGIN_DEBUG_EXCEPTION_HANDLER
 
                        /*
@@ -561,7 +590,7 @@ class CurlFetchThread : public SimpleThread
                        */
 
                        while (!m_requests.empty()) {
-                               Request req = m_requests.pop_front();
+                               Request req = m_requests.pop_frontNoEx();
                                processRequest(req);
                        }
                        processQueued(&pool);
@@ -641,9 +670,9 @@ void httpfetch_cleanup()
 {
        verbosestream<<"httpfetch_cleanup: cleaning up"<<std::endl;
 
-       g_httpfetch_thread->setRun(false);
+       g_httpfetch_thread->Stop();
        g_httpfetch_thread->requestWakeUp();
-       g_httpfetch_thread->stop();
+       g_httpfetch_thread->Wait();
        delete g_httpfetch_thread;
 
        curl_global_cleanup();