2016-03-24 12:57:10 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-03-24 12:57:10 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-03-24 12:57:10 +00:00
|
|
|
*/
|
|
|
|
#include "mtproto/connection_http.h"
|
|
|
|
|
|
|
|
namespace MTP {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
mtpBuffer HTTPConnection::handleResponse(QNetworkReply *reply) {
|
|
|
|
QByteArray response = reply->readAll();
|
|
|
|
TCP_LOG(("HTTP Info: read %1 bytes").arg(response.size()));
|
|
|
|
|
|
|
|
if (response.isEmpty()) return mtpBuffer();
|
|
|
|
|
|
|
|
if (response.size() & 0x03 || response.size() < 8) {
|
|
|
|
LOG(("HTTP Error: bad response size %1").arg(response.size()));
|
|
|
|
return mtpBuffer(1, -500);
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpBuffer data(response.size() >> 2);
|
|
|
|
memcpy(data.data(), response.constData(), response.size());
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
qint32 HTTPConnection::handleError(QNetworkReply *reply) { // returnes "maybe bad key"
|
|
|
|
auto result = qint32(kErrorCodeOther);
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
if (statusCode.isValid()) {
|
|
|
|
int status = statusCode.toInt();
|
2017-02-25 16:44:02 +00:00
|
|
|
result = -status;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (reply->error()) {
|
|
|
|
case QNetworkReply::ConnectionRefusedError: LOG(("HTTP Error: connection refused - %1").arg(reply->errorString())); break;
|
|
|
|
case QNetworkReply::RemoteHostClosedError: LOG(("HTTP Error: remote host closed - %1").arg(reply->errorString())); break;
|
|
|
|
case QNetworkReply::HostNotFoundError: LOG(("HTTP Error: host not found - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
case QNetworkReply::TimeoutError: LOG(("HTTP Error: timeout - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
case QNetworkReply::OperationCanceledError: LOG(("HTTP Error: cancelled - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
case QNetworkReply::TemporaryNetworkFailureError:
|
|
|
|
case QNetworkReply::NetworkSessionFailedError:
|
|
|
|
case QNetworkReply::BackgroundRequestNotAllowedError:
|
|
|
|
case QNetworkReply::UnknownNetworkError: LOG(("HTTP Error: network error %1 - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// proxy errors (101-199):
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::ProxyConnectionRefusedError:
|
|
|
|
case QNetworkReply::ProxyConnectionClosedError:
|
|
|
|
case QNetworkReply::ProxyNotFoundError:
|
|
|
|
case QNetworkReply::ProxyTimeoutError:
|
|
|
|
case QNetworkReply::ProxyAuthenticationRequiredError:
|
|
|
|
case QNetworkReply::UnknownProxyError:LOG(("HTTP Error: proxy error %1 - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// content errors (201-299):
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::ContentAccessDenied:
|
|
|
|
case QNetworkReply::ContentOperationNotPermittedError:
|
|
|
|
case QNetworkReply::ContentNotFoundError:
|
|
|
|
case QNetworkReply::AuthenticationRequiredError:
|
|
|
|
case QNetworkReply::ContentReSendError:
|
|
|
|
case QNetworkReply::UnknownContentError: LOG(("HTTP Error: content error %1 - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
// protocol errors
|
2016-03-24 12:57:10 +00:00
|
|
|
case QNetworkReply::ProtocolUnknownError:
|
|
|
|
case QNetworkReply::ProtocolInvalidOperationError:
|
|
|
|
case QNetworkReply::ProtocolFailure: LOG(("HTTP Error: protocol error %1 - %2").arg(reply->error()).arg(reply->errorString())); break;
|
|
|
|
};
|
|
|
|
TCP_LOG(("HTTP Error %1, restarting! - %2").arg(reply->error()).arg(reply->errorString()));
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
return result;
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPConnection::HTTPConnection(QThread *thread) : AbstractConnection(thread)
|
|
|
|
, status(WaitingHttp)
|
|
|
|
, httpNonce(rand_value<MTPint128>())
|
|
|
|
, _flags(0) {
|
|
|
|
manager.moveToThread(thread);
|
|
|
|
App::setProxySettings(manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::sendData(mtpBuffer &buffer) {
|
|
|
|
if (status == FinishedWork) return;
|
|
|
|
|
|
|
|
if (buffer.size() < 3) {
|
|
|
|
LOG(("TCP Error: writing bad packet, len = %1").arg(buffer.size() * sizeof(mtpPrime)));
|
|
|
|
TCP_LOG(("TCP Error: bad packet %1").arg(Logs::mb(&buffer[0], buffer.size() * sizeof(mtpPrime)).str()));
|
2017-02-25 16:44:02 +00:00
|
|
|
emit error(kErrorCodeOther);
|
2016-03-24 12:57:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 requestSize = (buffer.size() - 3) * sizeof(mtpPrime);
|
|
|
|
|
|
|
|
QNetworkRequest request(address);
|
|
|
|
request.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(requestSize));
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(qsl("application/x-www-form-urlencoded")));
|
|
|
|
|
|
|
|
TCP_LOG(("HTTP Info: sending %1 len request %2").arg(requestSize).arg(Logs::mb(&buffer[2], requestSize).str()));
|
|
|
|
requests.insert(manager.post(request, QByteArray((const char*)(&buffer[2]), requestSize)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::disconnectFromServer() {
|
|
|
|
if (status == FinishedWork) return;
|
|
|
|
status = FinishedWork;
|
|
|
|
|
|
|
|
Requests copy = requests;
|
|
|
|
requests.clear();
|
|
|
|
for (Requests::const_iterator i = copy.cbegin(), e = copy.cend(); i != e; ++i) {
|
|
|
|
(*i)->abort();
|
|
|
|
(*i)->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
|
|
|
|
|
|
|
|
address = QUrl();
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
void HTTPConnection::connectHttp(const DcOptions::Endpoint &endpoint) {
|
|
|
|
_flags = endpoint.flags;
|
|
|
|
auto addr = QString::fromStdString(endpoint.ip);
|
|
|
|
|
|
|
|
// not endpoint.port - always 80 port for http transport
|
|
|
|
address = QUrl(((_flags & MTPDdcOption::Flag::f_ipv6) ? qsl("http://[%1]:%2/api") : qsl("http://%1:%2/api")).arg(addr).arg(80));
|
2016-03-24 12:57:10 +00:00
|
|
|
TCP_LOG(("HTTP Info: address is %1").arg(address.toDisplayString()));
|
|
|
|
connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));
|
|
|
|
|
|
|
|
mtpBuffer buffer(preparePQFake(httpNonce));
|
|
|
|
|
2017-02-23 06:57:04 +00:00
|
|
|
DEBUG_LOG(("Connection Info: sending fake req_pq through HTTP/%1 transport").arg((_flags & MTPDdcOption::Flag::f_ipv6) ? "IPv6" : "IPv4"));
|
2016-03-24 12:57:10 +00:00
|
|
|
|
|
|
|
sendData(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPConnection::isConnected() const {
|
|
|
|
return (status == UsingHttp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPConnection::requestFinished(QNetworkReply *reply) {
|
|
|
|
if (status == FinishedWork) return;
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
if (reply->error() == QNetworkReply::NoError) {
|
|
|
|
requests.remove(reply);
|
|
|
|
|
|
|
|
mtpBuffer data = handleResponse(reply);
|
|
|
|
if (data.size() == 1) {
|
2017-02-25 16:44:02 +00:00
|
|
|
emit error(data[0]);
|
2016-03-24 12:57:10 +00:00
|
|
|
} else if (!data.isEmpty()) {
|
|
|
|
if (status == UsingHttp) {
|
2017-02-27 09:51:03 +00:00
|
|
|
_receivedQueue.push_back(data);
|
2016-03-24 12:57:10 +00:00
|
|
|
emit receivedData();
|
|
|
|
} else {
|
|
|
|
try {
|
2016-04-08 10:44:35 +00:00
|
|
|
auto res_pq = readPQFakeReply(data);
|
|
|
|
const auto &res_pq_data(res_pq.c_resPQ());
|
2016-03-24 12:57:10 +00:00
|
|
|
if (res_pq_data.vnonce == httpNonce) {
|
|
|
|
DEBUG_LOG(("Connection Info: HTTP/%1-transport connected by pq-response").arg((_flags & MTPDdcOption::Flag::f_ipv6) ? "IPv6" : "IPv4"));
|
|
|
|
status = UsingHttp;
|
|
|
|
emit connected();
|
|
|
|
}
|
|
|
|
} catch (Exception &e) {
|
|
|
|
DEBUG_LOG(("Connection Error: exception in parsing HTTP fake pq-responce, %1").arg(e.what()));
|
2017-02-25 16:44:02 +00:00
|
|
|
emit error(kErrorCodeOther);
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!requests.remove(reply)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
emit error(handleError(reply));
|
2016-03-24 12:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPConnection::usingHttpWait() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPConnection::needHttpWait() {
|
|
|
|
return requests.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 HTTPConnection::debugState() const {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString HTTPConnection::transport() const {
|
|
|
|
if (status == UsingHttp) {
|
|
|
|
return qsl("HTTP");
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace MTP
|