diff options
-rw-r--r-- | src/core/api/qwebengineurlrequestjob.cpp | 9 | ||||
-rw-r--r-- | src/core/api/qwebengineurlrequestjob.h | 1 | ||||
-rw-r--r-- | src/core/net/url_request_custom_job.cpp | 20 | ||||
-rw-r--r-- | src/core/net/url_request_custom_job_delegate.cpp | 11 | ||||
-rw-r--r-- | src/core/net/url_request_custom_job_delegate.h | 6 | ||||
-rw-r--r-- | src/core/net/url_request_custom_job_proxy.cpp | 5 | ||||
-rw-r--r-- | src/core/net/url_request_custom_job_proxy.h | 2 | ||||
-rw-r--r-- | tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp | 61 |
8 files changed, 107 insertions, 8 deletions
diff --git a/src/core/api/qwebengineurlrequestjob.cpp b/src/core/api/qwebengineurlrequestjob.cpp index c028a1167..1c262d3b0 100644 --- a/src/core/api/qwebengineurlrequestjob.cpp +++ b/src/core/api/qwebengineurlrequestjob.cpp @@ -125,6 +125,15 @@ QUrl QWebEngineUrlRequestJob::initiator() const } /*! + \since 5.12 + Returns any HTTP headers added to the request. +*/ +const QMap<QByteArray, QByteArray> &QWebEngineUrlRequestJob::requestHeaders() const +{ + return d_ptr->requestHeaders(); +} + +/*! Replies to the request with \a device and the MIME type \a contentType. The user has to be aware that \a device will be used on another thread diff --git a/src/core/api/qwebengineurlrequestjob.h b/src/core/api/qwebengineurlrequestjob.h index 7ce8be7ec..55ec7c6d2 100644 --- a/src/core/api/qwebengineurlrequestjob.h +++ b/src/core/api/qwebengineurlrequestjob.h @@ -73,6 +73,7 @@ public: QUrl requestUrl() const; QByteArray requestMethod() const; QUrl initiator() const; + const QMap<QByteArray, QByteArray> &requestHeaders() const; void reply(const QByteArray &contentType, QIODevice *device); void fail(Error error); diff --git a/src/core/net/url_request_custom_job.cpp b/src/core/net/url_request_custom_job.cpp index c69fb1808..f4dfc6ae9 100644 --- a/src/core/net/url_request_custom_job.cpp +++ b/src/core/net/url_request_custom_job.cpp @@ -39,10 +39,13 @@ #include "url_request_custom_job.h" #include "url_request_custom_job_proxy.h" +#include "../type_conversion.h" + #include "content/public/browser/browser_thread.h" #include "net/base/io_buffer.h" #include <QIODevice> +#include <QMap> using namespace net; @@ -76,9 +79,22 @@ URLRequestCustomJob::~URLRequestCustomJob() void URLRequestCustomJob::Start() { DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + HttpRequestHeaders requestHeaders = request()->extra_request_headers(); + QMap<QByteArray, QByteArray> headers; + net::HttpRequestHeaders::Iterator it(requestHeaders); + while (it.GetNext()) + headers.insert(toQByteArray(it.name()), toQByteArray(it.value())); + if (!request()->referrer().empty()) + headers.insert(QByteArray("Referer"), toQByteArray(request()->referrer())); + + // TODO: handle UploadDataStream, for instance using a QIODevice wrapper. + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, - base::Bind(&URLRequestCustomJobProxy::initialize, - m_proxy, request()->url(), request()->method(), request()->initiator())); + base::Bind(&URLRequestCustomJobProxy::initialize, m_proxy, + request()->url(), + request()->method(), + request()->initiator(), + headers)); } void URLRequestCustomJob::Kill() diff --git a/src/core/net/url_request_custom_job_delegate.cpp b/src/core/net/url_request_custom_job_delegate.cpp index 338bd7137..4ed6a2484 100644 --- a/src/core/net/url_request_custom_job_delegate.cpp +++ b/src/core/net/url_request_custom_job_delegate.cpp @@ -51,11 +51,13 @@ namespace QtWebEngineCore { URLRequestCustomJobDelegate::URLRequestCustomJobDelegate(URLRequestCustomJobProxy *proxy, const QUrl &url, const QByteArray &method, - const QUrl &initiatorOrigin) + const QUrl &initiatorOrigin, + const QMap<QByteArray, QByteArray> &headers) : m_proxy(proxy), m_request(url), m_method(method), - m_initiatorOrigin(initiatorOrigin) + m_initiatorOrigin(initiatorOrigin), + m_requestHeaders(headers) { } @@ -78,6 +80,11 @@ QUrl URLRequestCustomJobDelegate::initiator() const return m_initiatorOrigin; } +const QMap<QByteArray, QByteArray> &URLRequestCustomJobDelegate::requestHeaders() const +{ + return m_requestHeaders; +} + void URLRequestCustomJobDelegate::reply(const QByteArray &contentType, QIODevice *device) { if (device) diff --git a/src/core/net/url_request_custom_job_delegate.h b/src/core/net/url_request_custom_job_delegate.h index caabfcf99..9de0224f9 100644 --- a/src/core/net/url_request_custom_job_delegate.h +++ b/src/core/net/url_request_custom_job_delegate.h @@ -54,6 +54,7 @@ #include "base/memory/ref_counted.h" #include "qtwebenginecoreglobal_p.h" +#include <QMap> #include <QObject> #include <QUrl> @@ -80,6 +81,7 @@ public: QUrl url() const; QByteArray method() const; QUrl initiator() const; + const QMap<QByteArray, QByteArray> &requestHeaders() const; void reply(const QByteArray &contentType, QIODevice *device); void redirect(const QUrl& url); @@ -93,13 +95,15 @@ private: URLRequestCustomJobDelegate(URLRequestCustomJobProxy *proxy, const QUrl &url, const QByteArray &method, - const QUrl &initiatorOrigin); + const QUrl &initiatorOrigin, + const QMap<QByteArray, QByteArray> &requestHeaders); friend class URLRequestCustomJobProxy; scoped_refptr<URLRequestCustomJobProxy> m_proxy; QUrl m_request; QByteArray m_method; QUrl m_initiatorOrigin; + const QMap<QByteArray, QByteArray> m_requestHeaders; }; } // namespace diff --git a/src/core/net/url_request_custom_job_proxy.cpp b/src/core/net/url_request_custom_job_proxy.cpp index 5280318ad..10ca4d0b1 100644 --- a/src/core/net/url_request_custom_job_proxy.cpp +++ b/src/core/net/url_request_custom_job_proxy.cpp @@ -152,7 +152,7 @@ void URLRequestCustomJobProxy::readyRead() m_job->notifyReadyRead(); } -void URLRequestCustomJobProxy::initialize(GURL url, std::string method, base::Optional<url::Origin> initiator) +void URLRequestCustomJobProxy::initialize(GURL url, std::string method, base::Optional<url::Origin> initiator, QMap<QByteArray, QByteArray> headers) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); Q_ASSERT(!m_delegate); @@ -169,7 +169,8 @@ void URLRequestCustomJobProxy::initialize(GURL url, std::string method, base::Op if (schemeHandler) { m_delegate = new URLRequestCustomJobDelegate(this, toQt(url), QByteArray::fromStdString(method), - initiatorOrigin); + initiatorOrigin, + headers); QWebEngineUrlRequestJob *requestJob = new QWebEngineUrlRequestJob(m_delegate); schemeHandler->requestStarted(requestJob); } diff --git a/src/core/net/url_request_custom_job_proxy.h b/src/core/net/url_request_custom_job_proxy.h index 3986fe119..1f4494945 100644 --- a/src/core/net/url_request_custom_job_proxy.h +++ b/src/core/net/url_request_custom_job_proxy.h @@ -72,7 +72,7 @@ public: void abort(); void fail(int error); void release(); - void initialize(GURL url, std::string method, base::Optional<url::Origin> initiatorOrigin); + void initialize(GURL url, std::string method, base::Optional<url::Origin> initiatorOrigin, QMap<QByteArray, QByteArray> headers); void readyRead(); // IO thread owned: diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index a04a1ac6f..7e1796ffb 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -29,6 +29,7 @@ #include "../util.h" #include <QtCore/qbuffer.h> #include <QtTest/QtTest> +#include <QtWebEngineCore/qwebengineurlrequestinterceptor.h> #include <QtWebEngineCore/qwebengineurlrequestjob.h> #include <QtWebEngineCore/qwebengineurlschemehandler.h> #include <QtWebEngineWidgets/qwebengineprofile.h> @@ -52,6 +53,7 @@ private Q_SLOTS: void urlSchemeHandlerFailRequest(); void urlSchemeHandlerFailOnRead(); void urlSchemeHandlerStreaming(); + void urlSchemeHandlerRequestHeaders(); void customUserAgent(); void httpAcceptLanguage(); void downloadItem(); @@ -443,6 +445,65 @@ void tst_QWebEngineProfile::urlSchemeHandlerStreaming() QCOMPARE(toPlainTextSync(view.page()), QString::fromLatin1(result)); } +class ExtraHeaderInterceptor : public QWebEngineUrlRequestInterceptor +{ +public: + ExtraHeaderInterceptor() { } + + void setExtraHeader(const QByteArray &key, const QByteArray &value) + { + m_extraKey = key; + m_extraValue = value; + } + + void interceptRequest(QWebEngineUrlRequestInfo &info) override + { + info.setHttpHeader(m_extraKey, m_extraValue); + } + + QByteArray m_extraKey; + QByteArray m_extraValue; +}; + +class RequestHeadersUrlSchemeHandler : public ReplyingUrlSchemeHandler +{ +public: + void setExpectedHeader(const QByteArray &key, const QByteArray &value) + { + m_expectedKey = key; + m_expectedValue = value; + } + void requestStarted(QWebEngineUrlRequestJob *job) override + { + const auto requestHeaders = job->requestHeaders(); + QVERIFY(requestHeaders.contains(m_expectedKey)); + QCOMPARE(requestHeaders.value(m_expectedKey), m_expectedValue); + ReplyingUrlSchemeHandler::requestStarted(job); + } + QByteArray m_expectedKey; + QByteArray m_expectedValue; +}; + +void tst_QWebEngineProfile::urlSchemeHandlerRequestHeaders() +{ + RequestHeadersUrlSchemeHandler handler; + ExtraHeaderInterceptor interceptor; + + interceptor.setExtraHeader("Hello", "World"); + handler.setExpectedHeader("Hello", "World"); + + QWebEngineProfile profile; + profile.setRequestInterceptor(&interceptor); + profile.installUrlSchemeHandler("myscheme", &handler); + + QWebEngineView view; + QSignalSpy loadFinishedSpy(&view, SIGNAL(loadFinished(bool))); + view.setPage(new QWebEnginePage(&profile, &view)); + view.load(QUrl(QStringLiteral("myscheme://whatever"))); + QVERIFY(loadFinishedSpy.wait()); +} + + void tst_QWebEngineProfile::customUserAgent() { QString defaultUserAgent = QWebEngineProfile::defaultProfile()->httpUserAgent(); |