mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/cookie/RFC2965PortAttributeHandler.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with 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,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.impl.cookie;
michael@0 29
michael@0 30 import java.util.StringTokenizer;
michael@0 31
michael@0 32 import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0 33
michael@0 34 import ch.boye.httpclientandroidlib.cookie.ClientCookie;
michael@0 35 import ch.boye.httpclientandroidlib.cookie.Cookie;
michael@0 36 import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler;
michael@0 37 import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
michael@0 38 import ch.boye.httpclientandroidlib.cookie.CookieRestrictionViolationException;
michael@0 39 import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
michael@0 40 import ch.boye.httpclientandroidlib.cookie.SetCookie;
michael@0 41 import ch.boye.httpclientandroidlib.cookie.SetCookie2;
michael@0 42
michael@0 43 /**
michael@0 44 * <tt>"Port"</tt> cookie attribute handler for RFC 2965 cookie spec.
michael@0 45 *
michael@0 46 * @since 4.0
michael@0 47 */
michael@0 48 @Immutable
michael@0 49 public class RFC2965PortAttributeHandler implements CookieAttributeHandler {
michael@0 50
michael@0 51 public RFC2965PortAttributeHandler() {
michael@0 52 super();
michael@0 53 }
michael@0 54
michael@0 55 /**
michael@0 56 * Parses the given Port attribute value (e.g. "8000,8001,8002")
michael@0 57 * into an array of ports.
michael@0 58 *
michael@0 59 * @param portValue port attribute value
michael@0 60 * @return parsed array of ports
michael@0 61 * @throws MalformedCookieException if there is a problem in
michael@0 62 * parsing due to invalid portValue.
michael@0 63 */
michael@0 64 private static int[] parsePortAttribute(final String portValue)
michael@0 65 throws MalformedCookieException {
michael@0 66 StringTokenizer st = new StringTokenizer(portValue, ",");
michael@0 67 int[] ports = new int[st.countTokens()];
michael@0 68 try {
michael@0 69 int i = 0;
michael@0 70 while(st.hasMoreTokens()) {
michael@0 71 ports[i] = Integer.parseInt(st.nextToken().trim());
michael@0 72 if (ports[i] < 0) {
michael@0 73 throw new MalformedCookieException ("Invalid Port attribute.");
michael@0 74 }
michael@0 75 ++i;
michael@0 76 }
michael@0 77 } catch (NumberFormatException e) {
michael@0 78 throw new MalformedCookieException ("Invalid Port "
michael@0 79 + "attribute: " + e.getMessage());
michael@0 80 }
michael@0 81 return ports;
michael@0 82 }
michael@0 83
michael@0 84 /**
michael@0 85 * Returns <tt>true</tt> if the given port exists in the given
michael@0 86 * ports list.
michael@0 87 *
michael@0 88 * @param port port of host where cookie was received from or being sent to.
michael@0 89 * @param ports port list
michael@0 90 * @return true returns <tt>true</tt> if the given port exists in
michael@0 91 * the given ports list; <tt>false</tt> otherwise.
michael@0 92 */
michael@0 93 private static boolean portMatch(int port, int[] ports) {
michael@0 94 boolean portInList = false;
michael@0 95 for (int i = 0, len = ports.length; i < len; i++) {
michael@0 96 if (port == ports[i]) {
michael@0 97 portInList = true;
michael@0 98 break;
michael@0 99 }
michael@0 100 }
michael@0 101 return portInList;
michael@0 102 }
michael@0 103
michael@0 104 /**
michael@0 105 * Parse cookie port attribute.
michael@0 106 */
michael@0 107 public void parse(final SetCookie cookie, final String portValue)
michael@0 108 throws MalformedCookieException {
michael@0 109 if (cookie == null) {
michael@0 110 throw new IllegalArgumentException("Cookie may not be null");
michael@0 111 }
michael@0 112 if (cookie instanceof SetCookie2) {
michael@0 113 SetCookie2 cookie2 = (SetCookie2) cookie;
michael@0 114 if (portValue != null && portValue.trim().length() > 0) {
michael@0 115 int[] ports = parsePortAttribute(portValue);
michael@0 116 cookie2.setPorts(ports);
michael@0 117 }
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 /**
michael@0 122 * Validate cookie port attribute. If the Port attribute was specified
michael@0 123 * in header, the request port must be in cookie's port list.
michael@0 124 */
michael@0 125 public void validate(final Cookie cookie, final CookieOrigin origin)
michael@0 126 throws MalformedCookieException {
michael@0 127 if (cookie == null) {
michael@0 128 throw new IllegalArgumentException("Cookie may not be null");
michael@0 129 }
michael@0 130 if (origin == null) {
michael@0 131 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 132 }
michael@0 133 int port = origin.getPort();
michael@0 134 if (cookie instanceof ClientCookie
michael@0 135 && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
michael@0 136 if (!portMatch(port, cookie.getPorts())) {
michael@0 137 throw new CookieRestrictionViolationException(
michael@0 138 "Port attribute violates RFC 2965: "
michael@0 139 + "Request port not found in cookie's port list.");
michael@0 140 }
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 /**
michael@0 145 * Match cookie port attribute. If the Port attribute is not specified
michael@0 146 * in header, the cookie can be sent to any port. Otherwise, the request port
michael@0 147 * must be in the cookie's port list.
michael@0 148 */
michael@0 149 public boolean match(final Cookie cookie, final CookieOrigin origin) {
michael@0 150 if (cookie == null) {
michael@0 151 throw new IllegalArgumentException("Cookie may not be null");
michael@0 152 }
michael@0 153 if (origin == null) {
michael@0 154 throw new IllegalArgumentException("Cookie origin may not be null");
michael@0 155 }
michael@0 156 int port = origin.getPort();
michael@0 157 if (cookie instanceof ClientCookie
michael@0 158 && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
michael@0 159 if (cookie.getPorts() == null) {
michael@0 160 // Invalid cookie state: port not specified
michael@0 161 return false;
michael@0 162 }
michael@0 163 if (!portMatch(port, cookie.getPorts())) {
michael@0 164 return false;
michael@0 165 }
michael@0 166 }
michael@0 167 return true;
michael@0 168 }
michael@0 169
michael@0 170 }

mercurial