diff --git a/src/core/api/qwebengineurlrequestinfo.cpp b/src/core/api/qwebengineurlrequestinfo.cpp index e8ce65be3d65006cbdf0fcecca279b6ecab048a5..0aab4aa1b1e5639a83f535270837ef362a76f4a1 100644 --- a/src/core/api/qwebengineurlrequestinfo.cpp +++ b/src/core/api/qwebengineurlrequestinfo.cpp @@ -126,6 +126,7 @@ QWebEngineUrlRequestInfoPrivate::QWebEngineUrlRequestInfoPrivate(QWebEngineUrlRe , url(u) , firstPartyUrl(fpu) , method(m) + , changed(false) { } @@ -241,6 +242,12 @@ QByteArray QWebEngineUrlRequestInfo::requestMethod() const return d->method; } +bool QWebEngineUrlRequestInfo::changed() const +{ + Q_D(const QWebEngineUrlRequestInfo); + return d->changed; +} + /*! Redirects this request to \a url. It is only possible to redirect requests that do not have payload data, such as GET requests. @@ -249,6 +256,7 @@ QByteArray QWebEngineUrlRequestInfo::requestMethod() const void QWebEngineUrlRequestInfo::redirect(const QUrl &url) { Q_D(QWebEngineUrlRequestInfo); + d->changed = true; d->url = url; } @@ -261,6 +269,7 @@ void QWebEngineUrlRequestInfo::redirect(const QUrl &url) void QWebEngineUrlRequestInfo::block(bool shouldBlock) { Q_D(QWebEngineUrlRequestInfo); + d->changed = true; d->shouldBlockRequest = shouldBlock; } @@ -271,6 +280,7 @@ void QWebEngineUrlRequestInfo::block(bool shouldBlock) void QWebEngineUrlRequestInfo::setExtraHeader(const QByteArray &name, const QByteArray &value) { Q_D(QWebEngineUrlRequestInfo); + d->changed = true; d->extraHeaders.insert(name, value); } diff --git a/src/core/api/qwebengineurlrequestinfo.h b/src/core/api/qwebengineurlrequestinfo.h index e6e225051449869e041279d0b674d3b5dd329aac..1443665af0b5b39f4b3ab5fc01e5d1da7cd5409b 100644 --- a/src/core/api/qwebengineurlrequestinfo.h +++ b/src/core/api/qwebengineurlrequestinfo.h @@ -88,6 +88,7 @@ public: QUrl requestUrl() const; QUrl firstPartyUrl() const; QByteArray requestMethod() const; + bool changed() const; void block(bool shouldBlock); void redirect(const QUrl &url); diff --git a/src/core/api/qwebengineurlrequestinfo_p.h b/src/core/api/qwebengineurlrequestinfo_p.h index 1b1279d2792546700b8600dcea4cf37e2dfe628a..df5f18d6e9aa24941cfe9d1400f3f6596d301f18 100644 --- a/src/core/api/qwebengineurlrequestinfo_p.h +++ b/src/core/api/qwebengineurlrequestinfo_p.h @@ -68,6 +68,7 @@ public: QUrl url; QUrl firstPartyUrl; const QByteArray method; + bool changed; QHash<QByteArray, QByteArray> extraHeaders; QWebEngineUrlRequestInfo *q_ptr; diff --git a/src/core/api/qwebengineurlrequestinterceptor.h b/src/core/api/qwebengineurlrequestinterceptor.h index 9a632c3e07535b69f679910498a528b5b3ba6ed5..372ee90669279b7ac3b9a158e82cf9fae7458853 100644 --- a/src/core/api/qwebengineurlrequestinterceptor.h +++ b/src/core/api/qwebengineurlrequestinterceptor.h @@ -57,7 +57,7 @@ public: { } - virtual bool interceptRequest(QWebEngineUrlRequestInfo &info) = 0; + virtual void interceptRequest(QWebEngineUrlRequestInfo &info) = 0; }; QT_END_NAMESPACE diff --git a/src/core/network_delegate_qt.cpp b/src/core/network_delegate_qt.cpp index 3f67e7c0d39941f7c36eb5458d648d522ac76796..6fdabcd4c456b5ba305ab7a5feff69b92022ad58 100644 --- a/src/core/network_delegate_qt.cpp +++ b/src/core/network_delegate_qt.cpp @@ -109,7 +109,8 @@ int NetworkDelegateQt::OnBeforeURLRequest(net::URLRequest *request, const net::C , toQt(request->first_party_for_cookies()) , QByteArray::fromStdString(request->method())); QWebEngineUrlRequestInfo requestInfo(infoPrivate); - if (interceptor->interceptRequest(requestInfo)) { + interceptor->interceptRequest(requestInfo); + if (requestInfo.changed()) { int result = infoPrivate->shouldBlockRequest ? net::ERR_ABORTED : net::OK; if (qUrl != infoPrivate->url) diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp index 4a261c46ba1e53a1c5d316c5b10966925ecb4e24..c4461f1c17776ec17430a9e8acbda1dadfde8c0c 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp @@ -100,15 +100,14 @@ public: QList<QUrl> firstPartyUrls; bool shouldIntercept; - bool interceptRequest(QWebEngineUrlRequestInfo &info) override + void interceptRequest(QWebEngineUrlRequestInfo &info) override { info.block(info.requestMethod() != QByteArrayLiteral("GET")); - if (info.requestUrl().toString().endsWith(QLatin1String("__placeholder__"))) + if (shouldIntercept && info.requestUrl().toString().endsWith(QLatin1String("__placeholder__"))) info.redirect(QUrl("qrc:///resources/content.html")); observedUrls.append(info.requestUrl()); firstPartyUrls.append(info.firstPartyUrl()); - return shouldIntercept; } TestRequestInterceptor(bool intercept) : shouldIntercept(intercept) @@ -162,11 +161,10 @@ class LocalhostContentProvider : public QWebEngineUrlRequestInterceptor public: LocalhostContentProvider() { } - bool interceptRequest(QWebEngineUrlRequestInfo &info) override + void interceptRequest(QWebEngineUrlRequestInfo &info) override { requestedUrls.append(info.requestUrl()); info.redirect(QUrl("data:text/html,<p>hello")); - return true; } QList<QUrl> requestedUrls;