forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_update.cpp
156 lines (127 loc) · 3.41 KB
/
check_update.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Aseprite
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "updater/check_update.h"
#include "base/convert_to.h"
#include "base/debug.h"
#include "net/http_headers.h"
#include "net/http_request.h"
#include "net/http_response.h"
#include "tinyxml2.h"
#include "updater/user_agent.h"
#include "ver/info.h"
#include <iostream>
#include <memory>
#include <sstream>
namespace updater {
using namespace tinyxml2;
CheckUpdateResponse::CheckUpdateResponse()
: m_type(Unknown)
, m_waitDays(0.0)
{
}
CheckUpdateResponse::CheckUpdateResponse(const CheckUpdateResponse& other)
: m_type(other.m_type)
, m_version(other.m_version)
, m_url(other.m_url)
, m_waitDays(0.0)
{
}
CheckUpdateResponse::CheckUpdateResponse(const std::string& responseBody)
: m_type(Unknown)
, m_waitDays(0.0)
{
XMLDocument doc;
doc.Parse(responseBody.c_str());
XMLHandle handle(&doc);
XMLElement* xmlUpdate = handle.FirstChildElement("update").ToElement();
if (!xmlUpdate) {
// TODO show error?
return;
}
const char* latest_attr = xmlUpdate->Attribute("latest");
const char* version_attr = xmlUpdate->Attribute("version");
const char* type_attr = xmlUpdate->Attribute("type");
const char* url_attr = xmlUpdate->Attribute("url");
const char* uuid_attr = xmlUpdate->Attribute("uuid");
const char* waitdays_attr = xmlUpdate->Attribute("waitdays");
if (latest_attr && strcmp(latest_attr, "1") == 0)
m_type = NoUpdate;
if (type_attr) {
if (strcmp(type_attr, "critical") == 0)
m_type = Critical;
else if (strcmp(type_attr, "major") == 0)
m_type = Major;
}
if (version_attr)
m_version = version_attr;
if (url_attr)
m_url = url_attr;
if (uuid_attr)
m_uuid = uuid_attr;
if (waitdays_attr)
m_waitDays = base::convert_to<double>(std::string(waitdays_attr));
}
class CheckUpdate::CheckUpdateImpl
{
public:
CheckUpdateImpl() { }
~CheckUpdateImpl() { }
void abort()
{
if (m_request)
m_request->abort();
}
bool checkNewVersion(const Uuid& uuid, const std::string& extraParams, CheckUpdateDelegate* delegate)
{
std::string url = get_app_update_url();
if (!uuid.empty()) {
url += "&uuid=";
url += uuid;
}
if (!extraParams.empty()) {
url += "&";
url += extraParams;
}
m_request.reset(new net::HttpRequest(url));
net::HttpHeaders headers;
headers.setHeader("User-Agent", getUserAgent());
m_request->setHeaders(headers);
std::stringstream body;
net::HttpResponse response(&body);
if (m_request->send(response)) {
TRACE("Checking updates: %s (User-Agent: %s)\n", url.c_str(), getUserAgent().c_str());
TRACE("Response:\n--\n%s--\n", body.str().c_str());
CheckUpdateResponse data(body.str());
delegate->onResponse(data);
return true;
}
else
return false;
}
private:
std::unique_ptr<net::HttpRequest> m_request;
};
CheckUpdate::CheckUpdate()
: m_impl(new CheckUpdateImpl)
{
}
CheckUpdate::~CheckUpdate()
{
delete m_impl;
}
void CheckUpdate::abort()
{
m_impl->abort();
}
bool CheckUpdate::checkNewVersion(const Uuid& uuid, const std::string& extraParams, CheckUpdateDelegate* delegate)
{
return m_impl->checkNewVersion(uuid, extraParams, delegate);
}
} // namespace updater