Remove remaining modstore code (#6120)
[oweals/minetest.git] / src / convert_json.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <vector>
21 #include <iostream>
22 #include <sstream>
23
24 #include "convert_json.h"
25 #include "mods.h"
26 #include "config.h"
27 #include "log.h"
28 #include "settings.h"
29 #include "httpfetch.h"
30 #include "porting.h"
31
32 Json::Value fetchJsonValue(const std::string &url,
33                 std::vector<std::string> *extra_headers)
34 {
35         HTTPFetchRequest fetch_request;
36         HTTPFetchResult fetch_result;
37         fetch_request.url = url;
38         fetch_request.caller = HTTPFETCH_SYNC;
39
40         if (extra_headers != NULL)
41                 fetch_request.extra_headers = *extra_headers;
42
43         httpfetch_sync(fetch_request, fetch_result);
44
45         if (!fetch_result.succeeded) {
46                 return Json::Value();
47         }
48         Json::Value root;
49         Json::Reader reader;
50         std::istringstream stream(fetch_result.data);
51
52         if (!reader.parse(stream, root)) {
53                 errorstream << "URL: " << url << std::endl;
54                 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
55                 if (fetch_result.data.size() > 100) {
56                         errorstream << "Data (" << fetch_result.data.size()
57                                 << " bytes) printed to warningstream." << std::endl;
58                         warningstream << "data: \"" << fetch_result.data << "\"" << std::endl;
59                 } else {
60                         errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
61                 }
62                 return Json::Value();
63         }
64
65         return root;
66 }