forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_update.cpp
More file actions
158 lines (129 loc) · 3.44 KB
/
check_update.cpp
File metadata and controls
158 lines (129 loc) · 3.44 KB
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
157
158
// Aseprite
// Copyright (C) 2001-2016 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/bind.h"
#include "base/convert_to.h"
#include "base/debug.h"
#include "base/unique_ptr.h"
#include "net/http_headers.h"
#include "net/http_request.h"
#include "net/http_response.h"
#include "tinyxml.h"
#include "updater/user_agent.h"
#include <iostream>
#include <sstream>
namespace updater {
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)
{
TiXmlDocument doc;
doc.Parse(responseBody.c_str());
TiXmlHandle handle(&doc);
TiXmlElement* xmlUpdate = handle.FirstChild("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)
{
#ifndef UPDATE_URL
#define UPDATE_URL ""
#pragma message("warning: Define UPDATE_URL macro")
#endif
std::string url = 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:
base::UniquePtr<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