[XHR] overrideMimeType() should be able to change encoding in HEADERS RECEIVED state
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 10 Jul 2014 10:09:41 +0000 (10:09 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 10 Jul 2014 10:09:41 +0000 (10:09 +0000)
https://bugs.webkit.org/show_bug.cgi?id=128968

Patch by Youenn Fablet <youenn.fablet@crf.canon.fr> on 2014-07-10
Reviewed by Alexey Proskuryakov.

Source/WebCore:
Moved response encoding computation from didReceiveResponse to didReceiveData, just before the decoder is instantiated.
This allows overrideMimeType to be changed within readystatechange event callback and have an impact on selected encoding.

Test: http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html

* xml/XMLHttpRequest.cpp:
(WebCore::XMLHttpRequest::didReceiveResponse): Removed m_responseEncoding computation from response and mimeTypeOverride.
(WebCore::XMLHttpRequest::didReceiveData): Added m_responseEncoding computation from response and mimeTypeOverride.

LayoutTests:
Added test equivalent to http://w3c-test.org/web-platform-tests/master/XMLHttpRequest/overridemimetype-headers-received-state-force-shiftjis.htm
The test sets overrideMimeType when xhr state is changed to HEADERS RECEIVED.

* http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis-expected.txt: Added.
* http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html: Added.
* http/tests/xmlhttprequest/resources/get-content.php: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@170960 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html [new file with mode: 0644]
LayoutTests/http/tests/xmlhttprequest/resources/get-content.php [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/xml/XMLHttpRequest.cpp

index 2ed54b1..150d4d5 100644 (file)
@@ -1,3 +1,17 @@
+2014-07-10  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        [XHR] overrideMimeType() should be able to change encoding in HEADERS RECEIVED state
+        https://bugs.webkit.org/show_bug.cgi?id=128968
+
+        Reviewed by Alexey Proskuryakov.
+
+        Added test equivalent to http://w3c-test.org/web-platform-tests/master/XMLHttpRequest/overridemimetype-headers-received-state-force-shiftjis.htm
+        The test sets overrideMimeType when xhr state is changed to HEADERS RECEIVED.
+
+        * http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis-expected.txt: Added.
+        * http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html: Added.
+        * http/tests/xmlhttprequest/resources/get-content.php: Added.
+
 2014-07-08  Jeffrey Pfau  <jpfau@apple.com>
 
         Fix flaky loading tests
diff --git a/LayoutTests/http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis-expected.txt b/LayoutTests/http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis-expected.txt
new file mode 100644 (file)
index 0000000..e22eb3c
--- /dev/null
@@ -0,0 +1,3 @@
+
+PASS XMLHttpRequest: overrideMimeType() in HEADERS RECEIVED state, enforcing Shift-JIS encoding 
+
diff --git a/LayoutTests/http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html b/LayoutTests/http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html
new file mode 100644 (file)
index 0000000..221736b
--- /dev/null
@@ -0,0 +1,35 @@
+<!doctype html>
+<html>
+  <head>
+    <title>XMLHttpRequest: overrideMimeType() in HEADERS RECEIVED state, enforcing Shift-JIS encoding</title>
+    <meta charset="utf-8">
+    <script src="/js-test-resources/testharness.js"></script>
+    <script src="/js-test-resources/testharnessreport.js"></script>
+    <link rel="help" href="http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-overridemimetype()-method" data-tested-assertations="/following::ol/li[1] /following::ol/li[4]" />
+    <!-- similar to http://w3c-test.org/XMLHttpRequest/overridemimetype-headers-received-state-force-shiftjis.htm -->
+  </head>
+  <body>
+    <div id="log"></div>
+    <script>
+      var test = async_test();
+      var client = new XMLHttpRequest();
+      var readyState2Reached = false;
+      client.onreadystatechange = test.step_func( function() {
+        if(client.readyState===2){
+          readyState2Reached = true;
+          try{
+            client.overrideMimeType('text/plain;charset=Shift-JIS');
+          }catch(e){
+            assert_unreached('overrideMimeType should not throw in state 2');
+          }
+        }
+        if (client.readyState !== 4) return;
+        assert_equals( readyState2Reached, true, "readyState = 2 event fired" );
+        assert_equals( client.responseText, 'ใƒ†ใ‚นใƒˆ', 'overrideMimeType() in HEADERS RECEIVED state set encoding' );
+        test.done();
+      });
+      client.open("GET", "resources/get-content.php?type="+encodeURIComponent('text/html;charset=UTF-8')+'&content=%83%65%83%58%83%67');
+      client.send( '' );
+    </script>
+  </body>
+</html>
diff --git a/LayoutTests/http/tests/xmlhttprequest/resources/get-content.php b/LayoutTests/http/tests/xmlhttprequest/resources/get-content.php
new file mode 100644 (file)
index 0000000..9eabaa9
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+  $content = $_GET["content"] ? $_GET["content"] : "";
+  $type = $_GET["type"] ? $_GET["type"] : "";
+  header("HTTP/1.1 200 OK");
+  header("Content-Type:" . $type);
+
+  echo $content;
+  exit;
+?>
index 9cd66f5..14b9fe6 100644 (file)
@@ -1,3 +1,19 @@
+2014-07-10  Youenn Fablet  <youenn.fablet@crf.canon.fr>
+
+        [XHR] overrideMimeType() should be able to change encoding in HEADERS RECEIVED state
+        https://bugs.webkit.org/show_bug.cgi?id=128968
+
+        Reviewed by Alexey Proskuryakov.
+
+        Moved response encoding computation from didReceiveResponse to didReceiveData, just before the decoder is instantiated.
+        This allows overrideMimeType to be changed within readystatechange event callback and have an impact on selected encoding.
+
+        Test: http/tests/xmlhttprequest/overridemimetype-headers-received-state-force-shiftjis.html
+
+        * xml/XMLHttpRequest.cpp:
+        (WebCore::XMLHttpRequest::didReceiveResponse): Removed m_responseEncoding computation from response and mimeTypeOverride.
+        (WebCore::XMLHttpRequest::didReceiveData): Added m_responseEncoding computation from response and mimeTypeOverride.
+
 2014-07-09  Brady Eidson  <beidson@apple.com>
 
         Fire connected/disconnected events for Gamepads.
index c93eb20..d81e069 100644 (file)
@@ -1128,13 +1128,8 @@ void XMLHttpRequest::didReceiveResponse(unsigned long identifier, const Resource
     InspectorInstrumentation::didReceiveXHRResponse(scriptExecutionContext(), identifier);
 
     m_response = response;
-    if (!m_mimeTypeOverride.isEmpty()) {
+    if (!m_mimeTypeOverride.isEmpty())
         m_response.setHTTPHeaderField(HTTPHeaderName::ContentType, m_mimeTypeOverride);
-        m_responseEncoding = extractCharsetFromMediaType(m_mimeTypeOverride);
-    }
-
-    if (m_responseEncoding.isEmpty())
-        m_responseEncoding = response.textEncodingName();
 }
 
 void XMLHttpRequest::didReceiveData(const char* data, int len)
@@ -1145,6 +1140,12 @@ void XMLHttpRequest::didReceiveData(const char* data, int len)
     if (m_state < HEADERS_RECEIVED)
         changeState(HEADERS_RECEIVED);
 
+    // FIXME: Should we update "Content-Type" header field with m_mimeTypeOverride value in case it has changed since didReceiveResponse?
+    if (!m_mimeTypeOverride.isEmpty())
+        m_responseEncoding = extractCharsetFromMediaType(m_mimeTypeOverride);
+    if (m_responseEncoding.isEmpty())
+        m_responseEncoding = m_response.textEncodingName();
+
     bool useDecoder = shouldDecodeResponse();
 
     if (useDecoder && !m_decoder) {