Search in sources :

Example 1 with ParserCursor

use of org.apache.http.message.ParserCursor in project android_frameworks_base by ParanoidAndroid.

the class AndroidHttpClientConnection method parseResponseHeader.

/**
     * 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
     * @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;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) StatusLine(org.apache.http.StatusLine) ParserCursor(org.apache.http.message.ParserCursor) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) IOException(java.io.IOException)

Example 2 with ParserCursor

use of org.apache.http.message.ParserCursor in project robovm by robovm.

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 3 with ParserCursor

use of org.apache.http.message.ParserCursor in project robovm by robovm.

the class HttpRequestParser method parseHead.

protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException, ParseException {
    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);
    if (i == -1) {
        throw new ConnectionClosedException("Client closed connection");
    }
    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    RequestLine requestline = this.lineParser.parseRequestLine(this.lineBuf, cursor);
    return this.requestFactory.newHttpRequest(requestline);
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) RequestLine(org.apache.http.RequestLine) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 4 with ParserCursor

use of org.apache.http.message.ParserCursor in project robovm by robovm.

the class HttpResponseParser method parseHead.

protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException, ParseException {
    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);
    if (i == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }
    //create the status line from the status string
    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ParserCursor(org.apache.http.message.ParserCursor) StatusLine(org.apache.http.StatusLine)

Example 5 with ParserCursor

use of org.apache.http.message.ParserCursor in project XobotOS by xamarin.

the class Headers method parseHeader.

public void parseHeader(CharArrayBuffer buffer) {
    int pos = CharArrayBuffers.setLowercaseIndexOf(buffer, ':');
    if (pos == -1) {
        return;
    }
    String name = buffer.substringTrimmed(0, pos);
    if (name.length() == 0) {
        return;
    }
    pos++;
    String val = buffer.substringTrimmed(pos, buffer.length());
    if (HttpLog.LOGV) {
        HttpLog.v("hdr " + buffer.length() + " " + buffer);
    }
    switch(name.hashCode()) {
        case HASH_TRANSFER_ENCODING:
            if (name.equals(TRANSFER_ENCODING)) {
                mHeaders[IDX_TRANSFER_ENCODING] = val;
                HeaderElement[] encodings = BasicHeaderValueParser.DEFAULT.parseElements(buffer, new ParserCursor(pos, buffer.length()));
                // The chunked encoding must be the last one applied RFC2616,
                // 14.41
                int len = encodings.length;
                if (HTTP.IDENTITY_CODING.equalsIgnoreCase(val)) {
                    transferEncoding = ContentLengthStrategy.IDENTITY;
                } else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(encodings[len - 1].getName()))) {
                    transferEncoding = ContentLengthStrategy.CHUNKED;
                } else {
                    transferEncoding = ContentLengthStrategy.IDENTITY;
                }
            }
            break;
        case HASH_CONTENT_LEN:
            if (name.equals(CONTENT_LEN)) {
                mHeaders[IDX_CONTENT_LEN] = val;
                try {
                    contentLength = Long.parseLong(val);
                } catch (NumberFormatException e) {
                    if (false) {
                        Log.v(LOGTAG, "Headers.headers(): error parsing" + " content length: " + buffer.toString());
                    }
                }
            }
            break;
        case HASH_CONTENT_TYPE:
            if (name.equals(CONTENT_TYPE)) {
                mHeaders[IDX_CONTENT_TYPE] = val;
            }
            break;
        case HASH_CONTENT_ENCODING:
            if (name.equals(CONTENT_ENCODING)) {
                mHeaders[IDX_CONTENT_ENCODING] = val;
            }
            break;
        case HASH_CONN_DIRECTIVE:
            if (name.equals(CONN_DIRECTIVE)) {
                mHeaders[IDX_CONN_DIRECTIVE] = val;
                setConnectionType(buffer, pos);
            }
            break;
        case HASH_LOCATION:
            if (name.equals(LOCATION)) {
                mHeaders[IDX_LOCATION] = val;
            }
            break;
        case HASH_PROXY_CONNECTION:
            if (name.equals(PROXY_CONNECTION)) {
                mHeaders[IDX_PROXY_CONNECTION] = val;
                setConnectionType(buffer, pos);
            }
            break;
        case HASH_WWW_AUTHENTICATE:
            if (name.equals(WWW_AUTHENTICATE)) {
                mHeaders[IDX_WWW_AUTHENTICATE] = val;
            }
            break;
        case HASH_PROXY_AUTHENTICATE:
            if (name.equals(PROXY_AUTHENTICATE)) {
                mHeaders[IDX_PROXY_AUTHENTICATE] = val;
            }
            break;
        case HASH_CONTENT_DISPOSITION:
            if (name.equals(CONTENT_DISPOSITION)) {
                mHeaders[IDX_CONTENT_DISPOSITION] = val;
            }
            break;
        case HASH_ACCEPT_RANGES:
            if (name.equals(ACCEPT_RANGES)) {
                mHeaders[IDX_ACCEPT_RANGES] = val;
            }
            break;
        case HASH_EXPIRES:
            if (name.equals(EXPIRES)) {
                mHeaders[IDX_EXPIRES] = val;
            }
            break;
        case HASH_CACHE_CONTROL:
            if (name.equals(CACHE_CONTROL)) {
                // This should be ok, according to RFC 2616 chapter 4.2
                if (mHeaders[IDX_CACHE_CONTROL] != null && mHeaders[IDX_CACHE_CONTROL].length() > 0) {
                    mHeaders[IDX_CACHE_CONTROL] += (',' + val);
                } else {
                    mHeaders[IDX_CACHE_CONTROL] = val;
                }
            }
            break;
        case HASH_LAST_MODIFIED:
            if (name.equals(LAST_MODIFIED)) {
                mHeaders[IDX_LAST_MODIFIED] = val;
            }
            break;
        case HASH_ETAG:
            if (name.equals(ETAG)) {
                mHeaders[IDX_ETAG] = val;
            }
            break;
        case HASH_SET_COOKIE:
            if (name.equals(SET_COOKIE)) {
                mHeaders[IDX_SET_COOKIE] = val;
                cookies.add(val);
            }
            break;
        case HASH_PRAGMA:
            if (name.equals(PRAGMA)) {
                mHeaders[IDX_PRAGMA] = val;
            }
            break;
        case HASH_REFRESH:
            if (name.equals(REFRESH)) {
                mHeaders[IDX_REFRESH] = val;
            }
            break;
        case HASH_X_PERMITTED_CROSS_DOMAIN_POLICIES:
            if (name.equals(X_PERMITTED_CROSS_DOMAIN_POLICIES)) {
                mHeaders[IDX_X_PERMITTED_CROSS_DOMAIN_POLICIES] = val;
            }
            break;
        default:
            mExtraHeaderNames.add(name);
            mExtraHeaderValues.add(val);
    }
}
Also used : ParserCursor(org.apache.http.message.ParserCursor) HeaderElement(org.apache.http.HeaderElement)

Aggregations

ParserCursor (org.apache.http.message.ParserCursor)26 CharArrayBuffer (org.apache.http.util.CharArrayBuffer)11 HeaderElement (org.apache.http.HeaderElement)9 NoHttpResponseException (org.apache.http.NoHttpResponseException)9 StatusLine (org.apache.http.StatusLine)9 FormattedHeader (org.apache.http.FormattedHeader)6 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)6 BasicHeaderValueParser (org.apache.http.message.BasicHeaderValueParser)5 IOException (java.io.IOException)3 ConnectionClosedException (org.apache.http.ConnectionClosedException)3 ProtocolException (org.apache.http.ProtocolException)3 RequestLine (org.apache.http.RequestLine)3 MalformedChallengeException (org.apache.http.auth.MalformedChallengeException)3 HeaderValueParser (org.apache.http.message.HeaderValueParser)3 NameValuePair (org.apache.http.NameValuePair)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 ArrayList (java.util.ArrayList)1