Bug 1392220 - do not parse or return body for XHRs with HEAD or CONNECT method; r?annevk, baku draft
authorThomas Wisniewski <wisniewskit@gmail.com>
Sat, 09 Sep 2017 15:34:48 -0400
changeset 661944 b46dcaf23326ef33c800ef48074ec03080b65889
parent 661689 ea7b55d65d76214f97aaae502d65cb26fc6f5659
child 730710 90e2d23ba449872bbc94dd927d8a10f30c6228b8
push id78914
push userwisniewskit@gmail.com
push dateSat, 09 Sep 2017 19:39:09 +0000
reviewersannevk, baku
bugs1392220
milestone57.0a1
Bug 1392220 - do not parse or return body for XHRs with HEAD or CONNECT method; r?annevk, baku MozReview-Commit-ID: 40CxCiSFdjC
dom/workers/test/serviceworkers/fetch/fetch_tests.js
dom/xhr/XMLHttpRequestMainThread.cpp
testing/web-platform/meta/XMLHttpRequest/data-uri.htm.ini
--- a/dom/workers/test/serviceworkers/fetch/fetch_tests.js
+++ b/dom/workers/test/serviceworkers/fetch/fetch_tests.js
@@ -407,17 +407,21 @@ fetch('interrupt.sjs')
 }, function(e) {
   my_ok(false, "interrupted fetch failed");
   finish();
 });
 
 ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'].forEach(function(method) {
   fetchXHRWithMethod('xhr-method-test.txt', method, function(xhr) {
     my_ok(xhr.status == 200, method + " load should be successful");
-    my_ok(xhr.responseText == ("intercepted " + method), method + " load should have synthesized response");
+    if (method === "HEAD") {
+      my_ok(xhr.responseText == "", method + "load should not have synthesized response");
+    } else {
+      my_ok(xhr.responseText == ("intercepted " + method), method + " load should have synthesized response");
+    }
     finish();
   });
 });
 
 expectAsyncResult();
 fetch(new Request('empty-header', {headers:{"emptyheader":""}}))
 .then(function(res) {
   return res.text();
--- a/dom/xhr/XMLHttpRequestMainThread.cpp
+++ b/dom/xhr/XMLHttpRequestMainThread.cpp
@@ -619,16 +619,22 @@ XMLHttpRequestMainThread::GetResponseTex
     aSnapshot.SetVoid();
     return;
   }
 
   if (mState != State::loading && mState != State::done) {
     return;
   }
 
+  // Main Fetch step 18 requires to ignore body for head/connect methods.
+  if (mRequestMethod.EqualsLiteral("HEAD") ||
+      mRequestMethod.EqualsLiteral("CONNECT")) {
+    return;
+  }
+
   // We only decode text lazily if we're also parsing to a doc.
   // Also, if we've decoded all current data already, then no need to decode
   // more.
   if ((!mResponseXML && !mErrorParsingXML) ||
       mResponseBodyDecodedPos == mResponseBody.Length()) {
     mResponseText.CreateSnapshot(aSnapshot);
     return;
   }
@@ -2076,25 +2082,21 @@ XMLHttpRequestMainThread::OnStartRequest
           contentLength > 0 &&
           contentLength < XML_HTTP_REQUEST_MAX_CONTENT_LENGTH_PREALLOCATE) {
         mArrayBufferBuilder.setCapacity(static_cast<int32_t>(contentLength));
       }
     }
   }
 
   // Set up responseXML
-  bool parseBody = mResponseType == XMLHttpRequestResponseType::_empty ||
-                   mResponseType == XMLHttpRequestResponseType::Document;
-  nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(mChannel));
-  if (parseBody && httpChannel) {
-    nsAutoCString method;
-    rv = httpChannel->GetRequestMethod(method);
-    MOZ_ASSERT(NS_SUCCEEDED(rv));
-    parseBody = !method.EqualsLiteral("HEAD");
-  }
+  // Note: Main Fetch step 18 requires to ignore body for head/connect methods.
+  bool parseBody = (mResponseType == XMLHttpRequestResponseType::_empty ||
+                    mResponseType == XMLHttpRequestResponseType::Document) &&
+                   !(mRequestMethod.EqualsLiteral("HEAD") ||
+                     mRequestMethod.EqualsLiteral("CONNECT"));
 
   mIsHtml = false;
   mWarnAboutSyncHtml = false;
   if (parseBody && NS_SUCCEEDED(status)) {
     // We can gain a huge performance win by not even trying to
     // parse non-XML data. This also protects us from the situation
     // where we have an XML document and sink, but HTML (or other)
     // parser, which can produce unreliable results.
--- a/testing/web-platform/meta/XMLHttpRequest/data-uri.htm.ini
+++ b/testing/web-platform/meta/XMLHttpRequest/data-uri.htm.ini
@@ -1,26 +1,4 @@
 [data-uri.htm]
   type: testharness
-  [XHR method GET with charset image/png]
-    expected: FAIL
-
-  [XHR method POST with charset text/plain]
-    expected: FAIL
-
-  [XHR method PUT with charset text/plain]
-    expected: FAIL
-
-  [XHR method DELETE with charset text/plain]
-    expected: FAIL
-
-  [XHR method HEAD with charset text/plain]
-    expected: FAIL
-
-  [XHR method UNICORN with charset text/plain]
-    expected: FAIL
-
   [XHR method GET with MIME type image/png]
     expected: FAIL
-
-  [XHR method HEAD with MIME type text/plain]
-    expected: FAIL
-