summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorMÃ¥rten Nordheim <marten.nordheim@qt.io>2017-10-10 15:54:04 +0200
committerTimur Pocheptsov <timur.pocheptsov@qt.io>2017-10-17 14:22:22 +0000
commit8a39384e907e830c907f73009f498c486b22bd20 (patch)
tree0b3ca1e7ba7fe3915127d6a8165d2821b536f8ed /src/network
parent377d2502e3d3116c4cbe7398c2aa6f4afbe32851 (diff)
Open a session during redirects when needed
In some cases when a session isn't needed (i.e. for localhost), the session is not opened at all. If a program (e.g. our tests) redirects from localhost to a different system (e.g. the qt network test servers, or the internet) it will wait for a session forever. So, we need to check if a session is needed for the redirect-target and then open one. It is usually opened in QNetworkReplyHttpImplPrivate::_q_startOperation Change-Id: Id3b78182a3fb3f63f0235ecb1fb665df8bd0c4ca Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/network')
-rw-r--r--src/network/access/qnetworkreplyhttpimpl.cpp78
-rw-r--r--src/network/access/qnetworkreplyhttpimpl_p.h2
2 files changed, 59 insertions, 21 deletions
diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp
index fa60eb3564..9756b43108 100644
--- a/src/network/access/qnetworkreplyhttpimpl.cpp
+++ b/src/network/access/qnetworkreplyhttpimpl.cpp
@@ -165,6 +165,16 @@ static QHash<QByteArray, QByteArray> parseHttpOptionHeader(const QByteArray &hea
}
}
+#if QT_CONFIG(bearermanagement)
+static bool isSessionNeeded(const QUrl &url)
+{
+ // Connections to the local machine does not require a session
+ QString host = url.host().toLower();
+ return !QHostAddress(host).isLoopback() && host != QLatin1String("localhost")
+ && host != QSysInfo::machineHostName().toLower();
+}
+#endif // bearer management
+
QNetworkReplyHttpImpl::QNetworkReplyHttpImpl(QNetworkAccessManager* const manager,
const QNetworkRequest& request,
QNetworkAccessManager::Operation& operation,
@@ -1189,6 +1199,24 @@ void QNetworkReplyHttpImplPrivate::followRedirect()
if (managerPrivate->thread)
managerPrivate->thread->disconnect();
+#if QT_CONFIG(bearermanagement)
+ // If the original request didn't need a session (i.e. it was to localhost)
+ // then we might not have a session open, to which to redirect, if the
+ // new URL is remote. When this happens, we need to open the session now:
+ if (managerPrivate && isSessionNeeded(url)) {
+ if (auto session = managerPrivate->getNetworkSession()) {
+ if (session->state() != QNetworkSession::State::Connected || !session->isOpen()) {
+ startWaitForSession(session);
+ // Need to set 'request' to the redirectRequest so that when QNAM restarts
+ // the request after the session starts it will not repeat the previous request.
+ request = redirectRequest;
+ // Return now, QNAM will start the request when the session has started.
+ return;
+ }
+ }
+ }
+#endif // bearer management
+
QMetaObject::invokeMethod(q, "start", Qt::QueuedConnection,
Q_ARG(QNetworkRequest, redirectRequest));
}
@@ -1770,10 +1798,8 @@ bool QNetworkReplyHttpImplPrivate::start(const QNetworkRequest &newHttpRequest)
}
// This is not ideal.
- const QString host = url.host();
- if (host == QLatin1String("localhost") ||
- QHostAddress(host).isLoopback()) {
- // Don't need an open session for localhost access.
+ if (!isSessionNeeded(url)) {
+ // Don't need to check for an open session if we don't need one.
postRequest(newHttpRequest);
return true;
}
@@ -1797,6 +1823,32 @@ bool QNetworkReplyHttpImplPrivate::start(const QNetworkRequest &newHttpRequest)
#endif
}
+bool QNetworkReplyHttpImplPrivate::startWaitForSession(QSharedPointer<QNetworkSession> &session)
+{
+ Q_Q(QNetworkReplyHttpImpl);
+ state = WaitingForSession;
+
+ if (session) {
+ QObject::connect(session.data(), SIGNAL(error(QNetworkSession::SessionError)),
+ q, SLOT(_q_networkSessionFailed()), Qt::QueuedConnection);
+
+ if (!session->isOpen()) {
+ QVariant isBackground = request.attribute(QNetworkRequest::BackgroundRequestAttribute,
+ QVariant::fromValue(false));
+ session->setSessionProperty(QStringLiteral("ConnectInBackground"), isBackground);
+ session->open();
+ }
+ return true;
+ }
+ const Qt::ConnectionType connection = synchronous ? Qt::DirectConnection : Qt::QueuedConnection;
+ qWarning("Backend is waiting for QNetworkSession to connect, but there is none!");
+ QMetaObject::invokeMethod(q, "_q_error", connection,
+ Q_ARG(QNetworkReply::NetworkError, QNetworkReply::NetworkSessionFailedError),
+ Q_ARG(QString, QCoreApplication::translate("QNetworkReply", "Network session error.")));
+ QMetaObject::invokeMethod(q, "_q_finished", connection);
+ return false;
+}
+
void QNetworkReplyHttpImplPrivate::_q_startOperation()
{
Q_Q(QNetworkReplyHttpImpl);
@@ -1824,24 +1876,8 @@ void QNetworkReplyHttpImplPrivate::_q_startOperation()
// backend failed to start because the session state is not Connected.
// QNetworkAccessManager will call reply->backend->start() again for us when the session
// state changes.
- state = WaitingForSession;
-
- if (session) {
- QObject::connect(session.data(), SIGNAL(error(QNetworkSession::SessionError)),
- q, SLOT(_q_networkSessionFailed()), Qt::QueuedConnection);
-
- if (!session->isOpen()) {
- session->setSessionProperty(QStringLiteral("ConnectInBackground"), isBackground);
- session->open();
- }
- } else {
- qWarning("Backend is waiting for QNetworkSession to connect, but there is none!");
- QMetaObject::invokeMethod(q, "_q_error", synchronous ? Qt::DirectConnection : Qt::QueuedConnection,
- Q_ARG(QNetworkReply::NetworkError, QNetworkReply::NetworkSessionFailedError),
- Q_ARG(QString, QCoreApplication::translate("QNetworkReply", "Network session error.")));
- QMetaObject::invokeMethod(q, "_q_finished", synchronous ? Qt::DirectConnection : Qt::QueuedConnection);
+ if (!startWaitForSession(session))
return;
- }
} else if (session) {
QObject::connect(session.data(), SIGNAL(stateChanged(QNetworkSession::State)),
q, SLOT(_q_networkSessionStateChanged(QNetworkSession::State)),
diff --git a/src/network/access/qnetworkreplyhttpimpl_p.h b/src/network/access/qnetworkreplyhttpimpl_p.h
index 9383149124..1b702bd2ec 100644
--- a/src/network/access/qnetworkreplyhttpimpl_p.h
+++ b/src/network/access/qnetworkreplyhttpimpl_p.h
@@ -160,6 +160,8 @@ signals:
class QNetworkReplyHttpImplPrivate: public QNetworkReplyPrivate
{
+ bool startWaitForSession(QSharedPointer<QNetworkSession> &session);
+
public:
static QHttpNetworkRequest::Priority convert(const QNetworkRequest::Priority& prio);