Search in sources :

Example 6 with MalformedCookieException

use of org.apache.http.cookie.MalformedCookieException in project XobotOS by xamarin.

the class RFC2965PortAttributeHandler method parsePortAttribute.

/**
     * Parses the given Port attribute value (e.g. "8000,8001,8002")
     * into an array of ports.
     *
     * @param portValue port attribute value
     * @return parsed array of ports
     * @throws MalformedCookieException if there is a problem in
     *          parsing due to invalid portValue.
     */
private static int[] parsePortAttribute(final String portValue) throws MalformedCookieException {
    StringTokenizer st = new StringTokenizer(portValue, ",");
    int[] ports = new int[st.countTokens()];
    try {
        int i = 0;
        while (st.hasMoreTokens()) {
            ports[i] = Integer.parseInt(st.nextToken().trim());
            if (ports[i] < 0) {
                throw new MalformedCookieException("Invalid Port attribute.");
            }
            ++i;
        }
    } catch (NumberFormatException e) {
        throw new MalformedCookieException("Invalid Port " + "attribute: " + e.getMessage());
    }
    return ports;
}
Also used : StringTokenizer(java.util.StringTokenizer) MalformedCookieException(org.apache.http.cookie.MalformedCookieException)

Example 7 with MalformedCookieException

use of org.apache.http.cookie.MalformedCookieException in project platform_external_apache-http by android.

the class NetscapeDomainHandler method validate.

@Override
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
    super.validate(cookie, origin);
    // Perform Netscape Cookie draft specific validation
    String host = origin.getHost();
    String domain = cookie.getDomain();
    if (host.contains(".")) {
        int domainParts = new StringTokenizer(domain, ".").countTokens();
        if (isSpecialDomain(domain)) {
            if (domainParts < 2) {
                throw new MalformedCookieException("Domain attribute \"" + domain + "\" violates the Netscape cookie specification for " + "special domains");
            }
        } else {
            if (domainParts < 3) {
                throw new MalformedCookieException("Domain attribute \"" + domain + "\" violates the Netscape cookie specification");
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) MalformedCookieException(org.apache.http.cookie.MalformedCookieException)

Example 8 with MalformedCookieException

use of org.apache.http.cookie.MalformedCookieException in project platform_external_apache-http by android.

the class NetscapeDraftSpec method parse.

/**
      * Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
      *
      * <p>Syntax of the Set-Cookie HTTP Response Header:</p>
      * 
      * <p>This is the format a CGI script would use to add to 
      * the HTTP headers a new piece of data which is to be stored by 
      * the client for later retrieval.</p>
      *  
      * <PRE>
      *  Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
      * </PRE>
      *
      * <p>Please note that Netscape draft specification does not fully 
      * conform to the HTTP header format. Netscape draft does not specify 
      * whether multiple cookies may be sent in one header. Hence, comma 
      * character may be present in unquoted cookie value or unquoted 
      * parameter value.</p>
      * 
      * @see <a href="http://wp.netscape.com/newsref/std/cookie_spec.html">
      *  The Cookie Spec.</a>
      *
      * @param header the <tt>Set-Cookie</tt> received from the server
      * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
      * @throws MalformedCookieException if an exception occurs during parsing
      */
public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException {
    if (header == null) {
        throw new IllegalArgumentException("Header may not be null");
    }
    if (origin == null) {
        throw new IllegalArgumentException("Cookie origin may not be null");
    }
    NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
    CharArrayBuffer buffer;
    ParserCursor cursor;
    if (header instanceof FormattedHeader) {
        buffer = ((FormattedHeader) header).getBuffer();
        cursor = new ParserCursor(((FormattedHeader) header).getValuePos(), buffer.length());
    } else {
        String s = header.getValue();
        if (s == null) {
            throw new MalformedCookieException("Header value is null");
        }
        buffer = new CharArrayBuffer(s.length());
        buffer.append(s);
        cursor = new ParserCursor(0, buffer.length());
    }
    return parse(new HeaderElement[] { parser.parseHeader(buffer, cursor) }, origin);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) FormattedHeader(org.apache.http.FormattedHeader)

Example 9 with MalformedCookieException

use of org.apache.http.cookie.MalformedCookieException in project platform_external_apache-http by android.

the class BasicMaxAgeHandler method parse.

public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
    if (cookie == null) {
        throw new IllegalArgumentException("Cookie may not be null");
    }
    if (value == null) {
        throw new MalformedCookieException("Missing value for max-age attribute");
    }
    int age;
    try {
        age = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        throw new MalformedCookieException("Invalid max-age attribute: " + value);
    }
    if (age < 0) {
        throw new MalformedCookieException("Negative max-age attribute: " + value);
    }
    cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
}
Also used : MalformedCookieException(org.apache.http.cookie.MalformedCookieException) Date(java.util.Date)

Example 10 with MalformedCookieException

use of org.apache.http.cookie.MalformedCookieException in project platform_external_apache-http by android.

the class RFC2965PortAttributeHandler method parsePortAttribute.

/**
     * Parses the given Port attribute value (e.g. "8000,8001,8002")
     * into an array of ports.
     *
     * @param portValue port attribute value
     * @return parsed array of ports
     * @throws MalformedCookieException if there is a problem in
     *          parsing due to invalid portValue.
     */
private static int[] parsePortAttribute(final String portValue) throws MalformedCookieException {
    StringTokenizer st = new StringTokenizer(portValue, ",");
    int[] ports = new int[st.countTokens()];
    try {
        int i = 0;
        while (st.hasMoreTokens()) {
            ports[i] = Integer.parseInt(st.nextToken().trim());
            if (ports[i] < 0) {
                throw new MalformedCookieException("Invalid Port attribute.");
            }
            ++i;
        }
    } catch (NumberFormatException e) {
        throw new MalformedCookieException("Invalid Port " + "attribute: " + e.getMessage());
    }
    return ports;
}
Also used : StringTokenizer(java.util.StringTokenizer) MalformedCookieException(org.apache.http.cookie.MalformedCookieException)

Aggregations

MalformedCookieException (org.apache.http.cookie.MalformedCookieException)30 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)11 HeaderElement (org.apache.http.HeaderElement)9 Cookie (org.apache.http.cookie.Cookie)9 ArrayList (java.util.ArrayList)6 StringTokenizer (java.util.StringTokenizer)6 FormattedHeader (org.apache.http.FormattedHeader)6 NameValuePair (org.apache.http.NameValuePair)6 CookieOrigin (org.apache.http.cookie.CookieOrigin)6 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)6 ParserCursor (org.apache.http.message.ParserCursor)6 CharArrayBuffer (org.apache.http.util.CharArrayBuffer)6 Test (org.junit.Test)5 Date (java.util.Date)4 ClientCookie (org.apache.http.cookie.ClientCookie)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Header (org.apache.http.Header)3 BasicHeader (org.apache.http.message.BasicHeader)1