mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
Replace any direct curl usage by httpfetch
This commit is contained in:
parent
b03135548b
commit
0d990bd189
@ -198,7 +198,6 @@
|
|||||||
# - Media fetch if server uses remote_media setting
|
# - Media fetch if server uses remote_media setting
|
||||||
# - Serverlist download and server announcement
|
# - Serverlist download and server announcement
|
||||||
# - Downloads performed by main menu (e.g. mod manager)
|
# - Downloads performed by main menu (e.g. mod manager)
|
||||||
# - Downloads performed by mods (minetest.httpfetch)
|
|
||||||
# Only has an effect if compiled with cURL
|
# Only has an effect if compiled with cURL
|
||||||
#curl_parallel_limit = 8
|
#curl_parallel_limit = 8
|
||||||
|
|
||||||
|
@ -28,58 +28,38 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "main.h" // for g_settings
|
#include "main.h" // for g_settings
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
#include "httpfetch.h"
|
||||||
#if USE_CURL
|
|
||||||
#include <curl/curl.h>
|
|
||||||
|
|
||||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
||||||
{
|
|
||||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
|
||||||
return size * nmemb;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Json::Value fetchJsonValue(const std::string url,
|
Json::Value fetchJsonValue(const std::string url,
|
||||||
struct curl_slist *chunk) {
|
struct curl_slist *chunk) {
|
||||||
#if USE_CURL
|
#if USE_CURL
|
||||||
std::string liststring;
|
|
||||||
CURL *curl;
|
|
||||||
|
|
||||||
curl = curl_easy_init();
|
HTTPFetchRequest fetchrequest;
|
||||||
if (curl)
|
HTTPFetchResult fetchresult;
|
||||||
{
|
fetchrequest.url = url;
|
||||||
CURLcode res;
|
fetchrequest.useragent = std::string("Minetest ")+minetest_version_hash;
|
||||||
|
fetchrequest.timeout = g_settings->getS32("curl_timeout");
|
||||||
|
fetchrequest.caller = HTTPFETCH_SYNC;
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
struct curl_slist* runptr = chunk;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
while(runptr) {
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
fetchrequest.extra_headers.push_back(runptr->data);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
|
runptr = runptr->next;
|
||||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, g_settings->getS32("curl_timeout"));
|
|
||||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, g_settings->getS32("curl_timeout"));
|
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, (std::string("Minetest ")+minetest_version_hash).c_str());
|
|
||||||
|
|
||||||
if (chunk != 0)
|
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
|
||||||
|
|
||||||
res = curl_easy_perform(curl);
|
|
||||||
if (res != CURLE_OK)
|
|
||||||
errorstream<<"Jsonreader: "<< url <<" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
}
|
||||||
|
httpfetch_sync(fetchrequest,fetchresult);
|
||||||
|
|
||||||
Json::Value root;
|
if (!fetchresult.succeeded) {
|
||||||
Json::Reader reader;
|
|
||||||
std::istringstream stream(liststring);
|
|
||||||
if (!liststring.size()) {
|
|
||||||
return Json::Value();
|
return Json::Value();
|
||||||
}
|
}
|
||||||
|
Json::Value root;
|
||||||
|
Json::Reader reader;
|
||||||
|
std::istringstream stream(fetchresult.data);
|
||||||
|
|
||||||
if (!reader.parse( stream, root ) )
|
if (!reader.parse( stream, root ) )
|
||||||
{
|
{
|
||||||
errorstream << "URL: " << url << std::endl;
|
errorstream << "URL: " << url << std::endl;
|
||||||
errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
|
errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
|
||||||
errorstream << "data: \"" << liststring << "\"" << std::endl;
|
errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
|
||||||
return Json::Value();
|
return Json::Value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "sound.h"
|
#include "sound.h"
|
||||||
#include "sound_openal.h"
|
#include "sound_openal.h"
|
||||||
#include "clouds.h"
|
#include "clouds.h"
|
||||||
|
#include "httpfetch.h"
|
||||||
|
|
||||||
#include <IGUIStaticText.h>
|
#include <IGUIStaticText.h>
|
||||||
#include <ICameraSceneNode.h>
|
#include <ICameraSceneNode.h>
|
||||||
@ -507,38 +508,27 @@ bool GUIEngine::setTexture(texture_layer layer,std::string texturepath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
#if USE_CURL
|
|
||||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
||||||
{
|
|
||||||
FILE* targetfile = (FILE*) userp;
|
|
||||||
fwrite(contents,size,nmemb,targetfile);
|
|
||||||
return size * nmemb;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
bool GUIEngine::downloadFile(std::string url,std::string target) {
|
bool GUIEngine::downloadFile(std::string url,std::string target) {
|
||||||
#if USE_CURL
|
#if USE_CURL
|
||||||
//download file via curl
|
|
||||||
CURL *curl;
|
|
||||||
|
|
||||||
curl = curl_easy_init();
|
|
||||||
|
|
||||||
if (curl)
|
|
||||||
{
|
|
||||||
CURLcode res;
|
|
||||||
bool retval = true;
|
bool retval = true;
|
||||||
|
|
||||||
FILE* targetfile = fopen(target.c_str(),"wb");
|
FILE* targetfile = fopen(target.c_str(),"wb");
|
||||||
|
|
||||||
if (targetfile) {
|
if (targetfile) {
|
||||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
HTTPFetchRequest fetchrequest;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
HTTPFetchResult fetchresult;
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
fetchrequest.url = url;
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, targetfile);
|
fetchrequest.useragent = std::string("Minetest ")+minetest_version_hash;
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, (std::string("Minetest ")+minetest_version_hash).c_str());
|
fetchrequest.timeout = g_settings->getS32("curl_timeout");
|
||||||
res = curl_easy_perform(curl);
|
fetchrequest.caller = HTTPFETCH_SYNC;
|
||||||
if (res != CURLE_OK) {
|
httpfetch_sync(fetchrequest,fetchresult);
|
||||||
errorstream << "File at url \"" << url
|
|
||||||
<<"\" not found (" << curl_easy_strerror(res) << ")" <<std::endl;
|
if (fetchresult.succeeded) {
|
||||||
|
if (fwrite(fetchresult.data.c_str(),1,fetchresult.data.size(),targetfile) != fetchresult.data.size()) {
|
||||||
|
retval = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
retval = false;
|
retval = false;
|
||||||
}
|
}
|
||||||
fclose(targetfile);
|
fclose(targetfile);
|
||||||
@ -547,11 +537,10 @@ bool GUIEngine::downloadFile(std::string url,std::string target) {
|
|||||||
retval = false;
|
retval = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
#else
|
||||||
#endif
|
|
||||||
return false;
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -40,6 +40,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "tile.h" // ITextureSource
|
#include "tile.h" // ITextureSource
|
||||||
#include "hud.h" // drawItemStack
|
#include "hud.h" // drawItemStack
|
||||||
|
#include "hex.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
#include "util/numeric.h"
|
#include "util/numeric.h"
|
||||||
#include "filesys.h"
|
#include "filesys.h"
|
||||||
@ -2736,19 +2737,6 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
|
|||||||
return Parent ? Parent->OnEvent(event) : false;
|
return Parent ? Parent->OnEvent(event) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool hex_digit_decode(char hexdigit, unsigned char &value)
|
|
||||||
{
|
|
||||||
if(hexdigit >= '0' && hexdigit <= '9')
|
|
||||||
value = hexdigit - '0';
|
|
||||||
else if(hexdigit >= 'A' && hexdigit <= 'F')
|
|
||||||
value = hexdigit - 'A' + 10;
|
|
||||||
else if(hexdigit >= 'a' && hexdigit <= 'f')
|
|
||||||
value = hexdigit - 'a' + 10;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GUIFormSpecMenu::parseColor(std::string &value, video::SColor &color, bool quiet)
|
bool GUIFormSpecMenu::parseColor(std::string &value, video::SColor &color, bool quiet)
|
||||||
{
|
{
|
||||||
const char *hexpattern = NULL;
|
const char *hexpattern = NULL;
|
||||||
|
@ -280,6 +280,7 @@ struct HTTPFetchOngoing
|
|||||||
if (curl != NULL) {
|
if (curl != NULL) {
|
||||||
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
|
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
|
||||||
&result.response_code) != CURLE_OK) {
|
&result.response_code) != CURLE_OK) {
|
||||||
|
//we failed to get a return code make sure it is still 0
|
||||||
result.response_code = 0;
|
result.response_code = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
// Can be used in place of "caller" in asynchronous transfers to discard result
|
// Can be used in place of "caller" in asynchronous transfers to discard result
|
||||||
// (used as default value of "caller")
|
// (used as default value of "caller")
|
||||||
#define HTTPFETCH_DISCARD 0
|
#define HTTPFETCH_DISCARD 0
|
||||||
|
#define HTTPFETCH_SYNC 1
|
||||||
|
|
||||||
struct HTTPFetchRequest
|
struct HTTPFetchRequest
|
||||||
{
|
{
|
||||||
@ -96,6 +97,7 @@ struct HTTPFetchResult
|
|||||||
caller = fetchrequest.caller;
|
caller = fetchrequest.caller;
|
||||||
request_id = fetchrequest.request_id;
|
request_id = fetchrequest.request_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initializes the httpfetch module
|
// Initializes the httpfetch module
|
||||||
|
Loading…
Reference in New Issue
Block a user