HTTP Example: Add proxy handling

If a proxy is configured on the system then we will request credentials
if needed.

Task-number: QTBUG-108874
Fixes: QTBUG-106245
Pick-to: 6.5
Change-Id: Icbea491492cde4634421b1a1e722a3768d56dec8
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Mårten Nordheim 2023-01-11 18:19:39 +01:00
parent d18b5195c1
commit 5006fddd33
2 changed files with 32 additions and 0 deletions

View File

@ -51,6 +51,10 @@ HttpWindow::HttpWindow(QWidget *parent)
connect(&qnam, &QNetworkAccessManager::authenticationRequired, connect(&qnam, &QNetworkAccessManager::authenticationRequired,
this, &HttpWindow::slotAuthenticationRequired); this, &HttpWindow::slotAuthenticationRequired);
//! [qnam-auth-required-1] //! [qnam-auth-required-1]
#if QT_CONFIG(networkproxy)
connect(&qnam, &QNetworkAccessManager::proxyAuthenticationRequired,
this, &HttpWindow::slotProxyAuthenticationRequired);
#endif
QFormLayout *formLayout = new QFormLayout; QFormLayout *formLayout = new QFormLayout;
urlLineEdit->setClearButtonEnabled(true); urlLineEdit->setClearButtonEnabled(true);
@ -277,3 +281,25 @@ void HttpWindow::sslErrors(const QList<QSslError> &errors)
} }
//! [sslerrors-2] //! [sslerrors-2]
#endif #endif
#if QT_CONFIG(networkproxy)
void HttpWindow::slotProxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
{
QDialog authenticationDialog;
Ui::Dialog ui;
ui.setupUi(&authenticationDialog);
authenticationDialog.adjustSize();
ui.siteDescription->setText(tr("A network proxy at %1 is requesting credentials for realm: %2")
.arg(proxy.hostName(), authenticator->realm()));
// If the user passed credentials in the URL to http_proxy or similar they may be available to
// us. Otherwise this will just leave the fields empty
ui.userEdit->setText(proxy.user());
ui.passwordEdit->setText(proxy.password());
if (authenticationDialog.exec() == QDialog::Accepted) {
authenticator->setUser(ui.userEdit->text());
authenticator->setPassword(ui.passwordEdit->text());
}
}
#endif

View File

@ -19,6 +19,9 @@ class QSslError;
class QAuthenticator; class QAuthenticator;
class QNetworkReply; class QNetworkReply;
class QCheckBox; class QCheckBox;
#if QT_CONFIG(networkproxy)
class QNetworkProxy;
#endif
QT_END_NAMESPACE QT_END_NAMESPACE
@ -52,6 +55,9 @@ private slots:
#if QT_CONFIG(ssl) #if QT_CONFIG(ssl)
void sslErrors(const QList<QSslError> &errors); void sslErrors(const QList<QSslError> &errors);
#endif #endif
#if QT_CONFIG(networkproxy)
void slotProxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
#endif
private: private:
std::unique_ptr<QFile> openFileForWrite(const QString &fileName); std::unique_ptr<QFile> openFileForWrite(const QString &fileName);