]> source.dussan.org Git - gwtquery.git/commitdiff
Dont set credentials by default for ajax requests. Fixes issue #261
authorManolo Carrasco <manolo@apache.org>
Mon, 27 Jan 2014 10:40:32 +0000 (11:40 +0100)
committerManolo Carrasco <manolo@apache.org>
Mon, 27 Jan 2014 10:40:32 +0000 (11:40 +0100)
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java
gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java
gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestJre.java
gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTests.java
gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java

index 8f0846c64bd7b783d6b58665980452ad36870969..566ab14dcdf853beac7908fa5528963998306cb0 100644 (file)
@@ -63,6 +63,7 @@ public class Ajax extends GQuery {
     String getType();
     String getUrl();
     String getUsername();
+    boolean getWithCredentials();
     Settings setContentType(String t);
     Settings setContext(Element e);
     Settings setData(Object p);
@@ -76,6 +77,7 @@ public class Ajax extends GQuery {
     Settings setType(String t);
     Settings setUrl(String u);
     Settings setUsername(String u);
+    Settings setWithCredentials(boolean b);
   }
 
   public static final Class<Ajax> Ajax = registerPlugin(Ajax.class, new Plugin<Ajax>() {
index 79d3fa8d48890908d1a015e156698b219969510f..1d1bf0139cee0480d0ca643f4ce3bfbc115b5196 100644 (file)
@@ -120,7 +120,7 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal
 
     // Using gQuery to set credentials since this method was added in 2.5.1
     // xmlHttpRequest.setWithCredentials(true);
-    JsUtils.prop(xmlHttpRequest, "withCredentials", true);
+    JsUtils.prop(xmlHttpRequest, "withCredentials", settings.getWithCredentials());
     
     final Request request = createRequestVltr(xmlHttpRequest, settings.getTimeout(), this);
     
index 166fa3b45b0eea09905117882c4dd79fe58e89f6..9da7f4203e5c570c58a69e04139a98b85d62bc9b 100644 (file)
@@ -168,8 +168,13 @@ public class AjaxTransportJre implements AjaxTransport {
     }
     
     int code = c.getResponseCode();
-    if (isCORS && !localDomain.equals(c.getHeaderField("Access-Control-Allow-Origin"))) {
-      code = 0;
+    if (isCORS) {
+      if (!localDomain.equals(c.getHeaderField("Access-Control-Allow-Origin"))) {
+        code = 0;
+      }
+      if (s.getWithCredentials() && c.getHeaderField("Access-Control-Allow-Credentials") == null) {
+        code = 0;
+      }
     }
     
     BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
index abe00e30d1385fb2a1eb05ea37bb6e9ae083d439..1167651d6ee0cd67b86c614661f612b07dd7325f 100644 (file)
@@ -49,6 +49,7 @@ public class AjaxTestJre extends AjaxTests {
     
     echoUrl = localDomain + "/" + servletPath;
     echoUrlCORS = corsDomain + "/" + servletPath + "?cors=true";
+
     startWebServer(port);
   }
   
index f6a064c5d3176c1562c99faf73a8ce0a89ca8690..ee3005c93fcde991d5582cc702a172bbbfe61f50 100644 (file)
@@ -15,6 +15,8 @@
  */
 package com.google.gwt.query.client.ajax;
 
+import junit.framework.Assert;
+
 import com.google.gwt.http.client.Response;
 import com.google.gwt.junit.DoNotRunWith;
 import com.google.gwt.junit.Platform;
@@ -119,7 +121,49 @@ public abstract class AjaxTests extends GWTTestCase {
       .setData(jsonGET)
       .setDataType("json");
 
-    performAjaxJsonTest_CORS(s);
+    performAjaxJsonTest_CORS(s)
+      .done(new Function() {
+          public void f() {
+            Response r = arguments(3);
+            Assert.assertNotNull(r.getHeader("Access-Control-Allow-Origin"));
+            Assert.assertNull(r.getHeader("Access-Control-Allow-Credentials"));
+          }
+        });
+  }
+  
+  @DoNotRunWith(Platform.HtmlUnitBug)
+  public void testAjaxJsonGet_CORS_WithCredentials_Supported() {
+    Settings s = Ajax.createSettings()
+      .setType("get")
+      // Enable credentials in servlet 
+      .setUrl(echoUrlCORS + "&credentials=true")
+      .setData(jsonGET)
+      .setDataType("json")
+      .setWithCredentials(true);
+
+    performAjaxJsonTest_CORS(s)
+      .done(new Function() {
+        public void f() {
+          Response r = arguments(3);
+          Assert.assertNotNull(r.getHeader("Access-Control-Allow-Origin"));
+          Assert.assertNotNull(r.getHeader("Access-Control-Allow-Credentials"));
+        }
+      });
+  }
+  
+  @DoNotRunWith(Platform.HtmlUnitBug)
+  public void testAjaxJsonGet_CORS_WithCredentials_Unsupported() {
+    Settings s = Ajax.createSettings()
+      .setType("get")
+      // Disable credentials in servlet 
+      .setUrl(echoUrlCORS)
+      .setData(jsonGET)
+      .setDataType("json")
+      .setWithCredentials(true);
+    
+    Ajax.ajax(s)
+      .fail(finishFunction)
+      .done(failFunction);
   }
   
   public void testAjaxGetJsonP() {
index 5152de4cfad8513352934926384db932bbeb1152..b00d2469caa345f6c15179046abdba4808c6a9bf 100644 (file)
@@ -61,7 +61,9 @@ public class GQAjaxTestServlet extends HttpServlet {
     String origin = req.getHeader("Origin");
     if ("true".equals(req.getParameter("cors")) && origin != null) {
       resp.addHeader("Access-Control-Allow-Origin", origin);
-      resp.addHeader("Access-Control-Allow-Credentials", "true");
+      if ("true".equals(req.getParameter("credentials"))) {
+        resp.addHeader("Access-Control-Allow-Credentials", "true");
+      }
       String method = req.getHeader("Access-Control-Request-Method");
       if (method != null) {
         resp.addHeader("Access-Control-Allow-Methods", method);