Search in sources :

Example 6 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class DefaultConnectionKeepAliveStrategy method getKeepAliveDuration.

public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
    while (it.hasNext()) {
        HeaderElement he = it.nextElement();
        String param = he.getName();
        String value = he.getValue();
        if (value != null && param.equalsIgnoreCase("timeout")) {
            try {
                return Long.parseLong(value) * 1000;
            } catch (NumberFormatException ignore) {
            }
        }
    }
    return -1;
}
Also used : BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) HeaderElement(org.apache.http.HeaderElement) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator)

Example 7 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class BasicHeaderElementIterator method parseNextElement.

private void parseNextElement() {
    // loop while there are headers left to parse
    while (this.headerIt.hasNext() || this.cursor != null) {
        if (this.cursor == null || this.cursor.atEnd()) {
            // get next header value
            bufferHeaderValue();
        }
        // Anything buffered?
        if (this.cursor != null) {
            // loop while there is data in the buffer 
            while (!this.cursor.atEnd()) {
                HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor);
                if (!(e.getName().length() == 0 && e.getValue() == null)) {
                    // Found something
                    this.currentElement = e;
                    return;
                }
            }
            // if at the end of the buffer
            if (this.cursor.atEnd()) {
                // discard it
                this.cursor = null;
                this.buffer = null;
            }
        }
    }
}
Also used : HeaderElement(org.apache.http.HeaderElement)

Example 8 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class BasicHeaderElementIterator method nextElement.

public HeaderElement nextElement() throws NoSuchElementException {
    if (this.currentElement == null) {
        parseNextElement();
    }
    if (this.currentElement == null) {
        throw new NoSuchElementException("No more header elements available");
    }
    HeaderElement element = this.currentElement;
    this.currentElement = null;
    return element;
}
Also used : HeaderElement(org.apache.http.HeaderElement) NoSuchElementException(java.util.NoSuchElementException)

Example 9 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class BestMatchSpec method parse.

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");
    }
    HeaderElement[] helems = header.getElements();
    boolean versioned = false;
    boolean netscape = false;
    for (HeaderElement helem : helems) {
        if (helem.getParameterByName("version") != null) {
            versioned = true;
        }
        if (helem.getParameterByName("expires") != null) {
            netscape = true;
        }
    }
    if (netscape) {
    }
    // Do we have a cookie with a version attribute?
    if (versioned) {
        return getStrict().parse(helems, origin);
    } else if (netscape) {
        // comma separators
        return getNetscape().parse(header, origin);
    } else {
        return getCompat().parse(helems, origin);
    }
}
Also used : HeaderElement(org.apache.http.HeaderElement)

Example 10 with HeaderElement

use of org.apache.http.HeaderElement in project XobotOS by xamarin.

the class BrowserCompatSpec method parse.

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");
    }
    String headervalue = header.getValue();
    boolean isNetscapeCookie = false;
    int i1 = headervalue.toLowerCase(Locale.ENGLISH).indexOf("expires=");
    if (i1 != -1) {
        i1 += "expires=".length();
        int i2 = headervalue.indexOf(';', i1);
        if (i2 == -1) {
            i2 = headervalue.length();
        }
        try {
            DateUtils.parseDate(headervalue.substring(i1, i2), this.datepatterns);
            isNetscapeCookie = true;
        } catch (DateParseException e) {
        // Does not look like a valid expiry date
        }
    }
    HeaderElement[] elems = null;
    if (isNetscapeCookie) {
        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());
        }
        elems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
    } else {
        elems = header.getElements();
    }
    return parse(elems, origin);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) HeaderElement(org.apache.http.HeaderElement) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) FormattedHeader(org.apache.http.FormattedHeader)

Aggregations

HeaderElement (org.apache.http.HeaderElement)58 Header (org.apache.http.Header)17 NameValuePair (org.apache.http.NameValuePair)14 HeaderElementIterator (org.apache.http.HeaderElementIterator)10 BasicHeaderElementIterator (org.apache.http.message.BasicHeaderElementIterator)10 HttpResponse (org.apache.http.HttpResponse)9 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)9 ParserCursor (org.apache.http.message.ParserCursor)9 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Cookie (org.apache.http.cookie.Cookie)5 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)5 HttpContext (org.apache.http.protocol.HttpContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)4 HashSet (java.util.HashSet)3 NoSuchElementException (java.util.NoSuchElementException)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3