Title: [244617] trunk
Revision
244617
Author
beid...@apple.com
Date
2019-04-24 15:03:04 -0700 (Wed, 24 Apr 2019)

Log Message

XMLHTTPRequest POSTs to a custom WKURLSchemeHandler protocol are missing the HTTP body.
https://bugs.webkit.org/show_bug.cgi?id=191362

Reviewed by Alex Christensen.

Source/WebCore:

Covered by new API tests.

In 2008 some refactoring added an HTTP(S)-only restriction to copying the form body for
XHRs that POST, and it added that restriction with no explanation.

We definitely want to allow that.

Blobs are broken at this time (covered by bug 197237)

* xml/XMLHttpRequest.cpp:
(WebCore::XMLHttpRequest::send):
(WebCore::XMLHttpRequest::sendBytesData):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm: Add a test that POSTs all sorts of things
  from an XHR to a custom protocol.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (244616 => 244617)


--- trunk/Source/WebCore/ChangeLog	2019-04-24 21:41:42 UTC (rev 244616)
+++ trunk/Source/WebCore/ChangeLog	2019-04-24 22:03:04 UTC (rev 244617)
@@ -1,3 +1,23 @@
+2019-04-24  Brady Eidson  <beid...@apple.com>
+
+        XMLHTTPRequest POSTs to a custom WKURLSchemeHandler protocol are missing the HTTP body.
+        https://bugs.webkit.org/show_bug.cgi?id=191362
+
+        Reviewed by Alex Christensen.
+
+        Covered by new API tests.
+
+        In 2008 some refactoring added an HTTP(S)-only restriction to copying the form body for
+        XHRs that POST, and it added that restriction with no explanation.
+
+        We definitely want to allow that.
+
+        Blobs are broken at this time (covered by bug 197237)
+
+        * xml/XMLHttpRequest.cpp:
+        (WebCore::XMLHttpRequest::send):
+        (WebCore::XMLHttpRequest::sendBytesData):
+
 2019-04-24  John Wilander  <wilan...@apple.com>
 
         Age out unconverted Ad Click Attributions after one week.

Modified: trunk/Source/WebCore/xml/XMLHttpRequest.cpp (244616 => 244617)


--- trunk/Source/WebCore/xml/XMLHttpRequest.cpp	2019-04-24 21:41:42 UTC (rev 244616)
+++ trunk/Source/WebCore/xml/XMLHttpRequest.cpp	2019-04-24 22:03:04 UTC (rev 244617)
@@ -459,7 +459,7 @@
     if (auto result = prepareToSend())
         return WTFMove(result.value());
 
-    if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
+    if (m_method != "GET" && m_method != "HEAD") {
         if (!m_requestHeaders.contains(HTTPHeaderName::ContentType)) {
 #if ENABLE(DASHBOARD_SUPPORT)
             if (usesDashboardBackwardCompatibilityMode())
@@ -489,7 +489,7 @@
     if (auto result = prepareToSend())
         return WTFMove(result.value());
 
-    if (!body.isNull() && m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
+    if (!body.isNull() && m_method != "GET" && m_method != "HEAD") {
         String contentType = m_requestHeaders.get(HTTPHeaderName::ContentType);
         if (contentType.isNull()) {
 #if ENABLE(DASHBOARD_SUPPORT)
@@ -516,7 +516,17 @@
     if (auto result = prepareToSend())
         return WTFMove(result.value());
 
-    if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
+    if (m_method != "GET" && m_method != "HEAD") {
+        if (!m_url.protocolIsInHTTPFamily()) {
+            // FIXME: We would like to support posting Blobs to non-http URLs (e.g. custom URL schemes)
+            // but because of the architecture of blob-handling that will require a fair amount of work.
+            
+            ASCIILiteral consoleMessage { "POST of a Blob to non-HTTP protocols in XMLHttpRequest.send() is currently unsupported."_s };
+            scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, consoleMessage);
+            
+            return createRequest();
+        }
+        
         if (!m_requestHeaders.contains(HTTPHeaderName::ContentType)) {
             const String& blobType = body.type();
             if (!blobType.isEmpty() && isValidContentType(blobType))
@@ -539,7 +549,7 @@
     if (auto result = prepareToSend())
         return WTFMove(result.value());
 
-    if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
+    if (m_method != "GET" && m_method != "HEAD") {
         m_requestEntityBody = FormData::createMultiPart(body, document());
         m_requestEntityBody->generateFiles(document());
         if (!m_requestHeaders.contains(HTTPHeaderName::ContentType))
@@ -566,7 +576,7 @@
     if (auto result = prepareToSend())
         return WTFMove(result.value());
 
-    if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
+    if (m_method != "GET" && m_method != "HEAD") {
         m_requestEntityBody = FormData::create(data, length);
         if (m_upload)
             m_requestEntityBody->setAlwaysStream(true);

Modified: trunk/Tools/ChangeLog (244616 => 244617)


--- trunk/Tools/ChangeLog	2019-04-24 21:41:42 UTC (rev 244616)
+++ trunk/Tools/ChangeLog	2019-04-24 22:03:04 UTC (rev 244617)
@@ -1,3 +1,13 @@
+2019-04-24  Brady Eidson  <beid...@apple.com>
+
+        XMLHTTPRequest POSTs to a custom WKURLSchemeHandler protocol are missing the HTTP body.
+        https://bugs.webkit.org/show_bug.cgi?id=191362
+
+        Reviewed by Alex Christensen.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm: Add a test that POSTs all sorts of things
+          from an XHR to a custom protocol.
+
 2019-04-24  John Wilander  <wilan...@apple.com>
 
         Age out unconverted Ad Click Attributions after one week.

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm (244616 => 244617)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm	2019-04-24 21:41:42 UTC (rev 244616)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm	2019-04-24 22:03:04 UTC (rev 244617)
@@ -27,6 +27,9 @@
 
 #import "PlatformUtilities.h"
 #import "Test.h"
+#import "TestNavigationDelegate.h"
+#import "TestURLSchemeHandler.h"
+#import "TestWKWebView.h"
 #import <WebKit/WKURLSchemeHandler.h>
 #import <WebKit/WKURLSchemeTaskPrivate.h>
 #import <WebKit/WKWebViewConfigurationPrivate.h>
@@ -579,4 +582,113 @@
     TestWebKitAPI::Util::run(&done);
 }
 
+static const char* xhrPostDocument = R"XHRPOSTRESOURCE(<html><head><script>
+window._onload_ = function()
+{
+    {
+        var xhr = new XMLHttpRequest();
+        xhr.open('POST', '/arraybuffer');
+        
+        var chars = [];
+        var str = "Hi there";
+        for (var i = 0; i < str.length; ++i)
+            chars.push(str.charCodeAt(i));
 
+        xhr.send(new Uint8Array(chars));
+    }
+    {
+        var xhr = new XMLHttpRequest();
+        xhr.open('POST', '/string');
+        xhr.send('foo=bar');
+    }
+    {
+        var xhr = new XMLHttpRequest();
+        xhr.open('POST', '/document');
+        xhr.send(window.document);
+    }
+    {
+        var xhr = new XMLHttpRequest();
+        xhr.open('POST', '/formdata');
+        
+        var formData = new FormData();
+        formData.append("foo", "baz");
+        xhr.send(formData);
+    }
+    {
+//        // FIXME: XHR posting of Blobs is currently unsupported
+//        // https://bugs.webkit.org/show_bug.cgi?id=197237
+//        var xhr = new XMLHttpRequest();
+//        xhr.open('POST', '/blob');
+//        var blob = new Blob(["Hello world!"], {type: "text/plain"});
+//        xhr.send(blob);
+    }
+};
+</script></head>
+<body>
+Hello world!
+</body></html>)XHRPOSTRESOURCE";
+
+
+TEST(URLSchemeHandler, XHRPost)
+{
+    auto handler = adoptNS([[TestURLSchemeHandler alloc] init]);
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"xhrpost"];
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
+
+    static bool done;
+    static uint8_t seenTasks;
+    [handler setStartURLSchemeTaskHandler:^(WKWebView *, id<WKURLSchemeTask> task) {
+        if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/string"]) {
+            static bool reached;
+            EXPECT_FALSE(reached);
+            reached = true;
+            EXPECT_EQ(task.request.HTTPBody.length, 7u);
+            EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), "foo=bar");
+        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/arraybuffer"]) {
+            static bool reached;
+            EXPECT_FALSE(reached);
+            reached = true;
+            EXPECT_EQ(task.request.HTTPBody.length, 8u);
+            EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), "Hi there");
+        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/document"]) {
+            static bool reached;
+            EXPECT_FALSE(reached);
+            reached = true;
+            EXPECT_EQ(task.request.HTTPBody.length, strlen(xhrPostDocument));
+            EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), xhrPostDocument);
+        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/formdata"]) {
+            static bool reached;
+            EXPECT_FALSE(reached);
+            reached = true;
+            // The length of this is variable
+            auto *formDataString = [NSString stringWithUTF8String:static_cast<const char*>(task.request.HTTPBody.bytes)];
+            EXPECT_TRUE([formDataString containsString:@"Content-Disposition: form-data; name=\"foo\""]);
+            EXPECT_TRUE([formDataString containsString:@"baz"]);
+            EXPECT_TRUE([formDataString containsString:@"WebKitFormBoundary"]);
+        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/blob"]) {
+            static bool reached;
+            EXPECT_FALSE(reached);
+            reached = true;
+            
+            // FIXME: XHR posting of Blobs is currently unsupported
+            // https://bugs.webkit.org/show_bug.cgi?id=197237
+
+            FAIL();
+        } else {
+            // We only expect one of the 5 URLs up above.
+            FAIL();
+        }
+
+        auto response = adoptNS([[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:0 textEncodingName:nil]);
+        [task didReceiveResponse:response.get()];
+        [task didFinish];
+        
+        if (++seenTasks == 4)
+            done = true;
+    }];
+    
+    [webView loadHTMLString:[NSString stringWithUTF8String:xhrPostDocument] baseURL:[NSURL URLWithString:@"xhrpost://example/xhrtest"]];
+    TestWebKitAPI::Util::run(&done);
+}
+
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to