2013-06-23 18:30:21 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "convert_json.h"
|
2018-04-17 15:54:50 +02:00
|
|
|
#include "content/mods.h"
|
2013-06-23 18:30:21 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "log.h"
|
2013-07-15 23:36:44 +02:00
|
|
|
#include "settings.h"
|
2013-11-09 22:49:27 +01:00
|
|
|
#include "httpfetch.h"
|
2014-02-26 14:21:38 +01:00
|
|
|
#include "porting.h"
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2014-09-15 02:46:45 +02:00
|
|
|
Json::Value fetchJsonValue(const std::string &url,
|
|
|
|
std::vector<std::string> *extra_headers)
|
|
|
|
{
|
|
|
|
HTTPFetchRequest fetch_request;
|
|
|
|
HTTPFetchResult fetch_result;
|
|
|
|
fetch_request.url = url;
|
|
|
|
fetch_request.caller = HTTPFETCH_SYNC;
|
2013-11-09 22:49:27 +01:00
|
|
|
|
2014-06-19 20:58:22 +02:00
|
|
|
if (extra_headers != NULL)
|
2014-09-15 02:46:45 +02:00
|
|
|
fetch_request.extra_headers = *extra_headers;
|
2014-06-19 20:58:22 +02:00
|
|
|
|
2014-09-15 02:46:45 +02:00
|
|
|
httpfetch_sync(fetch_request, fetch_result);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2014-09-15 02:46:45 +02:00
|
|
|
if (!fetch_result.succeeded) {
|
2013-06-23 18:30:21 +02:00
|
|
|
return Json::Value();
|
|
|
|
}
|
2013-11-09 22:49:27 +01:00
|
|
|
Json::Value root;
|
2014-09-15 02:46:45 +02:00
|
|
|
std::istringstream stream(fetch_result.data);
|
2013-06-23 18:30:21 +02:00
|
|
|
|
2017-09-26 20:30:14 +02:00
|
|
|
Json::CharReaderBuilder builder;
|
|
|
|
builder.settings_["collectComments"] = false;
|
|
|
|
std::string errs;
|
|
|
|
|
|
|
|
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
|
2013-06-23 18:30:21 +02:00
|
|
|
errorstream << "URL: " << url << std::endl;
|
2017-09-26 20:30:14 +02:00
|
|
|
errorstream << "Failed to parse json data " << errs << std::endl;
|
2016-01-28 23:53:58 +01:00
|
|
|
if (fetch_result.data.size() > 100) {
|
|
|
|
errorstream << "Data (" << fetch_result.data.size()
|
|
|
|
<< " bytes) printed to warningstream." << std::endl;
|
|
|
|
warningstream << "data: \"" << fetch_result.data << "\"" << std::endl;
|
|
|
|
} else {
|
|
|
|
errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
|
|
|
|
}
|
2013-06-23 18:30:21 +02:00
|
|
|
return Json::Value();
|
|
|
|
}
|
|
|
|
|
2014-09-15 02:46:45 +02:00
|
|
|
return root;
|
2013-06-23 18:30:21 +02:00
|
|
|
}
|
2017-09-26 20:30:14 +02:00
|
|
|
|
|
|
|
std::string fastWriteJson(const Json::Value &value)
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
Json::StreamWriterBuilder builder;
|
|
|
|
builder["indentation"] = "";
|
|
|
|
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
|
|
|
writer->write(value, &oss);
|
|
|
|
return oss.str();
|
|
|
|
}
|