michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.impl.cookie;
michael@0:
michael@0: import java.util.StringTokenizer;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0:
michael@0: import ch.boye.httpclientandroidlib.cookie.ClientCookie;
michael@0: import ch.boye.httpclientandroidlib.cookie.Cookie;
michael@0: import ch.boye.httpclientandroidlib.cookie.CookieAttributeHandler;
michael@0: import ch.boye.httpclientandroidlib.cookie.CookieOrigin;
michael@0: import ch.boye.httpclientandroidlib.cookie.CookieRestrictionViolationException;
michael@0: import ch.boye.httpclientandroidlib.cookie.MalformedCookieException;
michael@0: import ch.boye.httpclientandroidlib.cookie.SetCookie;
michael@0: import ch.boye.httpclientandroidlib.cookie.SetCookie2;
michael@0:
michael@0: /**
michael@0: * "Port" cookie attribute handler for RFC 2965 cookie spec.
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: @Immutable
michael@0: public class RFC2965PortAttributeHandler implements CookieAttributeHandler {
michael@0:
michael@0: public RFC2965PortAttributeHandler() {
michael@0: super();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Parses the given Port attribute value (e.g. "8000,8001,8002")
michael@0: * into an array of ports.
michael@0: *
michael@0: * @param portValue port attribute value
michael@0: * @return parsed array of ports
michael@0: * @throws MalformedCookieException if there is a problem in
michael@0: * parsing due to invalid portValue.
michael@0: */
michael@0: private static int[] parsePortAttribute(final String portValue)
michael@0: throws MalformedCookieException {
michael@0: StringTokenizer st = new StringTokenizer(portValue, ",");
michael@0: int[] ports = new int[st.countTokens()];
michael@0: try {
michael@0: int i = 0;
michael@0: while(st.hasMoreTokens()) {
michael@0: ports[i] = Integer.parseInt(st.nextToken().trim());
michael@0: if (ports[i] < 0) {
michael@0: throw new MalformedCookieException ("Invalid Port attribute.");
michael@0: }
michael@0: ++i;
michael@0: }
michael@0: } catch (NumberFormatException e) {
michael@0: throw new MalformedCookieException ("Invalid Port "
michael@0: + "attribute: " + e.getMessage());
michael@0: }
michael@0: return ports;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns true if the given port exists in the given
michael@0: * ports list.
michael@0: *
michael@0: * @param port port of host where cookie was received from or being sent to.
michael@0: * @param ports port list
michael@0: * @return true returns true if the given port exists in
michael@0: * the given ports list; false otherwise.
michael@0: */
michael@0: private static boolean portMatch(int port, int[] ports) {
michael@0: boolean portInList = false;
michael@0: for (int i = 0, len = ports.length; i < len; i++) {
michael@0: if (port == ports[i]) {
michael@0: portInList = true;
michael@0: break;
michael@0: }
michael@0: }
michael@0: return portInList;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Parse cookie port attribute.
michael@0: */
michael@0: public void parse(final SetCookie cookie, final String portValue)
michael@0: throws MalformedCookieException {
michael@0: if (cookie == null) {
michael@0: throw new IllegalArgumentException("Cookie may not be null");
michael@0: }
michael@0: if (cookie instanceof SetCookie2) {
michael@0: SetCookie2 cookie2 = (SetCookie2) cookie;
michael@0: if (portValue != null && portValue.trim().length() > 0) {
michael@0: int[] ports = parsePortAttribute(portValue);
michael@0: cookie2.setPorts(ports);
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Validate cookie port attribute. If the Port attribute was specified
michael@0: * in header, the request port must be in cookie's port list.
michael@0: */
michael@0: public void validate(final Cookie cookie, final CookieOrigin origin)
michael@0: throws MalformedCookieException {
michael@0: if (cookie == null) {
michael@0: throw new IllegalArgumentException("Cookie may not be null");
michael@0: }
michael@0: if (origin == null) {
michael@0: throw new IllegalArgumentException("Cookie origin may not be null");
michael@0: }
michael@0: int port = origin.getPort();
michael@0: if (cookie instanceof ClientCookie
michael@0: && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
michael@0: if (!portMatch(port, cookie.getPorts())) {
michael@0: throw new CookieRestrictionViolationException(
michael@0: "Port attribute violates RFC 2965: "
michael@0: + "Request port not found in cookie's port list.");
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: /**
michael@0: * Match cookie port attribute. If the Port attribute is not specified
michael@0: * in header, the cookie can be sent to any port. Otherwise, the request port
michael@0: * must be in the cookie's port list.
michael@0: */
michael@0: public boolean match(final Cookie cookie, final CookieOrigin origin) {
michael@0: if (cookie == null) {
michael@0: throw new IllegalArgumentException("Cookie may not be null");
michael@0: }
michael@0: if (origin == null) {
michael@0: throw new IllegalArgumentException("Cookie origin may not be null");
michael@0: }
michael@0: int port = origin.getPort();
michael@0: if (cookie instanceof ClientCookie
michael@0: && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) {
michael@0: if (cookie.getPorts() == null) {
michael@0: // Invalid cookie state: port not specified
michael@0: return false;
michael@0: }
michael@0: if (!portMatch(port, cookie.getPorts())) {
michael@0: return false;
michael@0: }
michael@0: }
michael@0: return true;
michael@0: }
michael@0:
michael@0: }