Provide torrent creation feature via WebAPI
[qBittorrent.git] / src / base / net / downloadhandlerimpl.cpp
blob370347946c1f0741e2ecff0f0bd0e8e637982007
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 <QUrl>
35 #include "base/3rdparty/expected.hpp"
36 #include "base/utils/fs.h"
37 #include "base/utils/io.h"
38 #include "base/utils/misc.h"
40 #ifdef QT_NO_COMPRESS
41 #include "base/utils/gzip.h"
42 #endif
44 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
45 #include "base/preferences.h"
46 #include "base/utils/os.h"
47 #endif // Q_OS_MACOS || Q_OS_WIN
49 const int MAX_REDIRECTIONS = 20; // the common value for web browsers
51 Net::DownloadHandlerImpl::DownloadHandlerImpl(DownloadManager *manager
52 , const DownloadRequest &downloadRequest, const bool useProxy)
53 : DownloadHandler {manager}
54 , m_manager {manager}
55 , m_downloadRequest {downloadRequest}
56 , m_useProxy {useProxy}
58 m_result.url = url();
59 m_result.status = DownloadStatus::Success;
62 void Net::DownloadHandlerImpl::cancel()
64 if (m_isFinished)
65 return;
67 if (m_reply && m_reply->isRunning())
69 m_reply->abort();
71 else if (m_redirectionHandler)
73 m_redirectionHandler->cancel();
75 else
77 setError(errorCodeToString(QNetworkReply::OperationCanceledError));
78 finish();
82 void Net::DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply)
84 Q_ASSERT(reply);
85 Q_ASSERT(!m_reply);
86 Q_ASSERT(!m_isFinished);
88 m_reply = reply;
89 m_reply->setParent(this);
90 if (m_downloadRequest.limit() > 0)
91 connect(m_reply, &QNetworkReply::downloadProgress, this, &DownloadHandlerImpl::checkDownloadSize);
92 connect(m_reply, &QNetworkReply::finished, this, &DownloadHandlerImpl::processFinishedDownload);
95 QNetworkReply *Net::DownloadHandlerImpl::assignedNetworkReply() const
97 return m_reply;
100 // Returns original url
101 QString Net::DownloadHandlerImpl::url() const
103 return m_downloadRequest.url();
106 Net::DownloadRequest Net::DownloadHandlerImpl::downloadRequest() const
108 return m_downloadRequest;
111 bool Net::DownloadHandlerImpl::useProxy() const
113 return m_useProxy;
116 void Net::DownloadHandlerImpl::processFinishedDownload()
118 qDebug("Download finished: %s", qUtf8Printable(url()));
120 // Check if the request was successful
121 if (m_reply->error() != QNetworkReply::NoError)
123 // Failure
124 qDebug("Download failure (%s), reason: %s", qUtf8Printable(url()), qUtf8Printable(errorCodeToString(m_reply->error())));
125 setError(errorCodeToString(m_reply->error()));
126 finish();
127 return;
130 // Check if the server ask us to redirect somewhere else
131 const QVariant redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
132 if (redirection.isValid())
134 handleRedirection(redirection.toUrl());
135 return;
138 // Success
139 #ifdef QT_NO_COMPRESS
140 m_result.data = (m_reply->rawHeader("Content-Encoding") == "gzip")
141 ? Utils::Gzip::decompress(m_reply->readAll())
142 : m_reply->readAll();
143 #else
144 m_result.data = m_reply->readAll();
145 #endif
147 if (m_downloadRequest.saveToFile())
149 const Path destinationPath = m_downloadRequest.destFileName();
150 if (destinationPath.isEmpty())
152 const nonstd::expected<Path, QString> result = Utils::IO::saveToTempFile(m_result.data);
153 if (result)
155 m_result.filePath = result.value();
157 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
158 if (Preferences::instance()->isMarkOfTheWebEnabled())
159 Utils::OS::applyMarkOfTheWeb(m_result.filePath, m_result.url);
160 #endif // Q_OS_MACOS || Q_OS_WIN
162 else
164 setError(tr("I/O Error: %1").arg(result.error()));
167 else
169 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(destinationPath, m_result.data);
170 if (result)
172 m_result.filePath = destinationPath;
174 #if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
175 if (Preferences::instance()->isMarkOfTheWebEnabled())
176 Utils::OS::applyMarkOfTheWeb(m_result.filePath, m_result.url);
177 #endif // Q_OS_MACOS || Q_OS_WIN
179 else
181 setError(tr("I/O Error: %1").arg(result.error()));
186 finish();
189 void Net::DownloadHandlerImpl::checkDownloadSize(const qint64 bytesReceived, const qint64 bytesTotal)
191 if ((bytesTotal > 0) && (bytesTotal <= m_downloadRequest.limit()))
193 // Total number of bytes is available
194 disconnect(m_reply, &QNetworkReply::downloadProgress, this, &DownloadHandlerImpl::checkDownloadSize);
195 return;
198 if ((bytesTotal > m_downloadRequest.limit()) || (bytesReceived > m_downloadRequest.limit()))
200 m_reply->abort();
201 setError(tr("The file size (%1) exceeds the download limit (%2)")
202 .arg(Utils::Misc::friendlyUnit(bytesTotal)
203 , Utils::Misc::friendlyUnit(m_downloadRequest.limit())));
204 finish();
208 void Net::DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
210 if (m_redirectionCount >= MAX_REDIRECTIONS)
212 setError(tr("Exceeded max redirections (%1)").arg(MAX_REDIRECTIONS));
213 finish();
214 return;
217 // Resolve relative urls
218 const QUrl resolvedUrl = newUrl.isRelative() ? m_reply->url().resolved(newUrl) : newUrl;
219 const QString newUrlString = resolvedUrl.toString();
220 qDebug("Redirecting from %s to %s...", qUtf8Printable(m_reply->url().toString()), qUtf8Printable(newUrlString));
222 // Redirect to magnet workaround
223 if (newUrlString.startsWith(u"magnet:", Qt::CaseInsensitive))
225 m_result.status = Net::DownloadStatus::RedirectedToMagnet;
226 m_result.magnetURI = newUrlString;
227 m_result.errorString = tr("Redirected to magnet URI");
228 finish();
229 return;
232 m_redirectionHandler = static_cast<DownloadHandlerImpl *>(
233 m_manager->download(DownloadRequest(m_downloadRequest).url(newUrlString), useProxy()));
234 m_redirectionHandler->m_redirectionCount = m_redirectionCount + 1;
235 connect(m_redirectionHandler, &DownloadHandlerImpl::finished, this, [this](const DownloadResult &result)
237 m_result = result;
238 m_result.url = url();
239 finish();
243 void Net::DownloadHandlerImpl::setError(const QString &error)
245 m_result.errorString = error;
246 m_result.status = DownloadStatus::Failed;
249 void Net::DownloadHandlerImpl::finish()
251 m_isFinished = true;
252 emit finished(m_result);
255 QString Net::DownloadHandlerImpl::errorCodeToString(const QNetworkReply::NetworkError status)
257 switch (status)
259 case QNetworkReply::HostNotFoundError:
260 return tr("The remote host name was not found (invalid hostname)");
261 case QNetworkReply::OperationCanceledError:
262 return tr("The operation was canceled");
263 case QNetworkReply::RemoteHostClosedError:
264 return tr("The remote server closed the connection prematurely, before the entire reply was received and processed");
265 case QNetworkReply::TimeoutError:
266 return tr("The connection to the remote server timed out");
267 case QNetworkReply::SslHandshakeFailedError:
268 return tr("SSL/TLS handshake failed");
269 case QNetworkReply::ConnectionRefusedError:
270 return tr("The remote server refused the connection");
271 case QNetworkReply::ProxyConnectionRefusedError:
272 return tr("The connection to the proxy server was refused");
273 case QNetworkReply::ProxyConnectionClosedError:
274 return tr("The proxy server closed the connection prematurely");
275 case QNetworkReply::ProxyNotFoundError:
276 return tr("The proxy host name was not found");
277 case QNetworkReply::ProxyTimeoutError:
278 return tr("The connection to the proxy timed out or the proxy did not reply in time to the request sent");
279 case QNetworkReply::ProxyAuthenticationRequiredError:
280 return tr("The proxy requires authentication in order to honor the request but did not accept any credentials offered");
281 case QNetworkReply::ContentAccessDenied:
282 return tr("The access to the remote content was denied (401)");
283 case QNetworkReply::ContentOperationNotPermittedError:
284 return tr("The operation requested on the remote content is not permitted");
285 case QNetworkReply::ContentNotFoundError:
286 return tr("The remote content was not found at the server (404)");
287 case QNetworkReply::AuthenticationRequiredError:
288 return tr("The remote server requires authentication to serve the content but the credentials provided were not accepted");
289 case QNetworkReply::ProtocolUnknownError:
290 return tr("The Network Access API cannot honor the request because the protocol is not known");
291 case QNetworkReply::ProtocolInvalidOperationError:
292 return tr("The requested operation is invalid for this protocol");
293 case QNetworkReply::UnknownNetworkError:
294 return tr("An unknown network-related error was detected");
295 case QNetworkReply::UnknownProxyError:
296 return tr("An unknown proxy-related error was detected");
297 case QNetworkReply::UnknownContentError:
298 return tr("An unknown error related to the remote content was detected");
299 case QNetworkReply::ProtocolFailure:
300 return tr("A breakdown in protocol was detected");
301 default:
302 return tr("Unknown error");