Skip to content

Commit

Permalink
fix: HTML detection for documents starting with <html>
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Nov 5, 2022
1 parent df00f39 commit 319b523
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/src/downloader/file-downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void FileDownloader::replyFinished()
const auto msg = m_reply->errorString();
const QUrl redirectUrl = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
const bool failedLastWrite = data.length() > 0 && written < 0;
const bool invalidHtml = !m_allowHtmlResponses && QString(data.left(100)).trimmed().startsWith("<!DOCTYPE", Qt::CaseInsensitive);
const bool invalidHtml = !m_allowHtmlResponses && isHtml(data);
const bool emptyFile = m_readSize == 0 && redirectUrl.isEmpty();

if (error != NetworkReply::NetworkError::NoError || failedLastWrite || invalidHtml || emptyFile) {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,3 +1210,15 @@ void renameSettingsKey(QSettings *settings, const QString &before, const QString
settings->setValue(after, settings->value(before));
settings->remove(before);
}


/**
* Helper function to know if a byte array contains HTML.
* Only performs a basic check and might fail for XML or some differently formatted documents.
*/
bool isHtml(const QByteArray &data)
{
const QString left = QString(data.left(100)).trimmed();
return left.startsWith("<!DOCTYPE", Qt::CaseInsensitive)
|| left.startsWith("<html>", Qt::CaseInsensitive);
}
2 changes: 2 additions & 0 deletions src/lib/src/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ QRect stringToRect(const QString &str);
void renameSettingsGroup(QSettings *settings, const QString &before, const QString &after);
void renameSettingsKey(QSettings *settings, const QString &before, const QString &after);

bool isHtml(const QByteArray &data);



template <typename T>
Expand Down

0 comments on commit 319b523

Please sign in to comment.