Bug 1205288 - implement and test fetch spec changes for blob scheme with non-GET method. r=bkelly
authordimi <dlee@mozilla.com>
Wed, 06 Jan 2016 17:13:19 +0800
changeset 278971 4f4ef7073bb53185edc3a208446c687b291abe61
parent 278930 2f6e12a6f04f8e676c357bd50ec88e81f2037306
child 278972 f3bc2537d59799255ab14029d51316881c7a298a
push id29863
push userkwierso@gmail.com
push dateFri, 08 Jan 2016 01:12:09 +0000
treeherdermozilla-central@8a7cc8b7aa55 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1205288
milestone46.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1205288 - implement and test fetch spec changes for blob scheme with non-GET method. r=bkelly
dom/fetch/FetchDriver.cpp
dom/tests/mochitest/fetch/test_fetch_basic.js
--- a/dom/fetch/FetchDriver.cpp
+++ b/dom/fetch/FetchDriver.cpp
@@ -125,16 +125,25 @@ FetchDriver::HttpFetch()
   if (mRequest->Mode() == RequestMode::No_cors &&
       mRequest->UnsafeRequest() &&
       (!mRequest->HasSimpleMethod() ||
        !mRequest->Headers()->HasOnlySimpleHeaders())) {
     MOZ_ASSERT(false, "The API should have caught this");
     return NS_ERROR_DOM_BAD_URI;
   }
 
+  // non-GET requests aren't allowed for blob.
+  if (IsBlobURI(uri)) {
+    nsAutoCString method;
+    mRequest->GetMethod(method);
+    if (!method.EqualsLiteral("GET")) {
+      return NS_ERROR_DOM_NETWORK_ERR;
+    }
+  }
+
   // Step 2 deals with letting ServiceWorkers intercept requests. This is
   // handled by Necko after the channel is opened.
   // FIXME(nsm): Bug 1119026: The channel's skip service worker flag should be
   // set based on the Request's flag.
 
   // Step 3.1 "If the CORS preflight flag is set and one of these conditions is
   // true..." is handled by the CORS proxy.
   //
--- a/dom/tests/mochitest/fetch/test_fetch_basic.js
+++ b/dom/tests/mochitest/fetch/test_fetch_basic.js
@@ -67,15 +67,38 @@ function testSameOriginBlobURL() {
     ok(res.headers.has("content-length"), "Headers must have Content-Length header");
     is(parseInt(res.headers.get("content-length")), 16, "Content-Length should match Blob's size");
     return res.text().then(function(body) {
       is(body, "english sentence", "Blob fetch body should match");
     });
   });
 }
 
+function testNonGetBlobURL() {
+  var blob = new Blob(["english ", "sentence"], { type: "text/plain" });
+  var url = URL.createObjectURL(blob);
+  return Promise.all(
+    [
+      "HEAD",
+      "POST",
+      "PUT",
+      "DELETE"
+    ].map(method => {
+      var req = new Request(url, { method: method });
+      return fetch(req).then(function(res) {
+        ok(false, "Blob URL with non-GET request should not succeed");
+      }).catch(function(e) {
+        ok(e instanceof TypeError, "Blob URL with non-GET request should get a TypeError");
+      });
+    })
+  ).then(function() {
+    URL.revokeObjectURL(url);
+  });
+}
+
 function runTest() {
   return Promise.resolve()
     .then(testAboutURL)
     .then(testDataURL)
     .then(testSameOriginBlobURL)
+    .then(testNonGetBlobURL)
     // Put more promise based tests here.
 }