summaryrefslogtreecommitdiff
path: root/src/convert_json.cpp
diff options
context:
space:
mode:
authorsapier <Sapier at GMX dot net>2013-11-09 22:49:27 +0100
committerKahrl <kahrl@gmx.net>2013-12-13 18:05:46 +0100
commit0d990bd189881a9a6c829e1c9ec17b2dc5ac12c8 (patch)
tree90474ffb3ca64934aaefaaba92d85f6ff56b9574 /src/convert_json.cpp
parentb03135548bbd9bcb73d14b067b96ec913404dd5f (diff)
downloadminetest-0d990bd189881a9a6c829e1c9ec17b2dc5ac12c8.tar.gz
minetest-0d990bd189881a9a6c829e1c9ec17b2dc5ac12c8.tar.bz2
minetest-0d990bd189881a9a6c829e1c9ec17b2dc5ac12c8.zip
Replace any direct curl usage by httpfetch
Diffstat (limited to 'src/convert_json.cpp')
-rw-r--r--src/convert_json.cpp56
1 files changed, 18 insertions, 38 deletions
diff --git a/src/convert_json.cpp b/src/convert_json.cpp
index 0b69f43a3..dbf0a82ac 100644
--- a/src/convert_json.cpp
+++ b/src/convert_json.cpp
@@ -28,58 +28,38 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "main.h" // for g_settings
#include "settings.h"
#include "version.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
+#include "httpfetch.h"
Json::Value fetchJsonValue(const std::string url,
struct curl_slist *chunk) {
#if USE_CURL
- std::string liststring;
- CURL *curl;
- curl = curl_easy_init();
- if (curl)
- {
- CURLcode res;
-
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
- curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
- 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);
+ HTTPFetchRequest fetchrequest;
+ HTTPFetchResult fetchresult;
+ fetchrequest.url = url;
+ fetchrequest.useragent = std::string("Minetest ")+minetest_version_hash;
+ fetchrequest.timeout = g_settings->getS32("curl_timeout");
+ fetchrequest.caller = HTTPFETCH_SYNC;
+
+ struct curl_slist* runptr = chunk;
+ while(runptr) {
+ fetchrequest.extra_headers.push_back(runptr->data);
+ runptr = runptr->next;
}
+ httpfetch_sync(fetchrequest,fetchresult);
- Json::Value root;
- Json::Reader reader;
- std::istringstream stream(liststring);
- if (!liststring.size()) {
+ if (!fetchresult.succeeded) {
return Json::Value();
}
+ Json::Value root;
+ Json::Reader reader;
+ std::istringstream stream(fetchresult.data);
if (!reader.parse( stream, root ) )
{
errorstream << "URL: " << url << std::endl;
errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
- errorstream << "data: \"" << liststring << "\"" << std::endl;
+ errorstream << "data: \"" << fetchresult.data << "\"" << std::endl;
return Json::Value();
}