geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rickmcgu...@apache.org
Subject svn commit: r631262 - in /geronimo/sandbox/AsyncHttpClient/src: main/java/org/apache/ahc/ main/java/org/apache/ahc/codec/ test/java/org/apache/ahc/
Date Tue, 26 Feb 2008 15:52:10 GMT
Author: rickmcguire
Date: Tue Feb 26 07:52:08 2008
New Revision: 631262

URL: http://svn.apache.org/viewvc?rev=631262&view=rev
Log:
GERONIMO-3861 cookies need to be validated and filtered

Patch provided by Sangjin Lee 


Added:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
  (with props)
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
  (with props)
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
  (with props)
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java   (with
props)
Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpDecoder.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ChunkedTest.java
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java Tue
Feb 26 07:52:08 2008
@@ -30,6 +30,8 @@
 
 import javax.net.ssl.SSLContext;
 
+import org.apache.ahc.codec.CookiePolicy;
+import org.apache.ahc.codec.DefaultCookiePolicy;
 import org.apache.ahc.codec.HttpDecoder;
 import org.apache.ahc.codec.HttpIoHandler;
 import org.apache.ahc.codec.HttpProtocolCodecFactory;
@@ -119,7 +121,10 @@
     private final HttpIoHandler handler;
     
     /** The cache for session reuse */
-    private SessionCache sessionCache; 
+    private SessionCache sessionCache;
+    
+    /** The cookie policy */
+    private volatile CookiePolicy cookiePolicy = new DefaultCookiePolicy();
 
     /** The Reuse Address Socket Parameter. */
     private boolean reuseAddress = DEFAULT_REUSE_ADDRESS;
@@ -421,6 +426,21 @@
     }
     
     /**
+     * Sets the current cookie policy.
+     */
+    public void setCookiePolicy(CookiePolicy policy) {
+        cookiePolicy = policy;
+    }
+    
+    /**
+     * Returns the current cookie policy.  It is <tt>DefaultCookiePolicy</tt>
+     * by default.
+     */
+    public CookiePolicy getCookiePolicy() {
+        return cookiePolicy;
+    }
+    
+    /**
      * Sends a request.  The call is non-blocking, and returns a future object
      * with which the caller can synchronize on the completion of the request.
      * This does not use a completion queue as provided by the other version of
@@ -470,6 +490,9 @@
         if (message.getResponseFuture() == null) {
             message.setResponseFuture(new ResponseFuture(message, queue));
         }
+        
+        // set the cookie policy onto the request
+        message.setCookiePolicy(cookiePolicy);
         
         // *IF* connection reuse is enabled, we should see if we have a cached 
         // connection first; if not, always open a new one

Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java?rev=631262&view=auto
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
(added)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
Tue Feb 26 07:52:08 2008
@@ -0,0 +1,41 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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 org.apache.ahc.codec;
+
+import java.net.URL;
+
+/**
+ * The interface that defines whether the cookie can be associated with the
+ * request.
+ */
+public interface CookiePolicy {
+    /**
+     * Returns whether the cookie can be accepted for the given URL.  Not all
+     * cookies that are accepted can be sent to the server; e.g. an expired
+     * cookie.  However, one can expect all cookies suitable to be sent will be
+     * accepted.
+     */
+    boolean accept(Cookie cookie, URL url);
+    
+    /**
+     * Returns whether the cookie should be sent for the given URL.
+     */
+    boolean matches(Cookie cookie, URL url);
+}

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/CookiePolicy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java?rev=631262&view=auto
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
(added)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
Tue Feb 26 07:52:08 2008
@@ -0,0 +1,201 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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 org.apache.ahc.codec;
+
+import java.net.URL;
+import java.util.Date;
+
+/**
+ * A CookiePolicy implementation that resembles a common browser behavior.  It
+ * uses the domain, path, max-age (or expires), secure attributes, as well as
+ *  the cookie value to examine whether the cookie is applicable for the 
+ *  request.
+ */
+public class DefaultCookiePolicy implements CookiePolicy {
+    private final CookiePolicy[] policies = {
+            new DomainPolicy(),
+            new PathPolicy(),
+            new ExpiresPolicy(),
+            new SecurePolicy(),
+            new EmptyValuePolicy()
+    };
+
+    public boolean accept(Cookie cookie, URL url) {
+        for (CookiePolicy policy : policies) {
+            if (!policy.accept(cookie, url)) {
+                return false;
+            }
+        }
+        return true;
+    }
+    
+    public boolean matches(Cookie cookie, URL url) {
+        for (CookiePolicy policy : policies) {
+            if (!policy.matches(cookie, url)) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
+
+/**
+ * Evaluates the domain attribute.
+ */
+class DomainPolicy implements CookiePolicy {
+    /**
+     * Returns whether the domain matches the request.  Returns true if the 
+     * domain attribute is not empty, and the domain matches the request host
+     * literally or via domain match.
+     */
+    public boolean matches(Cookie cookie, URL url) {
+        String domain = cookie.getDomain();
+        if (domain == null || domain.length() == 0) {
+            // domain is a required attribute
+            return false;
+        }
+        
+        // make sure to add a leading dot
+        // this is to prevent matching "foo.com" against "badfoo.com"
+        if (!domain.startsWith(".")) {
+            domain = "." + domain;
+        }
+        
+        String host = url.getHost();
+        // handle a direct match (i.e. domain is a FQDN)
+        if (domain.substring(1).equalsIgnoreCase(host)) {
+            return true;
+        }
+        
+        // now match it as a domain
+        return host.endsWith(domain);
+    }
+    
+    /**
+     * Same as <tt>matches()</tt>.
+     */
+    public boolean accept(Cookie cookie, URL url) {
+        return matches(cookie, url);
+    }
+}
+
+/**
+ * Evaluates the path attribute.
+ */
+class PathPolicy implements CookiePolicy {
+    /**
+     * Returns whether the path attribute matches the request.
+     */
+    public boolean matches(Cookie cookie, URL url) {
+        String cookiePath = cookie.getPath();
+        if (cookiePath == null || cookiePath.length() == 0) {
+            // path is required
+            return false;
+        }
+        
+        // strip the ending "/" if it's not a simple "/"
+        if (!cookiePath.equals("/") && cookiePath.endsWith("/")) {
+            cookiePath = cookiePath.substring(0, cookiePath.length()-1);
+        }
+        
+        String requestPath = url.getPath();
+        if (requestPath == null || requestPath.length() == 0) {
+            requestPath = "/";
+        }
+        // request path should be prefixed with the cookie path attribute
+        if (!requestPath.startsWith(cookiePath)) {
+            return false;
+        }
+        
+        // check the case like matching "/foo" aginst "/foobad"
+        if (!cookiePath.equals("/") && !requestPath.equals(cookiePath)) {
+            // the first character after the match should be '/'
+            return requestPath.charAt(cookiePath.length()) == '/';
+        }
+        
+        // match
+        return true;
+    }
+    
+    /**
+     * Same as <tt>matches()</tt>.
+     */
+    public boolean accept(Cookie cookie, URL url) {
+        return matches(cookie, url);
+    }
+}
+
+/**
+ * Evaluates the expiration attribute.
+ */
+class ExpiresPolicy implements CookiePolicy {
+    /**
+     * Returns whether the cookie is expired.  Returns true if it is a session
+     * cookie.
+     */
+    public boolean matches(Cookie cookie, URL url) {
+        Date expires = cookie.getExpires();
+        if (expires == null) {
+            // it is a session cookie; accept it
+            return true;
+        }
+        
+        return expires.after(new Date());
+    }
+    
+    /**
+     * Expiration should not be used when accepting.  Expired cookies may be
+     * used to clear existing cookies.
+     */
+    public boolean accept(Cookie cookie, URL url) {
+        return true;
+    }
+}
+
+/**
+ * Evaluates the secure attribute.
+ */
+class SecurePolicy implements CookiePolicy {
+    public boolean matches(Cookie cookie, URL url) {
+        return !cookie.isSecure() || url.getProtocol().equals("https");
+    }
+    
+    /**
+     * Same as <tt>matches()</tt>.
+     */
+    public boolean accept(Cookie cookie, URL url) {
+        return matches(cookie, url);
+    }
+}
+
+class EmptyValuePolicy implements CookiePolicy {
+    public boolean matches(Cookie cookie, URL url) {
+        String value = cookie.getValue();
+        return value != null && value.length() > 0;
+    }
+    
+    /**
+     * We should accept empty cookies as they are used to clear existing 
+     * cookies.
+     */
+    public boolean accept(Cookie cookie, URL url) {
+        return true;
+    }
+}

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpDecoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpDecoder.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpDecoder.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpDecoder.java Tue
Feb 26 07:52:08 2008
@@ -245,7 +245,7 @@
         msg.addHeader(nvp);
 
         if (name.equalsIgnoreCase(SET_COOKIE)) {
-            Cookie cookie = decodeCookie(value);
+            Cookie cookie = decodeCookie(value, msg);
             if (cookie != null) {
                 msg.addCookie(cookie);
             }
@@ -406,7 +406,7 @@
      * @throws Exception if any exception occurs
      * @see Cookie
      */
-    public Cookie decodeCookie(String cookieStr) throws Exception {
+    public Cookie decodeCookie(String cookieStr, HttpResponseMessage msg) throws Exception
{
 
         Cookie cookie = null;
 
@@ -452,6 +452,26 @@
             }
         }
 
+        // supply the hostname as the domain if it is missing
+        if (cookie.getDomain() == null) {
+            cookie.setDomain(msg.getRequestURL().getHost());
+        }
+        
+        // use the path (up to the rightmost "/") as the path attribute if it is
+        // missing
+        if (cookie.getPath() == null) {
+            String path = msg.getRequestURL().getPath();
+            int lastSlash = path.lastIndexOf('/');
+            if (lastSlash == -1) {
+                // if the slash is absent, treat it as the root context
+                path = "/";
+            } else if (lastSlash < path.length() - 1) {
+                // get it up to the rightmost slash
+                path = path.substring(0, lastSlash+1);
+            }
+            cookie.setPath(path);
+        }
+        
         return cookie;
     }
 }

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpMessage.java Tue
Feb 26 07:52:08 2008
@@ -118,7 +118,7 @@
     }
 
     /**
-     * Gets the cookies.
+     * Gets the cookies.  Returns all existing cookies without filtering.
      * 
      * @return the cookies
      */
@@ -128,7 +128,8 @@
 
     /**
      * Sets the cookies on the message.  Any existing cookies will be completely
-     * discarded.
+     * discarded.  Checks on whether the cookies are acceptable may be
+     * performed.
      * 
      * @param cookies the new cookies
      * @see #addCookies(Collection)
@@ -140,14 +141,17 @@
         
         Map<String,Cookie> newCookies = new HashMap<String,Cookie>();
         for (Cookie cookie : cookies) {
-            newCookies.put(cookie.getName(), cookie);
+            if (canAcceptCookie(cookie)) {
+                newCookies.put(cookie.getName(), cookie);
+            }
         }
         this.cookies = newCookies;
     }
     
     /**
-     * Adds the cookies to the message.  Only the existing cookies with the same
-     * names are replaced by the ones in the argument.
+     * Adds the cookies to the message.  If the cookie with the same name
+     * already exists, the cookie will be replaced.  Checks on whether the
+     * cookies are acceptable may be performed.
      * 
      * @see #setCookies(Collection)
      */
@@ -171,7 +175,16 @@
             return;
         }
         
-        this.cookies.put(cookie.getName(), cookie);
+        if (canAcceptCookie(cookie)) {
+            this.cookies.put(cookie.getName(), cookie);
+        }
+    }
+    
+    /**
+     * Returns whether the cookie can be accepted.  Returns true by default.
+     */
+    protected boolean canAcceptCookie(Cookie cookie) {
+        return true;
     }
 
 

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
Tue Feb 26 07:52:08 2008
@@ -21,8 +21,11 @@
 
 import java.net.ProtocolException;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ScheduledFuture;
 
@@ -159,6 +162,8 @@
     
     private ProxyConfiguration proxyConfig;
     
+    private CookiePolicy cookiePolicy;
+    
     private volatile long requestStartTime = 0L;
     
     private volatile long connectStartTime = 0L;
@@ -584,6 +589,56 @@
         return proxyConfig != null && !proxyConfig.isExcluded(getUrl());
     }
     
+    /**
+     * Returns the current cookie policy.
+     */
+    public CookiePolicy getCookiePolicy() {
+        return cookiePolicy;
+    }
+
+    /**
+     * Sets the current cookie policy.
+     */
+    public void setCookiePolicy(CookiePolicy policy) {
+        cookiePolicy = policy;
+    }
+    
+    /**
+     * Returns whether the cookie can be accepted for the request.  Uses the
+     * current cookie policy to evaluate the cookie against the request.  If the
+     * current cookie policy is null, it always returns true.
+     * 
+     * @see CookiePolicy
+     */
+    @Override
+    protected boolean canAcceptCookie(Cookie cookie) {
+        CookiePolicy policy = getCookiePolicy();
+        return policy == null || policy.accept(cookie, url);
+    }
+
+    /**
+     * Returns the set of cookies as filtered by the cookie policy.  Cookies
+     * returned by this method are suitable for including in the Cookie header.
+     * If the current cookie policy is null, all stored cookies are returned.
+     * 
+     * @see CookiePolicy
+     */
+    @Override
+    public Collection<Cookie> getCookies() {
+        CookiePolicy policy = getCookiePolicy();
+        if (policy == null) {
+            return cookies.values();
+        }
+        
+        List<Cookie> list = new ArrayList<Cookie>();
+        for (Cookie cookie : cookies.values()) {
+            if (policy.matches(cookie, url)) {
+                list.add(cookie);
+            }
+        }
+        return list;
+    }
+
     /**
      * Return the time when the request was first initiated.
      * 

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseDecoder.java
Tue Feb 26 07:52:08 2008
@@ -51,7 +51,8 @@
         try {
             HttpResponseMessage response = (HttpResponseMessage)ioSession.getAttribute(HttpIoHandler.CURRENT_RESPONSE);
             if (response == null) {
-                response = new HttpResponseMessage();
+                HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(HttpIoHandler.CURRENT_REQUEST);
+                response = new HttpResponseMessage(request.getUrl());
                 ioSession.setAttribute(HttpIoHandler.CURRENT_RESPONSE, response);
             }
 
@@ -133,7 +134,7 @@
         // specified, in which case connection close marks the end of the body
         if (response != null &&
                 !response.isChunked() &&
-                response.getContentLength() <= 0 &&
+                response.getContentLength() < 0 &&
                 response.getState() == HttpResponseMessage.STATE_HEADERS_READ) {
             completeResponse(session, out, response);
         }

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpResponseMessage.java
Tue Feb 26 07:52:08 2008
@@ -19,10 +19,9 @@
  */
 package org.apache.ahc.codec;
 
-import java.util.HashMap;
+import java.net.URL;
 import java.util.ArrayList;
 
-import org.apache.ahc.auth.AuthState;
 import org.apache.ahc.util.NameValuePair;
 
 /**
@@ -53,6 +52,8 @@
     
     /** The Constant STATE_FINISHED. */
     static final int STATE_FINISHED = 6;
+    
+    private final URL url;
 
     /** The status code. */
     private int statusCode;
@@ -80,6 +81,14 @@
 
     /** The challenge list **/
     private ArrayList<NameValuePair> challenges = new ArrayList<NameValuePair>();
+    
+    public HttpResponseMessage(URL url) {
+        this.url = url;
+    }
+    
+    public URL getRequestURL() {
+        return url;
+    }
     
     /**
      * Gets the HTTP status code.

Added: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java?rev=631262&view=auto
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
(added)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
Tue Feb 26 07:52:08 2008
@@ -0,0 +1,41 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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 org.apache.ahc.codec;
+
+import java.net.URL;
+
+/**
+ * A trivial cookie policy that accepts all cookies.
+ */
+public class NullCookiePolicy implements CookiePolicy {
+    /**
+     * Returns always true.
+     */
+    public boolean accept(Cookie cookie, URL url) {
+        return true;
+    }
+
+    /**
+     * Returns always true.
+     */
+    public boolean matches(Cookie cookie, URL url) {
+        return true;
+    }
+}

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ChunkedTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ChunkedTest.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ChunkedTest.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ChunkedTest.java Tue Feb
26 07:52:08 2008
@@ -23,6 +23,8 @@
 
 import junit.framework.TestCase;
 
+import org.apache.ahc.codec.HttpIoHandler;
+import org.apache.ahc.codec.HttpRequestMessage;
 import org.apache.ahc.codec.HttpResponseDecoder;
 import org.apache.ahc.codec.HttpResponseMessage;
 import org.apache.mina.common.ByteBuffer;
@@ -56,7 +58,9 @@
         buffer.put(FAKE_HTTP.getBytes());
         buffer.flip();
 
+        HttpRequestMessage request = new HttpRequestMessage(null, null);
         IoSession session = new FakeIoSession();
+        session.setAttribute(HttpIoHandler.CURRENT_REQUEST, request);
         HttpResponseDecoder decoder = new HttpResponseDecoder();
         FakeProtocolDecoderOutput out = new FakeProtocolDecoderOutput();
         decoder.decode(session, buffer, out);
@@ -71,7 +75,9 @@
         buffer.put(FAKE_HTTP.getBytes());
         buffer.flip();
 
+        HttpRequestMessage request = new HttpRequestMessage(null, null);
         IoSession session = new FakeIoSession();
+        session.setAttribute(HttpIoHandler.CURRENT_REQUEST, request);
         HttpResponseDecoder decoder = new HttpResponseDecoder();
         FakeProtocolDecoderOutput out = new FakeProtocolDecoderOutput();
         decoder.decode(session, buffer, out);

Added: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java?rev=631262&view=auto
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java (added)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java Tue Feb
26 07:52:08 2008
@@ -0,0 +1,190 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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 org.apache.ahc;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.apache.ahc.codec.Cookie;
+import org.apache.ahc.codec.DefaultCookiePolicy;
+import org.apache.ahc.codec.HttpRequestMessage;
+
+public class CookieTest extends TestCase {
+    public void testCookieDomainFQDNMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain("www.foo.com");
+        cookie.setPath("/");
+        request.addCookie(cookie);
+        
+        assertMatch(request, cookie);
+    }
+    
+    public void testCookieDomainMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://abc.xyz.foo.com"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        request.addCookie(cookie);
+        
+        assertMatch(request, cookie);
+    }
+    
+    public void testCookieDomainNonMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.badfoo.com"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain("foo.com");
+        cookie.setPath("/");
+        request.addCookie(cookie);
+        
+        assertNoMatch(request);
+    }
+    
+    public void testCookiePathTopLevelMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        request.addCookie(cookie);
+        
+        assertMatch(request, cookie);
+    }
+    
+    public void testCookiePathMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/foo/bar?zoh"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/foo");
+        request.addCookie(cookie);
+        
+        assertMatch(request, cookie);
+    }
+    
+    public void testCookiePathNonMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/foobad"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/foo");
+        request.addCookie(cookie);
+        
+        assertNoMatch(request);
+    }
+    
+    public void testFreshCookie() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        cookie.setExpires(new Date(System.currentTimeMillis() + 60*1000L));
+        request.addCookie(cookie);
+        
+        assertMatch(request, cookie);
+    }
+    
+    public void testExpiredCookie() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        cookie.setExpires(new Date(System.currentTimeMillis() - 60*1000L));
+        request.addCookie(cookie);
+
+        assertNoMatch(request);
+    }
+    
+    public void testSessionCookie() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        request.addCookie(cookie);
+
+        assertMatch(request, cookie);
+    }
+
+    public void testCookieSecureMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("https://www.foo.com/"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        cookie.setSecure(true);
+        request.addCookie(cookie);
+        
+        assertMatch(request, cookie);
+    }
+    
+    public void testCookieSecureNonMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "value");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        cookie.setSecure(true);
+        request.addCookie(cookie);
+        
+        assertNoMatch(request);
+    }
+    
+    public void testEmptyCookieNonMatch() throws Exception {
+        HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"),
null);
+        request.setCookiePolicy(new DefaultCookiePolicy());
+
+        Cookie cookie = new Cookie("name", "");
+        cookie.setDomain(".foo.com");
+        cookie.setPath("/");
+        request.addCookie(cookie);
+        
+        assertNoMatch(request);
+    }
+    
+    private void assertMatch(HttpRequestMessage request, Cookie cookie) {
+        Collection<Cookie> cookies = request.getCookies();
+        assertTrue(cookies.size() == 1);
+        assertEquals(cookie, cookies.iterator().next());
+    }
+    
+    private void assertNoMatch(HttpRequestMessage request) {
+        assertTrue(request.getCookies().isEmpty());
+    }
+}

Propchange: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/CookieTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java?rev=631262&r1=631261&r2=631262&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java
Tue Feb 26 07:52:08 2008
@@ -4,6 +4,8 @@
 
 import junit.framework.TestCase;
 
+import org.apache.ahc.codec.HttpIoHandler;
+import org.apache.ahc.codec.HttpRequestMessage;
 import org.apache.ahc.codec.HttpResponseDecoder;
 import org.apache.ahc.codec.HttpResponseMessage;
 import org.apache.mina.common.ByteBuffer;
@@ -28,19 +30,20 @@
         buffer.put(TEST_RESPONSE.getBytes());
         buffer.flip();
 
+        HttpRequestMessage request = new HttpRequestMessage(null, null);
         IoSession session = new FakeIoSession();
-//        session.setAttribute(HttpIoHandler.CURRENT_REQUEST, new HttpRequestMessage(null,
null));
+        session.setAttribute(HttpIoHandler.CURRENT_REQUEST, request);
         HttpResponseDecoder decoder = new HttpResponseDecoder();
         FakeProtocolDecoderOutput out = new FakeProtocolDecoderOutput();
         decoder.decode(session, buffer, out);
 
         HttpResponseMessage response = (HttpResponseMessage)out.getObject();
-        assertEquals("Fri, 15 Feb 2008 20:00:00 GMT", response.getHeader("Date"));
-        assertEquals("foo-bar", response.getHeader("Server"));
-        assertEquals("text/html", response.getHeader("Content-Type"));
-        assertEquals("13", response.getHeader("Content-Length"));
-        assertEquals("close", response.getHeader("Connection"));
-        assertEquals("test-continue\tit-keeps-going and-going-and-going", response.getHeader("Private-Header"));
+        assertEquals("Fri, 15 Feb 2008 20:00:00 GMT", response.getHeader("date"));
+        assertEquals("foo-bar", response.getHeader("server"));
+        assertEquals("text/html", response.getHeader("content-type"));
+        assertEquals("13", response.getHeader("content-length"));
+        assertEquals("close", response.getHeader("connection"));
+        assertEquals("test-continue\tit-keeps-going and-going-and-going", response.getHeader("private-header"));
         
         assertTrue(Arrays.equals(response.getContent(), "<html></html>".getBytes()));
     }



Mime
View raw message