Example usage for org.apache.http.impl.cookie NetscapeDraftHeaderParser parseHeader

List of usage examples for org.apache.http.impl.cookie NetscapeDraftHeaderParser parseHeader

Introduction

In this page you can find the example usage for org.apache.http.impl.cookie NetscapeDraftHeaderParser parseHeader.

Prototype

public HeaderElement parseHeader(final CharArrayBuffer buffer, final ParserCursor cursor)
            throws ParseException 

Source Link

Usage

From source file:com.gargoylesoftware.htmlunit.httpclient.HtmlUnitBrowserCompatCookieSpec.java

/**
 * {@inheritDoc}//from   ww w. j  a v a 2  s. c  o m
 */
@Override
public List<Cookie> parse(Header header, final CookieOrigin origin) throws MalformedCookieException {
    // first a hack to support empty headers
    final String text = header.getValue();
    int endPos = text.indexOf(';');
    if (endPos < 0) {
        endPos = text.indexOf('=');
    } else {
        final int pos = text.indexOf('=');
        if (pos > endPos) {
            endPos = -1;
        } else {
            endPos = pos;
        }
    }
    if (endPos < 0) {
        header = new BasicHeader(header.getName(), EMPTY_COOKIE_NAME + "=" + header.getValue());
    } else if (endPos == 0 || StringUtils.isBlank(text.substring(0, endPos))) {
        header = new BasicHeader(header.getName(), EMPTY_COOKIE_NAME + header.getValue());
    }

    final List<Cookie> cookies;

    final String headername = header.getName();
    if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) {
        throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'");
    }
    final HeaderElement[] helems = header.getElements();
    boolean versioned = false;
    boolean netscape = false;
    for (final HeaderElement helem : helems) {
        if (helem.getParameterByName("version") != null) {
            versioned = true;
        }
        if (helem.getParameterByName("expires") != null) {
            netscape = true;
        }
    }
    if (netscape || !versioned) {
        // Need to parse the header again, because Netscape style cookies do not correctly
        // support multiple header elements (comma cannot be treated as an element separator)
        final NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
        final CharArrayBuffer buffer;
        final ParserCursor cursor;
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            cursor = new ParserCursor(((FormattedHeader) header).getValuePos(), buffer.length());
        } else {
            final 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());
        }
        final HeaderElement elem = parser.parseHeader(buffer, cursor);
        final String name = elem.getName();
        final String value = elem.getValue();
        if (name == null || name.isEmpty()) {
            throw new MalformedCookieException("Cookie name may not be empty");
        }
        final BasicClientCookie cookie = new BasicClientCookie(name, value);
        cookie.setPath(getDefaultPath(origin));
        cookie.setDomain(getDefaultDomain(origin));

        // cycle through the parameters
        final NameValuePair[] attribs = elem.getParameters();
        for (int j = attribs.length - 1; j >= 0; j--) {
            final NameValuePair attrib = attribs[j];
            final String s = attrib.getName().toLowerCase(Locale.ROOT);
            cookie.setAttribute(s, attrib.getValue());
            final CookieAttributeHandler handler = findAttribHandler(s);
            if (handler != null) {
                handler.parse(cookie, attrib.getValue());
            }
        }
        // Override version for Netscape style cookies
        if (netscape) {
            cookie.setVersion(0);
        }
        cookies = Collections.<Cookie>singletonList(cookie);
    } else {
        cookies = parse(helems, origin);
    }

    for (final Cookie c : cookies) {
        // re-add quotes around value if parsing as incorrectly trimmed them
        if (header.getValue().contains(c.getName() + "=\"" + c.getValue())) {
            ((BasicClientCookie) c).setValue('"' + c.getValue() + '"');
        }
    }
    return cookies;
}