-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathconnection_http.cpp
More file actions
289 lines (243 loc) · 7.35 KB
/
Copy pathconnection_http.cpp
File metadata and controls
289 lines (243 loc) · 7.35 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "mtproto/connection_http.h"
#include "base/random.h"
#include "base/qthelp_url.h"
namespace MTP {
namespace details {
namespace {
constexpr auto kForceHttpPort = 80;
constexpr auto kFullConnectionTimeout = crl::time(8000);
} // namespace
HttpConnection::HttpConnection(QThread *thread, const ProxyData &proxy)
: AbstractConnection(thread, proxy)
, _checkNonce(base::RandomValue<MTPint128>()) {
_manager.moveToThread(thread);
_manager.setProxy(ToNetworkProxy(proxy));
}
ConnectionPointer HttpConnection::clone(const ProxyData &proxy) {
return ConnectionPointer::New<HttpConnection>(thread(), proxy);
}
void HttpConnection::sendData(mtpBuffer &&buffer) {
Expects(buffer.size() > 2);
if (_status == Status::Finished) {
return;
}
int32 requestSize = (buffer.size() - 2) * sizeof(mtpPrime);
QNetworkRequest request(url());
request.setHeader(
QNetworkRequest::ContentLengthHeader,
QVariant(requestSize));
request.setHeader(
QNetworkRequest::ContentTypeHeader,
QVariant(u"application/x-www-form-urlencoded"_q));
CONNECTION_LOG_INFO(u"Sending %1 len request."_q.arg(requestSize));
_requests.insert(_manager.post(request, QByteArray((const char*)(&buffer[2]), requestSize)));
}
void HttpConnection::disconnectFromServer() {
if (_status == Status::Finished) return;
_status = Status::Finished;
const auto requests = base::take(_requests);
for (const auto request : requests) {
request->abort();
request->deleteLater();
}
disconnect(
&_manager,
&QNetworkAccessManager::finished,
this,
&HttpConnection::requestFinished);
}
void HttpConnection::connectToServer(
const QString &address,
int port,
const bytes::vector &protocolSecret,
int16 protocolDcId,
bool protocolForFiles) {
_address = address;
connect(
&_manager,
&QNetworkAccessManager::finished,
this,
&HttpConnection::requestFinished);
auto buffer = preparePQFake(_checkNonce);
if (Logs::DebugEnabled()) {
_debugId = u"%1(dc:%2,%3)"_q
.arg(_debugId.toInt())
.arg(ProtocolDcDebugId(protocolDcId), url().toDisplayString());
}
_pingTime = crl::now();
sendData(std::move(buffer));
}
mtpBuffer HttpConnection::handleResponse(QNetworkReply *reply) {
QByteArray response = reply->readAll();
CONNECTION_LOG_INFO(u"Read %1 bytes."_q.arg(response.size()));
if (response.isEmpty()) return mtpBuffer();
if (response.size() & 0x03 || response.size() < 8) {
CONNECTION_LOG_ERROR(u"Bad response size %1."_q.arg(response.size()));
return mtpBuffer(1, -500);
}
mtpBuffer data(response.size() >> 2);
memcpy(data.data(), response.constData(), response.size());
return data;
}
// Returns "maybe bad key".
qint32 HttpConnection::handleError(QNetworkReply *reply) {
auto result = qint32(kErrorCodeOther);
QVariant statusCode = reply->attribute(
QNetworkRequest::HttpStatusCodeAttribute);
if (statusCode.isValid()) {
int status = statusCode.toInt();
result = -status;
}
switch (reply->error()) {
case QNetworkReply::ConnectionRefusedError:
CONNECTION_LOG_ERROR(u"Connection refused - %1."_q
.arg(reply->errorString()));
break;
case QNetworkReply::RemoteHostClosedError:
CONNECTION_LOG_ERROR(u"Remote host closed - %1."_q
.arg(reply->errorString()));
break;
case QNetworkReply::HostNotFoundError:
CONNECTION_LOG_ERROR(u"Host not found - %1."_q
.arg(reply->errorString()));
break;
case QNetworkReply::TimeoutError:
CONNECTION_LOG_ERROR(u"Timeout - %1."_q
.arg(reply->errorString()));
break;
case QNetworkReply::OperationCanceledError:
CONNECTION_LOG_ERROR(u"Cancelled - %1."_q
.arg(reply->errorString()));
break;
case QNetworkReply::SslHandshakeFailedError:
case QNetworkReply::TemporaryNetworkFailureError:
case QNetworkReply::NetworkSessionFailedError:
case QNetworkReply::BackgroundRequestNotAllowedError:
case QNetworkReply::UnknownNetworkError:
CONNECTION_LOG_ERROR(u"Network error %1 - %2."_q
.arg(reply->error())
.arg(reply->errorString()));
break;
// proxy errors (101-199):
case QNetworkReply::ProxyConnectionRefusedError:
case QNetworkReply::ProxyConnectionClosedError:
case QNetworkReply::ProxyNotFoundError:
case QNetworkReply::ProxyTimeoutError:
case QNetworkReply::ProxyAuthenticationRequiredError:
case QNetworkReply::UnknownProxyError:
CONNECTION_LOG_ERROR(u"Proxy error %1 - %2."_q
.arg(reply->error())
.arg(reply->errorString()));
break;
// content errors (201-299):
case QNetworkReply::ContentAccessDenied:
case QNetworkReply::ContentOperationNotPermittedError:
case QNetworkReply::ContentNotFoundError:
case QNetworkReply::AuthenticationRequiredError:
case QNetworkReply::ContentReSendError:
case QNetworkReply::UnknownContentError:
CONNECTION_LOG_ERROR(u"Content error %1 - %2."_q
.arg(reply->error())
.arg(reply->errorString()));
break;
// protocol errors
case QNetworkReply::ProtocolUnknownError:
case QNetworkReply::ProtocolInvalidOperationError:
case QNetworkReply::ProtocolFailure:
CONNECTION_LOG_ERROR(u"Protocol error %1 - %2."_q
.arg(reply->error())
.arg(reply->errorString()));
break;
};
return result;
}
bool HttpConnection::isConnected() const {
return (_status == Status::Ready);
}
void HttpConnection::requestFinished(QNetworkReply *reply) {
if (_status == Status::Finished) return;
reply->deleteLater();
if (reply->error() == QNetworkReply::NoError) {
_requests.remove(reply);
mtpBuffer data = handleResponse(reply);
if (data.size() == 1) {
error(data[0]);
} else if (!data.isEmpty()) {
if (_status == Status::Ready) {
_receivedQueue.push_back(data);
receivedData();
} else if (const auto res_pq = readPQFakeReply(data)) {
const auto &data = res_pq->c_resPQ();
if (data.vnonce() == _checkNonce) {
CONNECTION_LOG_INFO(
"HTTP-transport connected by pq-response.");
_status = Status::Ready;
_pingTime = crl::now() - _pingTime;
connected();
} else {
CONNECTION_LOG_ERROR(
"Wrong nonce in HTTP fake pq-response.");
error(kErrorCodeOther);
}
} else {
CONNECTION_LOG_ERROR(
"Could not parse HTTP fake pq-response.");
error(kErrorCodeOther);
}
}
} else {
if (!_requests.remove(reply)) {
return;
}
error(handleError(reply));
}
}
crl::time HttpConnection::pingTime() const {
return isConnected() ? _pingTime : crl::time(0);
}
crl::time HttpConnection::fullConnectTimeout() const {
return kFullConnectionTimeout;
}
bool HttpConnection::usingHttpWait() {
return true;
}
bool HttpConnection::needHttpWait() {
return _requests.isEmpty();
}
int32 HttpConnection::debugState() const {
return -1;
}
QString HttpConnection::transport() const {
if (!isConnected()) {
return QString();
}
auto result = u"HTTP"_q;
if (qthelp::is_ipv6(_address)) {
result += u"/IPv6"_q;
}
return result;
}
QString HttpConnection::tag() const {
auto result = u"HTTP"_q;
if (qthelp::is_ipv6(_address)) {
result += u"/IPv6"_q;
} else {
result += u"/IPv4"_q;
}
return result;
}
QUrl HttpConnection::url() const {
const auto pattern = qthelp::is_ipv6(_address)
? u"http://[%1]:%2/api"_q
: u"http://%1:%2/api"_q;
// Not endpoint.port - always 80 port for http transport.
return QUrl(pattern.arg(_address).arg(kForceHttpPort));
}
} // namespace details
} // namespace MTP