This commit is contained in:
Bark 2025-08-18 09:27:20 +03:00 committed by GitHub
commit bac9a11cc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 2 deletions

View file

@ -4038,6 +4038,11 @@ void SessionImpl::updateTrackersFromURL()
{ {
if (result.status == Net::DownloadStatus::Success) if (result.status == Net::DownloadStatus::Success)
{ {
if (!result.contentType.contains(u"text/plain"_s, Qt::CaseInsensitive))
{
LogMsg(tr("Cannot add trackers from URL, expected Content-Type is \'text/plain\' received \"%1\"").arg(result.contentType), Log::WARNING);
return;
}
setAdditionalTrackersFromURL(QString::fromUtf8(result.data)); setAdditionalTrackersFromURL(QString::fromUtf8(result.data));
LogMsg(tr("Tracker list updated"), Log::INFO); LogMsg(tr("Tracker list updated"), Log::INFO);
return; return;

View file

@ -143,6 +143,7 @@ void Net::DownloadHandlerImpl::processFinishedDownload()
#else #else
m_result.data = m_reply->readAll(); m_result.data = m_reply->readAll();
#endif #endif
m_result.contentType = m_reply->header(QNetworkRequest::ContentTypeHeader).toString();
if (m_downloadRequest.saveToFile()) if (m_downloadRequest.saveToFile())
{ {

View file

@ -104,6 +104,7 @@ namespace Net
QString url; QString url;
DownloadStatus status = DownloadStatus::Failed; DownloadStatus status = DownloadStatus::Failed;
QString errorString; QString errorString;
QString contentType;
QByteArray data; QByteArray data;
Path filePath; Path filePath;
QString magnetURI; QString magnetURI;

View file

@ -74,14 +74,35 @@ TrackersAdditionDialog::~TrackersAdditionDialog()
delete m_ui; delete m_ui;
} }
void TrackersAdditionDialog::onAccepted() const bool isValidEndpoint(const QStringView endpoint)
{
if (endpoint.isEmpty())
return false;
const QUrl url {endpoint.toString()};
if (!url.isValid())
return false;
if (url.scheme().isEmpty())
return false;
if (url.host().isEmpty())
return false;
return true;
}
void TrackersAdditionDialog::onAccepted()
{ {
const QList<BitTorrent::TrackerEntryStatus> currentTrackers = m_torrent->trackers(); const QList<BitTorrent::TrackerEntryStatus> currentTrackers = m_torrent->trackers();
const int baseTier = !currentTrackers.isEmpty() ? (currentTrackers.last().tier + 1) : 0; const int baseTier = !currentTrackers.isEmpty() ? (currentTrackers.last().tier + 1) : 0;
QList<BitTorrent::TrackerEntry> entries = BitTorrent::parseTrackerEntries(m_ui->textEditTrackersList->toPlainText()); QList<BitTorrent::TrackerEntry> entries = BitTorrent::parseTrackerEntries(m_ui->textEditTrackersList->toPlainText());
for (BitTorrent::TrackerEntry &entry : entries) for (BitTorrent::TrackerEntry &entry : entries)
{
if (!isValidEndpoint(entry.url))
{
QMessageBox::warning(this, tr("Invalid tracker URL"), tr("The tracker URL \"%1\" is invalid").arg(entry.url));
return;
}
entry.tier = Utils::Number::clampingAdd(entry.tier, baseTier); entry.tier = Utils::Number::clampingAdd(entry.tier, baseTier);
}
m_torrent->addTrackers(entries); m_torrent->addTrackers(entries);
} }
@ -116,6 +137,13 @@ void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadRe
return; return;
} }
if (!result.contentType.contains(u"text/plain"_s, Qt::CaseInsensitive))
{
QMessageBox::warning(this, tr("Download trackers list error")
, tr("The content type of the downloaded file is not plain text. Content-Type: \"%1\"").arg(result.contentType));
return;
}
// Add fetched trackers to the list // Add fetched trackers to the list
const QString existingText = m_ui->textEditTrackersList->toPlainText(); const QString existingText = m_ui->textEditTrackersList->toPlainText();
if (!existingText.isEmpty() && !existingText.endsWith(u'\n')) if (!existingText.isEmpty() && !existingText.endsWith(u'\n'))

View file

@ -58,7 +58,7 @@ public:
~TrackersAdditionDialog(); ~TrackersAdditionDialog();
private slots: private slots:
void onAccepted() const; void onAccepted();
void onDownloadButtonClicked(); void onDownloadButtonClicked();
void onTorrentListDownloadFinished(const Net::DownloadResult &result); void onTorrentListDownloadFinished(const Net::DownloadResult &result);