Search in sources :

Example 21 with MalformedCookieException

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

the class ResponseProcessCookies method processCookies.

private void processCookies(final HeaderIterator iterator, final CookieSpec cookieSpec, final CookieOrigin cookieOrigin, final CookieStore cookieStore) {
    while (iterator.hasNext()) {
        Header header = iterator.nextHeader();
        try {
            List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin);
            for (Cookie cookie : cookies) {
                try {
                    cookieSpec.validate(cookie, cookieOrigin);
                    cookieStore.addCookie(cookie);
                    if (this.log.isDebugEnabled()) {
                        // BEGIN android-changed
                        this.log.debug("Cookie accepted: \"" + cookieToString(cookie) + "\". ");
                    // END android-changed
                    }
                } catch (MalformedCookieException ex) {
                    if (this.log.isWarnEnabled()) {
                        // BEGIN android-changed
                        this.log.warn("Cookie rejected: \"" + cookieToString(cookie) + "\". " + ex.getMessage());
                    // END android-changed
                    }
                }
            }
        } catch (MalformedCookieException ex) {
            if (this.log.isWarnEnabled()) {
                this.log.warn("Invalid cookie header: \"" + header + "\". " + ex.getMessage());
            }
        }
    }
}
Also used : Cookie(org.apache.http.cookie.Cookie) Header(org.apache.http.Header) MalformedCookieException(org.apache.http.cookie.MalformedCookieException)

Example 22 with MalformedCookieException

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

the class RFC2965Spec method parse.

@Override
public List<Cookie> parse(final Header header, 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");
    }
    origin = adjustEffectiveHost(origin);
    HeaderElement[] elems = header.getElements();
    List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    for (HeaderElement headerelement : elems) {
        String name = headerelement.getName();
        String value = headerelement.getValue();
        if (name == null || name.length() == 0) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        BasicClientCookie cookie;
        if (header.getName().equals(SM.SET_COOKIE2)) {
            cookie = createCookie2(name, value, origin);
        } else {
            cookie = createCookie(name, value, origin);
        }
        // cycle through the parameters
        NameValuePair[] attribs = headerelement.getParameters();
        // Eliminate duplicate attributes. The first occurrence takes precedence
        // See RFC2965: 3.2  Origin Server Role
        Map<String, NameValuePair> attribmap = new HashMap<String, NameValuePair>(attribs.length);
        for (int j = attribs.length - 1; j >= 0; j--) {
            NameValuePair param = attribs[j];
            attribmap.put(param.getName().toLowerCase(Locale.ENGLISH), param);
        }
        for (Map.Entry<String, NameValuePair> entry : attribmap.entrySet()) {
            NameValuePair attrib = entry.getValue();
            String s = attrib.getName().toLowerCase(Locale.ENGLISH);
            cookie.setAttribute(s, attrib.getValue());
            CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        cookies.add(cookie);
    }
    return cookies;
}
Also used : ClientCookie(org.apache.http.cookie.ClientCookie) Cookie(org.apache.http.cookie.Cookie) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) HeaderElement(org.apache.http.HeaderElement) CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 23 with MalformedCookieException

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

the class CookieSpecBase method parse.

protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException {
    List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
    for (HeaderElement headerelement : elems) {
        String name = headerelement.getName();
        String value = headerelement.getValue();
        if (name == null || name.length() == 0) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        BasicClientCookie cookie = new BasicClientCookie(name, value);
        cookie.setPath(getDefaultPath(origin));
        cookie.setDomain(getDefaultDomain(origin));
        // cycle through the parameters
        NameValuePair[] attribs = headerelement.getParameters();
        for (int j = attribs.length - 1; j >= 0; j--) {
            NameValuePair attrib = attribs[j];
            String s = attrib.getName().toLowerCase(Locale.ENGLISH);
            cookie.setAttribute(s, attrib.getValue());
            CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        cookies.add(cookie);
    }
    return cookies;
}
Also used : Cookie(org.apache.http.cookie.Cookie) NameValuePair(org.apache.http.NameValuePair) HeaderElement(org.apache.http.HeaderElement) CookieAttributeHandler(org.apache.http.cookie.CookieAttributeHandler) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) ArrayList(java.util.ArrayList)

Example 24 with MalformedCookieException

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

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 25 with MalformedCookieException

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

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)

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