summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJüri Valdmann <juri.valdmann@qt.io>2019-09-24 17:46:19 +0200
committerJüri Valdmann <juri.valdmann@qt.io>2019-09-25 15:56:35 +0200
commite31acc86e991d6f83bd56eebb7371914fc9ca5ac (patch)
tree349d3cde4ec0083b5fbd21245b6506478848bd03
parent3db2cf8b8e3cf98ed03ab7220e4006946b741973 (diff)
Fix XMLHttpRequest status with custom schemes
Emulate a HTTP 200 OK response when QWebEngineUrlRequestJob::reply is called so that an XMLHttpRequest to a custom scheme gets a proper 'status' property. Note that calling QWebEngineUrlRequestJob::fail on the other hand emulates a network error, meaning it triggers the 'error' event in XMLHttpRequest. Fixes: QTBUG-78316 Change-Id: Ia7249a5b72533aa7e2ed4ef8f62d8b6b89820cc1 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r--src/core/net/url_request_custom_job.cpp18
-rw-r--r--src/core/net/url_request_custom_job.h1
-rw-r--r--tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp110
3 files changed, 129 insertions, 0 deletions
diff --git a/src/core/net/url_request_custom_job.cpp b/src/core/net/url_request_custom_job.cpp
index cba9b4dc5..d8442707e 100644
--- a/src/core/net/url_request_custom_job.cpp
+++ b/src/core/net/url_request_custom_job.cpp
@@ -40,10 +40,12 @@
#include "url_request_custom_job.h"
#include "url_request_custom_job_proxy.h"
+#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/io_buffer.h"
+#include "net/http/http_util.h"
#include <QIODevice>
@@ -136,6 +138,22 @@ bool URLRequestCustomJob::GetCharset(std::string* charset)
return false;
}
+void URLRequestCustomJob::GetResponseInfo(HttpResponseInfo* info)
+{
+ if (m_error)
+ return;
+
+ std::string headers;
+ if (m_redirect.is_valid()) {
+ headers += "HTTP/1.1 303 See Other\n";
+ headers += base::StringPrintf("Location: %s\n", m_redirect.spec().c_str());
+ } else {
+ headers += "HTTP/1.1 200 OK\n";
+ }
+
+ info->headers = new HttpResponseHeaders(HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size()));
+}
+
bool URLRequestCustomJob::IsRedirectResponse(GURL* location, int* http_status_code, bool* /*insecure_scheme_was_upgraded*/)
{
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
diff --git a/src/core/net/url_request_custom_job.h b/src/core/net/url_request_custom_job.h
index 16be76f29..c800d1595 100644
--- a/src/core/net/url_request_custom_job.h
+++ b/src/core/net/url_request_custom_job.h
@@ -64,6 +64,7 @@ public:
int ReadRawData(net::IOBuffer *buf, int buf_size) override;
bool GetMimeType(std::string *mimeType) const override;
bool GetCharset(std::string *charset) override;
+ void GetResponseInfo(net::HttpResponseInfo* info) override;
bool IsRedirectResponse(GURL* location, int* http_status_code, bool* insecure_scheme_was_upgraded) override;
protected:
diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp
index 8b75067ee..921831700 100644
--- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp
+++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp
@@ -40,6 +40,12 @@
#include <QtWebEngineWidgets/qwebengineview.h>
#include <QtWebEngineWidgets/qwebenginedownloaditem.h>
+#if QT_CONFIG(webengine_webchannel)
+#include <QWebChannel>
+#endif
+
+#include <map>
+
class tst_QWebEngineProfile : public QObject
{
Q_OBJECT
@@ -58,6 +64,7 @@ private Q_SLOTS:
void urlSchemeHandlerStreaming();
void urlSchemeHandlerRequestHeaders();
void urlSchemeHandlerInstallation();
+ void urlSchemeHandlerXhrStatus();
void customUserAgent();
void httpAcceptLanguage();
void downloadItem();
@@ -568,6 +575,109 @@ void tst_QWebEngineProfile::urlSchemeHandlerInstallation()
profile.removeUrlScheme("tst");
}
+#if QT_CONFIG(webengine_webchannel)
+class XhrStatusHost : public QObject
+{
+ Q_OBJECT
+public:
+ std::map<QUrl, int> requests;
+
+ bool isReady()
+ {
+ static const auto sig = QMetaMethod::fromSignal(&XhrStatusHost::load);
+ return isSignalConnected(sig);
+ }
+
+Q_SIGNALS:
+ void load(QUrl url);
+
+public Q_SLOTS:
+ void loadFinished(QUrl url, int status)
+ {
+ requests[url] = status;
+ }
+
+private:
+};
+
+class XhrStatusUrlSchemeHandler : public QWebEngineUrlSchemeHandler
+{
+public:
+ void requestStarted(QWebEngineUrlRequestJob *job)
+ {
+ QString path = job->requestUrl().path();
+ if (path == "/") {
+ QBuffer *buffer = new QBuffer(job);
+ buffer->open(QBuffer::ReadWrite);
+ buffer->write(QByteArrayLiteral(R"(
+<html>
+ <body>
+ <script src="qwebchannel.js"></script>
+ <script>
+ new QWebChannel(qt.webChannelTransport, (channel) => {
+ const host = channel.objects.host;
+ host.load.connect((url) => {
+ const xhr = new XMLHttpRequest();
+ xhr.onload = () => { host.loadFinished(url, xhr.status); };
+ xhr.onerror = () => { host.loadFinished(url, -1); };
+ xhr.open("GET", url, true);
+ xhr.send();
+ });
+ });
+ </script>
+ </body>
+</html>
+)"));
+ buffer->seek(0);
+ job->reply("text/html", buffer);
+ } else if (path == "/qwebchannel.js") {
+ QFile *file = new QFile(":/qtwebchannel/qwebchannel.js", job);
+ file->open(QFile::ReadOnly);
+ job->reply("application/javascript", file);
+ } else if (path == "/ok") {
+ QBuffer *buffer = new QBuffer(job);
+ buffer->buffer() = QByteArrayLiteral("ok");
+ job->reply("text/plain", buffer);
+ } else if (path == "/redirect") {
+ QUrl url = job->requestUrl();
+ url.setPath("/ok");
+ job->redirect(url);
+ } else if (path == "/fail") {
+ job->fail(QWebEngineUrlRequestJob::RequestFailed);
+ } else {
+ job->fail(QWebEngineUrlRequestJob::UrlNotFound);
+ }
+ }
+};
+#endif
+
+void tst_QWebEngineProfile::urlSchemeHandlerXhrStatus()
+{
+#if QT_CONFIG(webengine_webchannel)
+ XhrStatusUrlSchemeHandler handler;
+ XhrStatusHost host;
+ QWebEngineProfile profile;
+ QWebEnginePage page(&profile);
+ QWebChannel channel;
+ channel.registerObject("host", &host);
+ profile.installUrlSchemeHandler("aviancarrier", &handler);
+ page.setWebChannel(&channel);
+ page.load(QUrl("aviancarrier:/"));
+ QTRY_VERIFY(host.isReady());
+ host.load(QUrl("aviancarrier:/ok"));
+ host.load(QUrl("aviancarrier:/redirect"));
+ host.load(QUrl("aviancarrier:/fail"));
+ host.load(QUrl("aviancarrier:/notfound"));
+ QTRY_COMPARE(host.requests.size(), 4u);
+ QCOMPARE(host.requests[QUrl("aviancarrier:/ok")], 200);
+ QCOMPARE(host.requests[QUrl("aviancarrier:/redirect")], 200);
+ QCOMPARE(host.requests[QUrl("aviancarrier:/fail")], -1);
+ QCOMPARE(host.requests[QUrl("aviancarrier:/notfound")], -1);
+#else
+ QSKIP("No QtWebChannel");
+#endif
+}
+
void tst_QWebEngineProfile::customUserAgent()
{
QString defaultUserAgent = QWebEngineProfile::defaultProfile()->httpUserAgent();