Example usage for org.apache.http.util CharArrayBuffer charAt

List of usage examples for org.apache.http.util CharArrayBuffer charAt

Introduction

In this page you can find the example usage for org.apache.http.util CharArrayBuffer charAt.

Prototype

public char charAt(int i) 

Source Link

Usage

From source file:org.mycard.net.network.AndroidHttpClientConnection.java

/***
 * Parses the response headers and adds them to the
 * given {@code headers} object, and returns the response StatusLine
 * @param headers store parsed header to headers.
 * @throws IOException/*from  ww w . j  a  v  a  2s . c  o m*/
 * @return StatusLine
 * @see HttpClientConnection#receiveResponseHeader()
  */
public StatusLine parseResponseHeader(Headers headers) throws IOException, ParseException {
    assertOpen();

    CharArrayBuffer current = new CharArrayBuffer(64);

    if (inbuffer.readLine(current) == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }

    // Create the status line from the status string
    StatusLine statusline = BasicLineParser.DEFAULT.parseStatusLine(current,
            new ParserCursor(0, current.length()));

    //if (HttpLog.LOGV) HttpLog.v("read: " + statusline);
    int statusCode = statusline.getStatusCode();

    // Parse header body
    CharArrayBuffer previous = null;
    int headerNumber = 0;
    while (true) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            // This must be he buffer used to parse the status
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // Parse the header name and value
        // Check for folded headers first
        // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
        // discussion on folded headers
        char first = current.charAt(0);
        if ((first == ' ' || first == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int start = 0;
            int length = current.length();
            while (start < length) {
                char ch = current.charAt(start);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                start++;
            }
            if (maxLineLength > 0 && previous.length() + 1 + current.length() - start > maxLineLength) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, start, current.length() - start);
        } else {
            if (previous != null) {
                headers.parseHeader(previous);
            }
            headerNumber++;
            previous = current;
            current = null;
        }
        if (maxHeaderCount > 0 && headerNumber >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }

    if (previous != null) {
        headers.parseHeader(previous);
    }

    if (statusCode >= 200) {
        this.metrics.incrementResponseCount();
    }
    return statusline;
}

From source file:org.apache.http.impl.client.AbstractAuthenticationHandler.java

protected Map<String, Header> parseChallenges(final Header[] headers) throws MalformedChallengeException {

    final Map<String, Header> map = new HashMap<String, Header>(headers.length);
    for (final Header header : headers) {
        final CharArrayBuffer buffer;
        int pos;/*from   w  w w .j av a2s . c o m*/
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            pos = ((FormattedHeader) header).getValuePos();
        } else {
            final String s = header.getValue();
            if (s == null) {
                throw new MalformedChallengeException("Header value is null");
            }
            buffer = new CharArrayBuffer(s.length());
            buffer.append(s);
            pos = 0;
        }
        while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int beginIndex = pos;
        while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int endIndex = pos;
        final String s = buffer.substring(beginIndex, endIndex);
        map.put(s.toLowerCase(Locale.US), header);
    }
    return map;
}

From source file:org.apache.http.impl.client.AuthenticationStrategyImpl.java

public Map<String, Header> getChallenges(final HttpHost authhost, final HttpResponse response,
        final HttpContext context) throws MalformedChallengeException {
    Args.notNull(response, "HTTP response");
    final Header[] headers = response.getHeaders(this.headerName);
    final Map<String, Header> map = new HashMap<String, Header>(headers.length);
    for (final Header header : headers) {
        final CharArrayBuffer buffer;
        int pos;/*from   w  ww  .  java  2s .com*/
        if (header instanceof FormattedHeader) {
            buffer = ((FormattedHeader) header).getBuffer();
            pos = ((FormattedHeader) header).getValuePos();
        } else {
            final String s = header.getValue();
            if (s == null) {
                throw new MalformedChallengeException("Header value is null");
            }
            buffer = new CharArrayBuffer(s.length());
            buffer.append(s);
            pos = 0;
        }
        while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int beginIndex = pos;
        while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
            pos++;
        }
        final int endIndex = pos;
        final String s = buffer.substring(beginIndex, endIndex);
        map.put(s.toLowerCase(Locale.US), header);
    }
    return map;
}