Changeset 244617 in webkit


Ignore:
Timestamp:
Apr 24, 2019 3:03:04 PM (5 years ago)
Author:
beidson@apple.com
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.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r244614 r244617  
     12019-04-24  Brady Eidson  <beidson@apple.com>
     2
     3        XMLHTTPRequest POSTs to a custom WKURLSchemeHandler protocol are missing the HTTP body.
     4        https://bugs.webkit.org/show_bug.cgi?id=191362
     5
     6        Reviewed by Alex Christensen.
     7
     8        Covered by new API tests.
     9
     10        In 2008 some refactoring added an HTTP(S)-only restriction to copying the form body for
     11        XHRs that POST, and it added that restriction with no explanation.
     12
     13        We definitely want to allow that.
     14
     15        Blobs are broken at this time (covered by bug 197237)
     16
     17        * xml/XMLHttpRequest.cpp:
     18        (WebCore::XMLHttpRequest::send):
     19        (WebCore::XMLHttpRequest::sendBytesData):
     20
    1212019-04-24  John Wilander  <wilander@apple.com>
    222
  • trunk/Source/WebCore/xml/XMLHttpRequest.cpp

    r244377 r244617  
    460460        return WTFMove(result.value());
    461461
    462     if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
     462    if (m_method != "GET" && m_method != "HEAD") {
    463463        if (!m_requestHeaders.contains(HTTPHeaderName::ContentType)) {
    464464#if ENABLE(DASHBOARD_SUPPORT)
     
    490490        return WTFMove(result.value());
    491491
    492     if (!body.isNull() && m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
     492    if (!body.isNull() && m_method != "GET" && m_method != "HEAD") {
    493493        String contentType = m_requestHeaders.get(HTTPHeaderName::ContentType);
    494494        if (contentType.isNull()) {
     
    517517        return WTFMove(result.value());
    518518
    519     if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
     519    if (m_method != "GET" && m_method != "HEAD") {
     520        if (!m_url.protocolIsInHTTPFamily()) {
     521            // FIXME: We would like to support posting Blobs to non-http URLs (e.g. custom URL schemes)
     522            // but because of the architecture of blob-handling that will require a fair amount of work.
     523           
     524            ASCIILiteral consoleMessage { "POST of a Blob to non-HTTP protocols in XMLHttpRequest.send() is currently unsupported."_s };
     525            scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, consoleMessage);
     526           
     527            return createRequest();
     528        }
     529       
    520530        if (!m_requestHeaders.contains(HTTPHeaderName::ContentType)) {
    521531            const String& blobType = body.type();
     
    540550        return WTFMove(result.value());
    541551
    542     if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
     552    if (m_method != "GET" && m_method != "HEAD") {
    543553        m_requestEntityBody = FormData::createMultiPart(body, document());
    544554        m_requestEntityBody->generateFiles(document());
     
    567577        return WTFMove(result.value());
    568578
    569     if (m_method != "GET" && m_method != "HEAD" && m_url.protocolIsInHTTPFamily()) {
     579    if (m_method != "GET" && m_method != "HEAD") {
    570580        m_requestEntityBody = FormData::create(data, length);
    571581        if (m_upload)
  • trunk/Tools/ChangeLog

    r244614 r244617  
     12019-04-24  Brady Eidson  <beidson@apple.com>
     2
     3        XMLHTTPRequest POSTs to a custom WKURLSchemeHandler protocol are missing the HTTP body.
     4        https://bugs.webkit.org/show_bug.cgi?id=191362
     5
     6        Reviewed by Alex Christensen.
     7
     8        * TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm: Add a test that POSTs all sorts of things
     9          from an XHR to a custom protocol.
     10
    1112019-04-24  John Wilander  <wilander@apple.com>
    212
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKURLSchemeHandler-1.mm

    r242339 r244617  
    2828#import "PlatformUtilities.h"
    2929#import "Test.h"
     30#import "TestNavigationDelegate.h"
     31#import "TestURLSchemeHandler.h"
     32#import "TestWKWebView.h"
    3033#import <WebKit/WKURLSchemeHandler.h>
    3134#import <WebKit/WKURLSchemeTaskPrivate.h>
     
    580583}
    581584
    582 
     585static const char* xhrPostDocument = R"XHRPOSTRESOURCE(<html><head><script>
     586window.onload = function()
     587{
     588    {
     589        var xhr = new XMLHttpRequest();
     590        xhr.open('POST', '/arraybuffer');
     591       
     592        var chars = [];
     593        var str = "Hi there";
     594        for (var i = 0; i < str.length; ++i)
     595            chars.push(str.charCodeAt(i));
     596
     597        xhr.send(new Uint8Array(chars));
     598    }
     599    {
     600        var xhr = new XMLHttpRequest();
     601        xhr.open('POST', '/string');
     602        xhr.send('foo=bar');
     603    }
     604    {
     605        var xhr = new XMLHttpRequest();
     606        xhr.open('POST', '/document');
     607        xhr.send(window.document);
     608    }
     609    {
     610        var xhr = new XMLHttpRequest();
     611        xhr.open('POST', '/formdata');
     612       
     613        var formData = new FormData();
     614        formData.append("foo", "baz");
     615        xhr.send(formData);
     616    }
     617    {
     618//        // FIXME: XHR posting of Blobs is currently unsupported
     619//        // https://bugs.webkit.org/show_bug.cgi?id=197237
     620//        var xhr = new XMLHttpRequest();
     621//        xhr.open('POST', '/blob');
     622//        var blob = new Blob(["Hello world!"], {type: "text/plain"});
     623//        xhr.send(blob);
     624    }
     625};
     626</script></head>
     627<body>
     628Hello world!
     629</body></html>)XHRPOSTRESOURCE";
     630
     631
     632TEST(URLSchemeHandler, XHRPost)
     633{
     634    auto handler = adoptNS([[TestURLSchemeHandler alloc] init]);
     635    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     636    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"xhrpost"];
     637    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
     638
     639    static bool done;
     640    static uint8_t seenTasks;
     641    [handler setStartURLSchemeTaskHandler:^(WKWebView *, id<WKURLSchemeTask> task) {
     642        if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/string"]) {
     643            static bool reached;
     644            EXPECT_FALSE(reached);
     645            reached = true;
     646            EXPECT_EQ(task.request.HTTPBody.length, 7u);
     647            EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), "foo=bar");
     648        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/arraybuffer"]) {
     649            static bool reached;
     650            EXPECT_FALSE(reached);
     651            reached = true;
     652            EXPECT_EQ(task.request.HTTPBody.length, 8u);
     653            EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), "Hi there");
     654        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/document"]) {
     655            static bool reached;
     656            EXPECT_FALSE(reached);
     657            reached = true;
     658            EXPECT_EQ(task.request.HTTPBody.length, strlen(xhrPostDocument));
     659            EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), xhrPostDocument);
     660        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/formdata"]) {
     661            static bool reached;
     662            EXPECT_FALSE(reached);
     663            reached = true;
     664            // The length of this is variable
     665            auto *formDataString = [NSString stringWithUTF8String:static_cast<const char*>(task.request.HTTPBody.bytes)];
     666            EXPECT_TRUE([formDataString containsString:@"Content-Disposition: form-data; name=\"foo\""]);
     667            EXPECT_TRUE([formDataString containsString:@"baz"]);
     668            EXPECT_TRUE([formDataString containsString:@"WebKitFormBoundary"]);
     669        } else if ([task.request.URL.absoluteString isEqualToString:@"xhrpost://example/blob"]) {
     670            static bool reached;
     671            EXPECT_FALSE(reached);
     672            reached = true;
     673           
     674            // FIXME: XHR posting of Blobs is currently unsupported
     675            // https://bugs.webkit.org/show_bug.cgi?id=197237
     676
     677            FAIL();
     678        } else {
     679            // We only expect one of the 5 URLs up above.
     680            FAIL();
     681        }
     682
     683        auto response = adoptNS([[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:0 textEncodingName:nil]);
     684        [task didReceiveResponse:response.get()];
     685        [task didFinish];
     686       
     687        if (++seenTasks == 4)
     688            done = true;
     689    }];
     690   
     691    [webView loadHTMLString:[NSString stringWithUTF8String:xhrPostDocument] baseURL:[NSURL URLWithString:@"xhrpost://example/xhrtest"]];
     692    TestWebKitAPI::Util::run(&done);
     693}
     694
Note: See TracChangeset for help on using the changeset viewer.