diff options
| author | Felix (xq) Queißner <git@mq32.de> | 2020-06-19 19:23:50 +0200 |
|---|---|---|
| committer | Felix (xq) Queißner <git@mq32.de> | 2020-06-19 19:23:50 +0200 |
| commit | 5f6fbb575905455238465b425a2662a9c0de0e4e (patch) | |
| tree | 3754cb104df4093f0bd55ccf261200a7f6ee250e /src | |
| parent | 6fb1b63b670fae0e0fbd77794c4d30c5c0ca74bc (diff) | |
| download | kristall-5f6fbb575905455238465b425a2662a9c0de0e4e.tar.gz | |
Implements network timeouts.
Diffstat (limited to 'src')
| -rw-r--r-- | src/browsertab.cpp | 25 | ||||
| -rw-r--r-- | src/browsertab.hpp | 5 | ||||
| -rw-r--r-- | src/kristall.hpp | 3 |
3 files changed, 33 insertions, 0 deletions
diff --git a/src/browsertab.cpp b/src/browsertab.cpp index 783e9e3..eb34ae3 100644 --- a/src/browsertab.cpp +++ b/src/browsertab.cpp @@ -65,6 +65,11 @@ BrowserTab::BrowserTab(MainWindow *mainWindow) : QWidget(nullptr), this->ui->text_browser->setContextMenuPolicy(Qt::CustomContextMenu); connect(this->ui->url_bar, &SearchBar::escapePressed, this, &BrowserTab::on_url_bar_escapePressed); + + this->network_timeout_timer.setSingleShot(true); + this->network_timeout_timer.setTimerType(Qt::PreciseTimer); + + connect(&this->network_timeout_timer, &QTimer::timeout, this, &BrowserTab::on_networkTimeout); } BrowserTab::~BrowserTab() @@ -184,6 +189,8 @@ void BrowserTab::on_refresh_button_clicked() void BrowserTab::on_networkError(ProtocolHandler::NetworkError error_code, const QString &reason) { + this->network_timeout_timer.stop(); + QString file_name; switch(error_code) { @@ -221,8 +228,18 @@ void BrowserTab::on_networkError(ProtocolHandler::NetworkError error_code, const this->updateUI(); } +void BrowserTab::on_networkTimeout() +{ + if(this->current_handler != nullptr) { + this->current_handler->cancelRequest(); + } + on_networkError(ProtocolHandler::Timeout, "The server didn't respond in time."); +} + void BrowserTab::on_certificateRequired(const QString &reason) { + this->network_timeout_timer.stop(); + if (not trySetClientCertificate(reason)) { setErrorMessage(QString("The page requested a authorized client certificate, but none was provided.\r\nOriginal query was: %1").arg(reason)); @@ -302,6 +319,7 @@ static QByteArray convertToUtf8(QByteArray const & input, QString const & charSe void BrowserTab::on_requestComplete(const QByteArray &ref_data, const QString &mime_text) { this->ui->media_browser->stopPlaying(); + this->network_timeout_timer.stop(); QByteArray data = ref_data; MimeType mime = MimeParser::parse(mime_text); @@ -516,6 +534,8 @@ void BrowserTab::on_redirected(const QUrl &uri, bool is_permanent) { Q_UNUSED(is_permanent); + this->network_timeout_timer.stop(); + if (redirection_count >= global_options.max_redirections) { setErrorMessage(QString("Too many consecutive redirections. The last redirection would have redirected you to:\r\n%1").arg(uri.toString(QUrl::FullyEncoded))); @@ -706,6 +726,9 @@ void BrowserTab::on_requestProgress(qint64 transferred) this->current_stats.mime_type = MimeType { }; this->current_stats.loading_time = this->timer.elapsed(); emit this->fileLoaded(this->current_stats); + + this->network_timeout_timer.stop(); + this->network_timeout_timer.start(global_options.network_timeout); } void BrowserTab::on_back_button_clicked() @@ -841,6 +864,8 @@ bool BrowserTab::startRequest(const QUrl &url, ProtocolHandler::RequestOptions o this->current_location = url; this->ui->url_bar->setText(url.toString(QUrl::FormattingOptions(QUrl::FullyEncoded))); + this->network_timeout_timer.start(global_options.network_timeout); + return this->current_handler->startRequest(url, options); } diff --git a/src/browsertab.hpp b/src/browsertab.hpp index 47a9871..41126f2 100644 --- a/src/browsertab.hpp +++ b/src/browsertab.hpp @@ -8,6 +8,7 @@ #include <QTextDocument> #include <QNetworkAccessManager> #include <QElapsedTimer> +#include <QTimer> #include "documentoutlinemodel.hpp" #include "tabbrowsinghistory.hpp" @@ -106,6 +107,8 @@ private: // network slots void on_networkError(ProtocolHandler::NetworkError error, QString const & reason); void on_certificateRequired(QString const & info); + void on_networkTimeout(); + private: void setErrorMessage(QString const & msg); @@ -155,6 +158,8 @@ public: bool is_internal_location; DocumentStats current_stats; + + QTimer network_timeout_timer; }; #endif // BROWSERTAB_HPP diff --git a/src/kristall.hpp b/src/kristall.hpp index dd83498..8949632 100644 --- a/src/kristall.hpp +++ b/src/kristall.hpp @@ -37,6 +37,9 @@ struct GenericSettings int max_redirections = 5; RedirectionWarning redirection_policy = WarnOnHostChange; + // 5 seconds network timeout + int network_timeout = 5000; + void load(QSettings & settings); void save(QSettings & settings) const; }; |
