summaryrefslogtreecommitdiffstats
path: root/src/core/web_contents_adapter.cpp
diff options
context:
space:
mode:
authorViktor Engelmann <viktor.engelmann@qt.io>2016-08-09 11:51:19 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2017-01-11 10:33:07 +0000
commitc4e1aa2c4fff93c71eed3f8115170f314d969234 (patch)
tree43a1937891dc034de05e35de0066a00af8449bc3 /src/core/web_contents_adapter.cpp
parent4804e331304c5bcb79ebc785485793e5f1d8759e (diff)
Add methods to issue various types of HTTP requests
Added class QWebEngineHttpRequest, which describes a GET or POST HTTP Request. Also added overloads of method "load" to QWebEngineView, QWebEnginePage and WebContentsAdapter, which issue such a request. These can be used for example to simulate form-submissions. Task-number: QTBUG-53314 Task-number: QTBUG-53372 Change-Id: I85ac8cdd3d1557905b35e3172b922aba356d1c41 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/web_contents_adapter.cpp')
-rw-r--r--src/core/web_contents_adapter.cpp51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index 030d3ea89..228c37010 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -77,6 +77,7 @@
#include "content/public/common/page_state.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/renderer_preferences.h"
+#include "content/public/common/resource_request_body.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/web_preferences.h"
#include "third_party/WebKit/public/web/WebFindOptions.h"
@@ -499,6 +500,12 @@ void WebContentsAdapter::reloadAndBypassCache()
void WebContentsAdapter::load(const QUrl &url)
{
+ QWebEngineHttpRequest request(url);
+ load(request);
+}
+
+void WebContentsAdapter::load(const QWebEngineHttpRequest &request)
+{
// The situation can occur when relying on the editingFinished signal in QML to set the url
// of the WebView.
// When enter is pressed, onEditingFinished fires and the url of the webview is set, which
@@ -513,21 +520,55 @@ void WebContentsAdapter::load(const QUrl &url)
Q_UNUSED(guard);
Q_D(WebContentsAdapter);
- GURL gurl = toGurl(url);
+ GURL gurl = toGurl(request.url());
// Add URL scheme if missing from view-source URL.
- if (url.scheme() == content::kViewSourceScheme) {
- QUrl pageUrl = QUrl(url.toString().remove(0, strlen(content::kViewSourceScheme) + 1));
+ if (request.url().scheme() == content::kViewSourceScheme) {
+ QUrl pageUrl = QUrl(request.url().toString().remove(0,
+ strlen(content::kViewSourceScheme) + 1));
if (pageUrl.scheme().isEmpty()) {
QUrl extendedUrl = QUrl::fromUserInput(pageUrl.toString());
- extendedUrl = QUrl(QString("%1:%2").arg(content::kViewSourceScheme, extendedUrl.toString()));
+ extendedUrl = QUrl(QString("%1:%2").arg(content::kViewSourceScheme,
+ extendedUrl.toString()));
gurl = toGurl(extendedUrl);
}
}
content::NavigationController::LoadURLParams params(gurl);
- params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
+ params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED
+ | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
+
+ switch (request.method()) {
+ case QWebEngineHttpRequest::Get:
+ params.load_type = content::NavigationController::LOAD_TYPE_DEFAULT;
+ break;
+
+ case QWebEngineHttpRequest::Post:
+ params.load_type = content::NavigationController::LOAD_TYPE_HTTP_POST;
+ // chromium accepts LOAD_TYPE_HTTP_POST only for the HTTP and HTTPS protocols
+ if (!params.url.SchemeIsHTTPOrHTTPS()) {
+ d->adapterClient->loadFinished(false, request.url(), false,
+ net::ERR_DISALLOWED_URL_SCHEME,
+ QCoreApplication::translate("WebContentsAdapter",
+ "HTTP-POST data can only be sent over HTTP(S) protocol"));
+ return;
+ }
+ break;
+ }
+
+ params.post_data = content::ResourceRequestBody::CreateFromBytes(
+ (const char*)request.postData().constData(),
+ request.postData().length());
+
+ // convert the custom headers into the format that chromium expects
+ QVector<QByteArray> headers = request.headers();
+ for (QVector<QByteArray>::const_iterator it = headers.cbegin(); it != headers.cend(); ++it) {
+ if (params.extra_headers.length() > 0)
+ params.extra_headers += '\n';
+ params.extra_headers += (*it).toStdString() + ": " + request.header(*it).toStdString();
+ }
+
d->webContents->GetController().LoadURLWithParams(params);
focusIfNecessary();
}