Fixes IE's http status code mangling from 204 to 1223

XMLHTTPRequest object in IE will return a status code of 1223 and drops some
response headers if the server returns a HTTP/204.

This patch intercepts the original response in IE6-9 and returns 204 when the
code is 1223.

Fixes issue 5031.

Change-Id: I97b9094ef702cd852cc4d918183b394ffc853c32
Review-Link: https://gwt-review.googlesource.com/#/c/1440/

Review by: skybrian@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@11449 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/http/HTTP.gwt.xml b/user/src/com/google/gwt/http/HTTP.gwt.xml
index c212dd4..4d2da5b 100644
--- a/user/src/com/google/gwt/http/HTTP.gwt.xml
+++ b/user/src/com/google/gwt/http/HTTP.gwt.xml
@@ -24,4 +24,13 @@
 	<!-- Inheriting User module for Window and Timer. These should be factored
 	     out of User soon. -->
 	<inherits name="com.google.gwt.user.User"/>
+
+	<replace-with class="com.google.gwt.http.client.Request.RequestImplIE6To9">
+		<when-type-is class="com.google.gwt.http.client.Request.RequestImpl" />
+		<any>
+			<when-property-is name="user.agent" value="ie6" />
+			<when-property-is name="user.agent" value="ie8" />
+			<when-property-is name="user.agent" value="ie9" />
+		</any>
+	</replace-with>
 </module>
diff --git a/user/src/com/google/gwt/http/client/Request.java b/user/src/com/google/gwt/http/client/Request.java
index 212b13f..da41a8b 100644
--- a/user/src/com/google/gwt/http/client/Request.java
+++ b/user/src/com/google/gwt/http/client/Request.java
@@ -15,6 +15,7 @@
  */
 package com.google.gwt.http.client;
 
+import com.google.gwt.core.shared.GWT;
 import com.google.gwt.user.client.Timer;
 import com.google.gwt.xhr.client.XMLHttpRequest;
 
@@ -32,6 +33,51 @@
 public class Request {
 
   /**
+   * Native implementation associated with {@link Request}. User classes should not use this class
+   * directly.
+   */
+  static class RequestImpl {
+
+    /**
+     * Creates a {@link Response} instance for the given JavaScript XmlHttpRequest object.
+     *
+     * @param xmlHttpRequest xmlHttpRequest object for which we need a response
+     * @return a {@link Response} object instance
+     */
+    Response createResponse(final XMLHttpRequest xmlHttpRequest) {
+      return new ResponseImpl(xmlHttpRequest);
+    }
+  }
+
+  /**
+   * Special {@link RequestImpl} for IE6-9 to work around some IE specialities.
+   */
+  static class RequestImplIE6To9 extends RequestImpl {
+
+    @Override
+    Response createResponse(XMLHttpRequest xmlHttpRequest) {
+      return new ResponseImpl(xmlHttpRequest) {
+
+        @Override
+        public int getStatusCode() {
+          /*
+           * http://code.google.com/p/google-web-toolkit/issues/detail?id=5031
+           *
+           * The XMLHTTPRequest object in IE will return a status code of 1223 and drop some
+           * response headers if the server returns a HTTP/204.
+           *
+           * This issue is fixed in IE10.
+           */
+          int statusCode = super.getStatusCode();
+          return (statusCode == 1223) ? SC_NO_CONTENT : statusCode;
+        }
+      };
+    }
+  }
+
+  private static final RequestImpl impl = GWT.create(RequestImpl.class);
+
+  /**
    * Creates a {@link Response} instance for the given JavaScript XmlHttpRequest
    * object.
    * 
@@ -39,94 +85,7 @@
    * @return a {@link Response} object instance
    */
   private static Response createResponse(final XMLHttpRequest xmlHttpRequest) {
-    assert (isResponseReady(xmlHttpRequest));
-    Response response = new Response() {
-      @Override
-      public String getHeader(String header) {
-        StringValidator.throwIfEmptyOrNull("header", header);
-
-        return xmlHttpRequest.getResponseHeader(header);
-      }
-
-      @Override
-      public Header[] getHeaders() {
-        return Request.getHeaders(xmlHttpRequest);
-      }
-
-      @Override
-      public String getHeadersAsString() {
-        return xmlHttpRequest.getAllResponseHeaders();
-      }
-
-      @Override
-      public int getStatusCode() {
-        return xmlHttpRequest.getStatus();
-      }
-
-      @Override
-      public String getStatusText() {
-        return xmlHttpRequest.getStatusText();
-      }
-
-      @Override
-      public String getText() {
-        return xmlHttpRequest.getResponseText();
-      }
-    };
-    return response;
-  }
-
-  /**
-   * Returns an array of headers built by parsing the string of headers returned
-   * by the JavaScript <code>XmlHttpRequest</code> object.
-   * 
-   * @param xmlHttpRequest
-   * @return array of Header items
-   */
-  private static Header[] getHeaders(XMLHttpRequest xmlHttp) {
-    String allHeaders = xmlHttp.getAllResponseHeaders();
-    String[] unparsedHeaders = allHeaders.split("\n");
-    Header[] parsedHeaders = new Header[unparsedHeaders.length];
-
-    for (int i = 0, n = unparsedHeaders.length; i < n; ++i) {
-      String unparsedHeader = unparsedHeaders[i];
-
-      if (unparsedHeader.length() == 0) {
-        continue;
-      }
-
-      int endOfNameIdx = unparsedHeader.indexOf(':');
-      if (endOfNameIdx < 0) {
-        continue;
-      }
-
-      final String name = unparsedHeader.substring(0, endOfNameIdx).trim();
-      final String value = unparsedHeader.substring(endOfNameIdx + 1).trim();
-      Header header = new Header() {
-        @Override
-        public String getName() {
-          return name;
-        }
-
-        @Override
-        public String getValue() {
-          return value;
-        }
-
-        @Override
-        public String toString() {
-          return name + " : " + value;
-        }
-      };
-
-      parsedHeaders[i] = header;
-    }
-
-    return parsedHeaders;
-  }
-
-  private static boolean isResponseReady(XMLHttpRequest xhr) {
-    return xhr.getReadyState() == XMLHttpRequest.DONE;
+    return impl.createResponse(xmlHttpRequest);
   }
 
   /**
diff --git a/user/src/com/google/gwt/http/client/ResponseImpl.java b/user/src/com/google/gwt/http/client/ResponseImpl.java
new file mode 100644
index 0000000..8927aef
--- /dev/null
+++ b/user/src/com/google/gwt/http/client/ResponseImpl.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2008 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.http.client;
+
+import com.google.gwt.xhr.client.XMLHttpRequest;
+
+/**
+ * A {@link Response} implementation based on a {@link XMLHttpRequest}.
+ */
+class ResponseImpl extends Response {
+
+  private final XMLHttpRequest xmlHttpRequest;
+
+  public ResponseImpl(XMLHttpRequest xmlHttpRequest) {
+    this.xmlHttpRequest = xmlHttpRequest;
+
+    assert isResponseReady();
+  }
+
+  @Override
+  public String getHeader(String header) {
+    StringValidator.throwIfEmptyOrNull("header", header);
+
+    return xmlHttpRequest.getResponseHeader(header);
+  }
+
+  @Override
+  public Header[] getHeaders() {
+    String allHeaders = xmlHttpRequest.getAllResponseHeaders();
+    String[] unparsedHeaders = allHeaders.split("\n");
+    Header[] parsedHeaders = new Header[unparsedHeaders.length];
+
+    for (int i = 0, n = unparsedHeaders.length; i < n; ++i) {
+      String unparsedHeader = unparsedHeaders[i];
+
+      if (unparsedHeader.length() == 0) {
+        continue;
+      }
+
+      int endOfNameIdx = unparsedHeader.indexOf(':');
+      if (endOfNameIdx < 0) {
+        continue;
+      }
+
+      final String name = unparsedHeader.substring(0, endOfNameIdx).trim();
+      final String value = unparsedHeader.substring(endOfNameIdx + 1).trim();
+      Header header = new Header() {
+        @Override
+        public String getName() {
+          return name;
+        }
+
+        @Override
+        public String getValue() {
+          return value;
+        }
+
+        @Override
+        public String toString() {
+          return name + " : " + value;
+        }
+      };
+
+      parsedHeaders[i] = header;
+    }
+
+    return parsedHeaders;
+  }
+
+  @Override
+  public String getHeadersAsString() {
+    return xmlHttpRequest.getAllResponseHeaders();
+  }
+
+  @Override
+  public int getStatusCode() {
+    return xmlHttpRequest.getStatus();
+  }
+
+  @Override
+  public String getStatusText() {
+    return xmlHttpRequest.getStatusText();
+  }
+
+  @Override
+  public String getText() {
+    return xmlHttpRequest.getResponseText();
+  }
+
+  private boolean isResponseReady() {
+    return xmlHttpRequest.getReadyState() == XMLHttpRequest.DONE;
+  }
+}
diff --git a/user/test/com/google/gwt/http/RequestTest.gwt.xml b/user/test/com/google/gwt/http/RequestTest.gwt.xml
index 38b75b2..9926f14 100644
--- a/user/test/com/google/gwt/http/RequestTest.gwt.xml
+++ b/user/test/com/google/gwt/http/RequestTest.gwt.xml
@@ -15,6 +15,6 @@
 <module>
   <inherits name='com.google.gwt.user.User' />
 
-  <servlet path='/testRequest'
+  <servlet path='/testRequest/*'
     class='com.google.gwt.http.server.RequestTestServlet' />
 </module>
diff --git a/user/test/com/google/gwt/http/client/RequestTest.java b/user/test/com/google/gwt/http/client/RequestTest.java
index 7046195..0f31fac 100644
--- a/user/test/com/google/gwt/http/client/RequestTest.java
+++ b/user/test/com/google/gwt/http/client/RequestTest.java
@@ -127,4 +127,32 @@
       fail(e.getMessage());
     }
   }
+
+  /*
+   * Checks that the status code is correct when receiving a 204-No-Content. This needs special
+   * handling in IE6-9. See http://code.google.com/p/google-web-toolkit/issues/detail?id=5031
+   */
+  public void test204NoContent() {
+    delayTestFinishForRequest();
+
+    RequestBuilder builder =
+        new RequestBuilder(RequestBuilder.GET, getTestBaseURL() + "204NoContent");
+    try {
+      builder.sendRequest(null, new RequestCallback() {
+
+        @Override
+        public void onResponseReceived(Request request, Response response) {
+          assertEquals(204, response.getStatusCode());
+          finishTest();
+        }
+
+        @Override
+        public void onError(Request request, Throwable exception) {
+          fail(exception.getMessage());
+        }
+      });
+    } catch (RequestException e) {
+      fail(e.getMessage());
+    }
+  }
 }
diff --git a/user/test/com/google/gwt/http/server/RequestTestServlet.java b/user/test/com/google/gwt/http/server/RequestTestServlet.java
index ec631b4..e267650 100644
--- a/user/test/com/google/gwt/http/server/RequestTestServlet.java
+++ b/user/test/com/google/gwt/http/server/RequestTestServlet.java
@@ -29,12 +29,16 @@
 
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
-    try {
-      Thread.sleep(5000);
-    } catch (InterruptedException e) {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
+    if (request.getRequestURI().endsWith("/204NoContent")) {
+      response.setStatus(HttpServletResponse.SC_NO_CONTENT);
+    } else {
+      try {
+        Thread.sleep(5000);
+      } catch (InterruptedException e) {
+        // TODO Auto-generated catch block
+        e.printStackTrace();
+      }
+      response.setStatus(HttpServletResponse.SC_OK);
     }
-    response.setStatus(HttpServletResponse.SC_OK);
   }
 }
diff --git a/user/test/com/google/gwt/user/RPCSuite.java b/user/test/com/google/gwt/user/RPCSuite.java
index f34d3dd..96b74fc 100644
--- a/user/test/com/google/gwt/user/RPCSuite.java
+++ b/user/test/com/google/gwt/user/RPCSuite.java
@@ -34,6 +34,8 @@
 import com.google.gwt.user.client.rpc.EnumsTest;
 import com.google.gwt.user.client.rpc.EnumsTestWithTypeObfuscation;
 import com.google.gwt.user.client.rpc.ExceptionsTest;
+import com.google.gwt.user.client.rpc.FailedRequestTest;
+import com.google.gwt.user.client.rpc.FailingRequestBuilderTest;
 import com.google.gwt.user.client.rpc.InheritanceTest;
 import com.google.gwt.user.client.rpc.InheritanceTestWithTypeObfuscation;
 import com.google.gwt.user.client.rpc.ObjectGraphTest;
@@ -86,6 +88,8 @@
     suite.addTestSuite(RecursiveClassTest.class);
     suite.addTestSuite(TypeCheckedObjectsTest.class);
     suite.addTestSuite(XsrfProtectionTest.class);
+    suite.addTestSuite(FailedRequestTest.class);
+    suite.addTestSuite(FailingRequestBuilderTest.class);
 
     // This test turns on the type-elision feature of RPC
     suite.addTestSuite(ValueTypesTestWithTypeObfuscation.class);
diff --git a/user/test/com/google/gwt/user/RpcSuiteNoBrowser.java b/user/test/com/google/gwt/user/RpcSuiteNoBrowser.java
index cf99468..d70792d 100644
--- a/user/test/com/google/gwt/user/RpcSuiteNoBrowser.java
+++ b/user/test/com/google/gwt/user/RpcSuiteNoBrowser.java
@@ -16,8 +16,6 @@
 package com.google.gwt.user;
 
 import com.google.gwt.dev.BootStrapPlatform;
-import com.google.gwt.user.client.rpc.FailedRequestTest;
-import com.google.gwt.user.client.rpc.FailingRequestBuilderTest;
 import com.google.gwt.user.client.rpc.impl.ClientSerializationStreamReaderTest;
 import com.google.gwt.user.rebind.rpc.BlacklistTypeFilterTest;
 import com.google.gwt.user.rebind.rpc.SerializableTypeOracleBuilderTest;
@@ -63,8 +61,6 @@
     suite.addTestSuite(SerializationPolicyLoaderTest.class);
     suite.addTestSuite(RPCServletUtilsTest.class);
     suite.addTestSuite(RPCRequestTest.class);
-    suite.addTestSuite(FailedRequestTest.class);
-    suite.addTestSuite(FailingRequestBuilderTest.class);
     suite.addTestSuite(Base64Test.class);
     suite.addTestSuite(UtilTest.class);
     suite.addTestSuite(AbstractXsrfProtectedServiceServletTest.class);
diff --git a/user/test/com/google/gwt/user/client/rpc/FailedRequestTest.java b/user/test/com/google/gwt/user/client/rpc/FailedRequestTest.java
index 2691c34..b694fae 100644
--- a/user/test/com/google/gwt/user/client/rpc/FailedRequestTest.java
+++ b/user/test/com/google/gwt/user/client/rpc/FailedRequestTest.java
@@ -18,12 +18,10 @@
 import com.google.gwt.http.client.Request;
 import com.google.gwt.user.client.rpc.impl.FailedRequest;
 
-import junit.framework.TestCase;
-
 /**
  * Tests the {@link com.google.gwt.user.client.rpc.impl.FailedRequest} class.
  */
-public class FailedRequestTest extends TestCase {
+public class FailedRequestTest extends RpcTestBase {
   public void testBasics() {
     Request failedRequest = new FailedRequest();
     assertFalse(failedRequest.isPending());
diff --git a/user/test/com/google/gwt/user/client/rpc/FailingRequestBuilderTest.java b/user/test/com/google/gwt/user/client/rpc/FailingRequestBuilderTest.java
index effa813..4dcb045 100644
--- a/user/test/com/google/gwt/user/client/rpc/FailingRequestBuilderTest.java
+++ b/user/test/com/google/gwt/user/client/rpc/FailingRequestBuilderTest.java
@@ -19,12 +19,10 @@
 import com.google.gwt.http.client.RequestException;
 import com.google.gwt.user.client.rpc.impl.FailingRequestBuilder;
 
-import junit.framework.TestCase;
-
 /**
  * Tests the {@link FailingRequestBuilder} class.
  */
-public class FailingRequestBuilderTest extends TestCase {
+public class FailingRequestBuilderTest extends RpcTestBase {
   public void testBasics() throws RequestException {
     final boolean[] callbackCalled = new boolean[] {false};