Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 5 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 6 | * this work for additional information regarding copyright ownership. |
michael@0 | 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 8 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 9 | * the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | * ==================================================================== |
michael@0 | 19 | * |
michael@0 | 20 | * This software consists of voluntary contributions made by many |
michael@0 | 21 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 22 | * information on the Apache Software Foundation, please see |
michael@0 | 23 | * <http://www.apache.org/>. |
michael@0 | 24 | * |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | package ch.boye.httpclientandroidlib.impl.cookie; |
michael@0 | 28 | |
michael@0 | 29 | import java.util.ArrayList; |
michael@0 | 30 | import java.util.HashMap; |
michael@0 | 31 | import java.util.List; |
michael@0 | 32 | import java.util.Locale; |
michael@0 | 33 | import java.util.Map; |
michael@0 | 34 | |
michael@0 | 35 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 36 | |
michael@0 | 37 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 38 | import ch.boye.httpclientandroidlib.HeaderElement; |
michael@0 | 39 | import ch.boye.httpclientandroidlib.NameValuePair; |
michael@0 | 40 | import ch.boye.httpclientandroidlib.cookie.ClientCookie; |
michael@0 | 41 | import ch.boye.httpclientandroidlib.cookie.Cookie; |
michael@0 | 42 | import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler; |
michael@0 | 43 | import ch.boye.httpclientandroidlib.cookie.CookieOrigin; |
michael@0 | 44 | import ch.boye.httpclientandroidlib.cookie.CookieSpec; |
michael@0 | 45 | import ch.boye.httpclientandroidlib.cookie.MalformedCookieException; |
michael@0 | 46 | import ch.boye.httpclientandroidlib.cookie.SM; |
michael@0 | 47 | import ch.boye.httpclientandroidlib.message.BufferedHeader; |
michael@0 | 48 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * RFC 2965 compliant {@link CookieSpec} implementation. |
michael@0 | 52 | * |
michael@0 | 53 | * @since 4.0 |
michael@0 | 54 | */ |
michael@0 | 55 | @NotThreadSafe // superclass is @NotThreadSafe |
michael@0 | 56 | public class RFC2965Spec extends RFC2109Spec { |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Default constructor |
michael@0 | 60 | * |
michael@0 | 61 | */ |
michael@0 | 62 | public RFC2965Spec() { |
michael@0 | 63 | this(null, false); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | public RFC2965Spec(final String[] datepatterns, boolean oneHeader) { |
michael@0 | 67 | super(datepatterns, oneHeader); |
michael@0 | 68 | registerAttribHandler(ClientCookie.DOMAIN_ATTR, new RFC2965DomainAttributeHandler()); |
michael@0 | 69 | registerAttribHandler(ClientCookie.PORT_ATTR, new RFC2965PortAttributeHandler()); |
michael@0 | 70 | registerAttribHandler(ClientCookie.COMMENTURL_ATTR, new RFC2965CommentUrlAttributeHandler()); |
michael@0 | 71 | registerAttribHandler(ClientCookie.DISCARD_ATTR, new RFC2965DiscardAttributeHandler()); |
michael@0 | 72 | registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2965VersionAttributeHandler()); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | @Override |
michael@0 | 76 | public List<Cookie> parse( |
michael@0 | 77 | final Header header, |
michael@0 | 78 | CookieOrigin origin) throws MalformedCookieException { |
michael@0 | 79 | if (header == null) { |
michael@0 | 80 | throw new IllegalArgumentException("Header may not be null"); |
michael@0 | 81 | } |
michael@0 | 82 | if (origin == null) { |
michael@0 | 83 | throw new IllegalArgumentException("Cookie origin may not be null"); |
michael@0 | 84 | } |
michael@0 | 85 | if (!header.getName().equalsIgnoreCase(SM.SET_COOKIE2)) { |
michael@0 | 86 | throw new MalformedCookieException("Unrecognized cookie header '" |
michael@0 | 87 | + header.toString() + "'"); |
michael@0 | 88 | } |
michael@0 | 89 | origin = adjustEffectiveHost(origin); |
michael@0 | 90 | HeaderElement[] elems = header.getElements(); |
michael@0 | 91 | return createCookies(elems, origin); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | @Override |
michael@0 | 95 | protected List<Cookie> parse( |
michael@0 | 96 | final HeaderElement[] elems, |
michael@0 | 97 | CookieOrigin origin) throws MalformedCookieException { |
michael@0 | 98 | origin = adjustEffectiveHost(origin); |
michael@0 | 99 | return createCookies(elems, origin); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | private List<Cookie> createCookies( |
michael@0 | 103 | final HeaderElement[] elems, |
michael@0 | 104 | final CookieOrigin origin) throws MalformedCookieException { |
michael@0 | 105 | List<Cookie> cookies = new ArrayList<Cookie>(elems.length); |
michael@0 | 106 | for (HeaderElement headerelement : elems) { |
michael@0 | 107 | String name = headerelement.getName(); |
michael@0 | 108 | String value = headerelement.getValue(); |
michael@0 | 109 | if (name == null || name.length() == 0) { |
michael@0 | 110 | throw new MalformedCookieException("Cookie name may not be empty"); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | BasicClientCookie2 cookie = new BasicClientCookie2(name, value); |
michael@0 | 114 | cookie.setPath(getDefaultPath(origin)); |
michael@0 | 115 | cookie.setDomain(getDefaultDomain(origin)); |
michael@0 | 116 | cookie.setPorts(new int [] { origin.getPort() }); |
michael@0 | 117 | // cycle through the parameters |
michael@0 | 118 | NameValuePair[] attribs = headerelement.getParameters(); |
michael@0 | 119 | |
michael@0 | 120 | // Eliminate duplicate attributes. The first occurrence takes precedence |
michael@0 | 121 | // See RFC2965: 3.2 Origin Server Role |
michael@0 | 122 | Map<String, NameValuePair> attribmap = |
michael@0 | 123 | new HashMap<String, NameValuePair>(attribs.length); |
michael@0 | 124 | for (int j = attribs.length - 1; j >= 0; j--) { |
michael@0 | 125 | NameValuePair param = attribs[j]; |
michael@0 | 126 | attribmap.put(param.getName().toLowerCase(Locale.ENGLISH), param); |
michael@0 | 127 | } |
michael@0 | 128 | for (Map.Entry<String, NameValuePair> entry : attribmap.entrySet()) { |
michael@0 | 129 | NameValuePair attrib = entry.getValue(); |
michael@0 | 130 | String s = attrib.getName().toLowerCase(Locale.ENGLISH); |
michael@0 | 131 | |
michael@0 | 132 | cookie.setAttribute(s, attrib.getValue()); |
michael@0 | 133 | |
michael@0 | 134 | CookieAttributeHandler handler = findAttribHandler(s); |
michael@0 | 135 | if (handler != null) { |
michael@0 | 136 | handler.parse(cookie, attrib.getValue()); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | cookies.add(cookie); |
michael@0 | 140 | } |
michael@0 | 141 | return cookies; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | @Override |
michael@0 | 145 | public void validate(final Cookie cookie, CookieOrigin origin) |
michael@0 | 146 | throws MalformedCookieException { |
michael@0 | 147 | if (cookie == null) { |
michael@0 | 148 | throw new IllegalArgumentException("Cookie may not be null"); |
michael@0 | 149 | } |
michael@0 | 150 | if (origin == null) { |
michael@0 | 151 | throw new IllegalArgumentException("Cookie origin may not be null"); |
michael@0 | 152 | } |
michael@0 | 153 | origin = adjustEffectiveHost(origin); |
michael@0 | 154 | super.validate(cookie, origin); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | @Override |
michael@0 | 158 | public boolean match(final Cookie cookie, CookieOrigin origin) { |
michael@0 | 159 | if (cookie == null) { |
michael@0 | 160 | throw new IllegalArgumentException("Cookie may not be null"); |
michael@0 | 161 | } |
michael@0 | 162 | if (origin == null) { |
michael@0 | 163 | throw new IllegalArgumentException("Cookie origin may not be null"); |
michael@0 | 164 | } |
michael@0 | 165 | origin = adjustEffectiveHost(origin); |
michael@0 | 166 | return super.match(cookie, origin); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | /** |
michael@0 | 170 | * Adds valid Port attribute value, e.g. "8000,8001,8002" |
michael@0 | 171 | */ |
michael@0 | 172 | @Override |
michael@0 | 173 | protected void formatCookieAsVer(final CharArrayBuffer buffer, |
michael@0 | 174 | final Cookie cookie, int version) { |
michael@0 | 175 | super.formatCookieAsVer(buffer, cookie, version); |
michael@0 | 176 | // format port attribute |
michael@0 | 177 | if (cookie instanceof ClientCookie) { |
michael@0 | 178 | // Test if the port attribute as set by the origin server is not blank |
michael@0 | 179 | String s = ((ClientCookie) cookie).getAttribute(ClientCookie.PORT_ATTR); |
michael@0 | 180 | if (s != null) { |
michael@0 | 181 | buffer.append("; $Port"); |
michael@0 | 182 | buffer.append("=\""); |
michael@0 | 183 | if (s.trim().length() > 0) { |
michael@0 | 184 | int[] ports = cookie.getPorts(); |
michael@0 | 185 | if (ports != null) { |
michael@0 | 186 | for (int i = 0, len = ports.length; i < len; i++) { |
michael@0 | 187 | if (i > 0) { |
michael@0 | 188 | buffer.append(","); |
michael@0 | 189 | } |
michael@0 | 190 | buffer.append(Integer.toString(ports[i])); |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | buffer.append("\""); |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | /** |
michael@0 | 200 | * Set 'effective host name' as defined in RFC 2965. |
michael@0 | 201 | * <p> |
michael@0 | 202 | * If a host name contains no dots, the effective host name is |
michael@0 | 203 | * that name with the string .local appended to it. Otherwise |
michael@0 | 204 | * the effective host name is the same as the host name. Note |
michael@0 | 205 | * that all effective host names contain at least one dot. |
michael@0 | 206 | * |
michael@0 | 207 | * @param origin origin where cookie is received from or being sent to. |
michael@0 | 208 | * @return |
michael@0 | 209 | */ |
michael@0 | 210 | private static CookieOrigin adjustEffectiveHost(final CookieOrigin origin) { |
michael@0 | 211 | String host = origin.getHost(); |
michael@0 | 212 | |
michael@0 | 213 | // Test if the host name appears to be a fully qualified DNS name, |
michael@0 | 214 | // IPv4 address or IPv6 address |
michael@0 | 215 | boolean isLocalHost = true; |
michael@0 | 216 | for (int i = 0; i < host.length(); i++) { |
michael@0 | 217 | char ch = host.charAt(i); |
michael@0 | 218 | if (ch == '.' || ch == ':') { |
michael@0 | 219 | isLocalHost = false; |
michael@0 | 220 | break; |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | if (isLocalHost) { |
michael@0 | 224 | host += ".local"; |
michael@0 | 225 | return new CookieOrigin( |
michael@0 | 226 | host, |
michael@0 | 227 | origin.getPort(), |
michael@0 | 228 | origin.getPath(), |
michael@0 | 229 | origin.isSecure()); |
michael@0 | 230 | } else { |
michael@0 | 231 | return origin; |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | @Override |
michael@0 | 236 | public int getVersion() { |
michael@0 | 237 | return 1; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | @Override |
michael@0 | 241 | public Header getVersionHeader() { |
michael@0 | 242 | CharArrayBuffer buffer = new CharArrayBuffer(40); |
michael@0 | 243 | buffer.append(SM.COOKIE2); |
michael@0 | 244 | buffer.append(": "); |
michael@0 | 245 | buffer.append("$Version="); |
michael@0 | 246 | buffer.append(Integer.toString(getVersion())); |
michael@0 | 247 | return new BufferedHeader(buffer); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | @Override |
michael@0 | 251 | public String toString() { |
michael@0 | 252 | return "rfc2965"; |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | } |
michael@0 | 256 |