Improve Download Manager subsystem
[qBittorrent.git] / src / base / net / downloadhandlerimpl.cpp
blobc96c3cd7678f3f95209e04698d5e4885edcfa47b
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "downloadhandlerimpl.h"
32 #include <QtSystemDetection>
33 #include <QTemporaryFile>
34 #include <QUrl>
36 #include "base/3rdparty/expected.hpp"
37 #include "base/utils/fs.h"
38 #include "base/utils/io.h"
39 #include "base/utils/misc.h"
41 #ifdef QT_NO_COMPRESS
42 #include "base/utils/gzip.h"
43 #endif
45 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
46 #include "base/preferences.h"
47 #include "base/utils/os.h"
48 #endif // Q_OS_MACOS || Q_OS_WIN
50 const int MAX_REDIRECTIONS = 20; // the common value for web browsers
52 namespace
54 nonstd::expected<Path, QString> saveToTempFile(const QByteArray &data)
56 QTemporaryFile file {Utils::Fs::tempPath().data()};
57 if (!file.open() || (file.write(data) != data.length()) || !file.flush())
58 return nonstd::make_unexpected(file.errorString());
60 file.setAutoRemove(false);
61 return Path(file.fileName());
65 Net::DownloadHandlerImpl::DownloadHandlerImpl(DownloadManager *manager
66 , const DownloadRequest &downloadRequest, const bool useProxy)
67 : DownloadHandler {manager}
68 , m_manager {manager}
69 , m_downloadRequest {downloadRequest}
70 , m_useProxy {useProxy}
72 m_result.url = url();
73 m_result.status = DownloadStatus::Success;
76 void Net::DownloadHandlerImpl::cancel()
78 if (m_isFinished)
79 return;
81 if (m_reply && m_reply->isRunning())
83 m_reply->abort();
85 else if (m_redirectionHandler)
87 m_redirectionHandler->cancel();
89 else
91 setError(errorCodeToString(QNetworkReply::OperationCanceledError));
92 finish();
96 void Net::DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply)
98 Q_ASSERT(reply);
99 Q_ASSERT(!m_reply);
100 Q_ASSERT(!m_isFinished);
102 m_reply = reply;
103 m_reply->setParent(this);
104 if (m_downloadRequest.limit() > 0)
105 connect(m_reply, &QNetworkReply::downloadProgress, this, &DownloadHandlerImpl::checkDownloadSize);
106 connect(m_reply, &QNetworkReply::finished, this, &DownloadHandlerImpl::processFinishedDownload);
109 QNetworkReply *Net::DownloadHandlerImpl::assignedNetworkReply() const
111 return m_reply;
114 // Returns original url
115 QString Net::DownloadHandlerImpl::url() const
117 return m_downloadRequest.url();
120 Net::DownloadRequest Net::DownloadHandlerImpl::downloadRequest() const
122 return m_downloadRequest;
125 bool Net::DownloadHandlerImpl::useProxy() const
127 return m_useProxy;
130 void Net::DownloadHandlerImpl::processFinishedDownload()
132 qDebug("Download finished: %s", qUtf8Printable(url()));
134 // Check if the request was successful
135 if (m_reply->error() != QNetworkReply::NoError)
137 // Failure
138 qDebug("Download failure (%s), reason: %s", qUtf8Printable(url()), qUtf8Printable(errorCodeToString(m_reply->error())));
139 setError(errorCodeToString(m_reply->error()));
140 finish();
141 return;
144 // Check if the server ask us to redirect somewhere else
145 const QVariant redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
146 if (redirection.isValid())
148 handleRedirection(redirection.toUrl());
149 return;
152 // Success
153 #ifdef QT_NO_COMPRESS
154 m_result.data = (m_reply->rawHeader("Content-Encoding") == "gzip")
155 ? Utils::Gzip::decompress(m_reply->readAll())
156 : m_reply->readAll();
157 #else
158 m_result.data = m_reply->readAll();
159 #endif
161 if (m_downloadRequest.saveToFile())
163 const Path destinationPath = m_downloadRequest.destFileName();
164 if (destinationPath.isEmpty())
166 const nonstd::expected<Path, QString> result = saveToTempFile(m_result.data);
167 if (result)
169 m_result.filePath = result.value();
171 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
172 if (Preferences::instance()->isMarkOfTheWebEnabled())
173 Utils::OS::applyMarkOfTheWeb(m_result.filePath, m_result.url);
174 #endif // Q_OS_MACOS || Q_OS_WIN
176 else
178 setError(tr("I/O Error: %1").arg(result.error()));
181 else
183 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(destinationPath, m_result.data);
184 if (result)
186 m_result.filePath = destinationPath;
188 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
189 if (Preferences::instance()->isMarkOfTheWebEnabled())
190 Utils::OS::applyMarkOfTheWeb(m_result.filePath, m_result.url);
191 #endif // Q_OS_MACOS || Q_OS_WIN
193 else
195 setError(tr("I/O Error: %1").arg(result.error()));
200 finish();
203 void Net::DownloadHandlerImpl::checkDownloadSize(const qint64 bytesReceived, const qint64 bytesTotal)
205 if ((bytesTotal > 0) && (bytesTotal <= m_downloadRequest.limit()))
207 // Total number of bytes is available
208 disconnect(m_reply, &QNetworkReply::downloadProgress, this, &DownloadHandlerImpl::checkDownloadSize);
209 return;
212 if ((bytesTotal > m_downloadRequest.limit()) || (bytesReceived > m_downloadRequest.limit()))
214 m_reply->abort();
215 setError(tr("The file size (%1) exceeds the download limit (%2)")
216 .arg(Utils::Misc::friendlyUnit(bytesTotal)
217 , Utils::Misc::friendlyUnit(m_downloadRequest.limit())));
218 finish();
222 void Net::DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
224 if (m_redirectionCount >= MAX_REDIRECTIONS)
226 setError(tr("Exceeded max redirections (%1)").arg(MAX_REDIRECTIONS));
227 finish();
228 return;
231 // Resolve relative urls
232 const QUrl resolvedUrl = newUrl.isRelative() ? m_reply->url().resolved(newUrl) : newUrl;
233 const QString newUrlString = resolvedUrl.toString();
234 qDebug("Redirecting from %s to %s...", qUtf8Printable(m_reply->url().toString()), qUtf8Printable(newUrlString));
236 // Redirect to magnet workaround
237 if (newUrlString.startsWith(u"magnet:", Qt::CaseInsensitive))
239 m_result.status = Net::DownloadStatus::RedirectedToMagnet;
240 m_result.magnetURI = newUrlString;
241 m_result.errorString = tr("Redirected to magnet URI");
242 finish();
243 return;
246 m_redirectionHandler = static_cast<DownloadHandlerImpl *>(
247 m_manager->download(DownloadRequest(m_downloadRequest).url(newUrlString), useProxy()));
248 m_redirectionHandler->m_redirectionCount = m_redirectionCount + 1;
249 connect(m_redirectionHandler, &DownloadHandlerImpl::finished, this, [this](const DownloadResult &result)
251 m_result = result;
252 m_result.url = url();
253 finish();
257 void Net::DownloadHandlerImpl::setError(const QString &error)
259 m_result.errorString = error;
260 m_result.status = DownloadStatus::Failed;
263 void Net::DownloadHandlerImpl::finish()
265 m_isFinished = true;
266 emit finished(m_result);
269 QString Net::DownloadHandlerImpl::errorCodeToString(const QNetworkReply::NetworkError status)
271 switch (status)
273 case QNetworkReply::HostNotFoundError:
274 return tr("The remote host name was not found (invalid hostname)");
275 case QNetworkReply::OperationCanceledError:
276 return tr("The operation was canceled");
277 case QNetworkReply::RemoteHostClosedError:
278 return tr("The remote server closed the connection prematurely, before the entire reply was received and processed");
279 case QNetworkReply::TimeoutError:
280 return tr("The connection to the remote server timed out");
281 case QNetworkReply::SslHandshakeFailedError:
282 return tr("SSL/TLS handshake failed");
283 case QNetworkReply::ConnectionRefusedError:
284 return tr("The remote server refused the connection");
285 case QNetworkReply::ProxyConnectionRefusedError:
286 return tr("The connection to the proxy server was refused");
287 case QNetworkReply::ProxyConnectionClosedError:
288 return tr("The proxy server closed the connection prematurely");
289 case QNetworkReply::ProxyNotFoundError:
290 return tr("The proxy host name was not found");
291 case QNetworkReply::ProxyTimeoutError:
292 return tr("The connection to the proxy timed out or the proxy did not reply in time to the request sent");
293 case QNetworkReply::ProxyAuthenticationRequiredError:
294 return tr("The proxy requires authentication in order to honor the request but did not accept any credentials offered");
295 case QNetworkReply::ContentAccessDenied:
296 return tr("The access to the remote content was denied (401)");
297 case QNetworkReply::ContentOperationNotPermittedError:
298 return tr("The operation requested on the remote content is not permitted");
299 case QNetworkReply::ContentNotFoundError:
300 return tr("The remote content was not found at the server (404)");
301 case QNetworkReply::AuthenticationRequiredError:
302 return tr("The remote server requires authentication to serve the content but the credentials provided were not accepted");
303 case QNetworkReply::ProtocolUnknownError:
304 return tr("The Network Access API cannot honor the request because the protocol is not known");
305 case QNetworkReply::ProtocolInvalidOperationError:
306 return tr("The requested operation is invalid for this protocol");
307 case QNetworkReply::UnknownNetworkError:
308 return tr("An unknown network-related error was detected");
309 case QNetworkReply::UnknownProxyError:
310 return tr("An unknown proxy-related error was detected");
311 case QNetworkReply::UnknownContentError:
312 return tr("An unknown error related to the remote content was detected");
313 case QNetworkReply::ProtocolFailure:
314 return tr("A breakdown in protocol was detected");
315 default:
316 return tr("Unknown error");