From ffed5dee2db194c3b13c525e6f0329084eb42feb Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Wed, 29 Jun 2022 17:28:39 +0200 Subject: [PATCH] Added connection and transfer timeout settings to the scraper (not configurable via the GUI). --- es-core/src/HttpReq.cpp | 37 ++++++++++++++++++++++++++++++++++--- es-core/src/Settings.cpp | 2 ++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/es-core/src/HttpReq.cpp b/es-core/src/HttpReq.cpp index 33776f20b..a991301f9 100644 --- a/es-core/src/HttpReq.cpp +++ b/es-core/src/HttpReq.cpp @@ -11,6 +11,7 @@ #include "HttpReq.h" #include "Log.h" +#include "Settings.h" #include "resources/ResourceManager.h" #include "utils/FileSystemUtil.h" @@ -52,7 +53,7 @@ HttpReq::HttpReq(const std::string& url) , mHandle(nullptr) { // The multi-handle is cleaned up via a call from GuiScraperSearch after the scraping - // has been completed for a game, meaning the handle is valid for all cURL requests + // has been completed for a game, meaning the handle is valid for all curl requests // performed for the current game. if (!s_multi_handle) s_multi_handle = curl_multi_init(); @@ -60,13 +61,13 @@ HttpReq::HttpReq(const std::string& url) mHandle = curl_easy_init(); #if defined(USE_BUNDLED_CERTIFICATES) - // Use the bundled cURL TLS/SSL certificates (which actually come from the Mozilla project). + // Use the bundled curl TLS/SSL certificates (which actually come from the Mozilla project). // This is enabled by default on Windows. Although there is a possibility to use the OS // provided Schannel certificates I haven't been able to get this to work, and it also seems // to be problematic on older Windows versions. // The bundled certificates are also required on Linux when building an AppImage package as // distributions such as Debian, Ubuntu, Linux Mint and Manjaro place the TLS certificates in - // a different directory than for example Fedora and openSUSE. This makes cURL unusable on + // a different directory than for example Fedora and openSUSE. This makes curl unusable on // these latter operating systems unless the bundled file is used. curl_easy_setopt(mHandle, CURLOPT_CAINFO, ResourceManager::getInstance() @@ -88,6 +89,36 @@ HttpReq::HttpReq(const std::string& url) return; } + long connectionTimeout { + static_cast(Settings::getInstance()->getInt("ScraperConnectionTimeout"))}; + + if (connectionTimeout < 0 || connectionTimeout > 300) + connectionTimeout = + static_cast(Settings::getInstance()->getDefaultInt("ScraperConnectionTimeout")); + + // Set connection timeout (default is 300 seconds). + err = curl_easy_setopt(mHandle, CURLOPT_CONNECTTIMEOUT, connectionTimeout); + if (err != CURLE_OK) { + mStatus = REQ_IO_ERROR; + onError(curl_easy_strerror(err)); + return; + } + + long transferTimeout { + static_cast(Settings::getInstance()->getInt("ScraperTransferTimeout"))}; + + if (transferTimeout < 0 || transferTimeout > 300) + transferTimeout = + static_cast(Settings::getInstance()->getDefaultInt("ScraperTransferTimeout")); + + // Set transfer timeout (default is 0/infinity). + err = curl_easy_setopt(mHandle, CURLOPT_TIMEOUT, transferTimeout); + if (err != CURLE_OK) { + mStatus = REQ_IO_ERROR; + onError(curl_easy_strerror(err)); + return; + } + // Set curl to handle redirects. err = curl_easy_setopt(mHandle, CURLOPT_FOLLOWLOCATION, 1L); if (err != CURLE_OK) { diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp index 5e1a18139..2c51c7287 100644 --- a/es-core/src/Settings.cpp +++ b/es-core/src/Settings.cpp @@ -285,6 +285,8 @@ void Settings::setDefaults() mStringMap["UIMode_passkey"] = {"uuddlrlrba", "uuddlrlrba"}; mIntMap["LottieMaxFileCache"] = {150, 150}; mIntMap["LottieMaxTotalCache"] = {1024, 1024}; + mIntMap["ScraperConnectionTimeout"] = {60, 60}; + mIntMap["ScraperTransferTimeout"] = {120, 120}; // // Hardcoded or program-internal settings.