blob: f0e10bd12a106a0074a5dc1bb8f645ea63b17add (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#include "servers_list.hpp"
#include "http_request.hpp"
#include "settings.hpp"
#include "platform.hpp"
#include "../base/logging.hpp"
#include "../base/assert.hpp"
#include "../3party/jansson/myjansson.hpp"
#define SETTINGS_SERVERS_KEY "LastBaseUrls"
namespace downloader
{
bool ParseServerList(string const & jsonStr, vector<string> & outUrls)
{
outUrls.clear();
try
{
my::Json root(jsonStr.c_str());
for (size_t i = 0; i < json_array_size(root); ++i)
{
char const * url = json_string_value(json_array_get(root, i));
if (url)
outUrls.push_back(url);
}
}
catch (std::exception const & e)
{
LOG(LERROR, ("Can't parse server list json", e.what(), jsonStr));
}
return !outUrls.empty();
}
void GetServerListFromRequest(HttpRequest const & request, vector<string> & urls)
{
if (request.Status() == HttpRequest::ECompleted &&
ParseServerList(request.Data(), urls))
{
Settings::Set(SETTINGS_SERVERS_KEY, request.Data());
}
else
{
LOG(LWARNING, ("Can't get servers list from request"));
string serverList;
if (!Settings::Get(SETTINGS_SERVERS_KEY, serverList))
serverList = GetPlatform().DefaultUrlsJSON();
VERIFY ( ParseServerList(serverList, urls), () );
}
}
}
|