Search in sources :

Example 11 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class AbstractMessageParser method parse.

public HttpMessage parse() throws IOException, HttpException {
    HttpMessage message = null;
    try {
        message = parseHead(this.sessionBuffer);
    } catch (ParseException px) {
        throw new ProtocolException(px.getMessage(), px);
    }
    Header[] headers = AbstractMessageParser.parseHeaders(this.sessionBuffer, this.maxHeaderCount, this.maxLineLen, this.lineParser);
    message.setHeaders(headers);
    return message;
}
Also used : ProtocolException(org.apache.http.ProtocolException) Header(org.apache.http.Header) ParseException(org.apache.http.ParseException) HttpMessage(org.apache.http.HttpMessage)

Example 12 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class AbstractMessageParser method parseHeaders.

/**
     * Parses HTTP headers from the data receiver stream according to the generic 
     * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3.
     *  
     * @param inbuffer Session input buffer
     * @param maxHeaderCount maximum number of headers allowed. If the number
     *  of headers received from the data stream exceeds maxCount value, an
     *  IOException will be thrown. Setting this parameter to a negative value
     *  or zero  will disable the check.
     * @param maxLineLen maximum number of characters for a header line,
     *                   including the continuation lines
     * @return array of HTTP headers
     * 
     * @throws HttpException
     * @throws IOException
     */
public static Header[] parseHeaders(final SessionInputBuffer inbuffer, int maxHeaderCount, int maxLineLen, LineParser parser) throws HttpException, IOException {
    if (inbuffer == null) {
        throw new IllegalArgumentException("Session input buffer may not be null");
    }
    if (parser == null)
        parser = BasicLineParser.DEFAULT;
    ArrayList headerLines = new ArrayList();
    CharArrayBuffer current = null;
    CharArrayBuffer previous = null;
    for (; ; ) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // discussion on folded headers
        if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int i = 0;
            while (i < current.length()) {
                char ch = current.charAt(i);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                i++;
            }
            if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, i, current.length() - i);
        } else {
            headerLines.add(current);
            previous = current;
            current = null;
        }
        if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }
    Header[] headers = new Header[headerLines.size()];
    for (int i = 0; i < headerLines.size(); i++) {
        CharArrayBuffer buffer = (CharArrayBuffer) headerLines.get(i);
        try {
            headers[i] = parser.parseHeader(buffer);
        } catch (ParseException ex) {
            throw new ProtocolException(ex.getMessage());
        }
    }
    return headers;
}
Also used : ProtocolException(org.apache.http.ProtocolException) Header(org.apache.http.Header) ArrayList(java.util.ArrayList) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) IOException(java.io.IOException) ParseException(org.apache.http.ParseException)

Example 13 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class BasicLineParser method parseProtocolVersion.

// non-javadoc, see interface LineParser
public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    final String protoname = this.protocol.getProtocol();
    final int protolength = protoname.length();
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    skipWhitespace(buffer, cursor);
    int i = cursor.getPos();
    // long enough for "HTTP/1.1"?
    if (i + protolength + 4 > indexTo) {
        throw new ParseException("Not a valid protocol version: " + buffer.substring(indexFrom, indexTo));
    }
    // check the protocol name and slash
    boolean ok = true;
    for (int j = 0; ok && (j < protolength); j++) {
        ok = (buffer.charAt(i + j) == protoname.charAt(j));
    }
    if (ok) {
        ok = (buffer.charAt(i + protolength) == '/');
    }
    if (!ok) {
        throw new ParseException("Not a valid protocol version: " + buffer.substring(indexFrom, indexTo));
    }
    i += protolength + 1;
    int period = buffer.indexOf('.', i, indexTo);
    if (period == -1) {
        throw new ParseException("Invalid protocol version number: " + buffer.substring(indexFrom, indexTo));
    }
    int major;
    try {
        major = Integer.parseInt(buffer.substringTrimmed(i, period));
    } catch (NumberFormatException e) {
        throw new ParseException("Invalid protocol major version number: " + buffer.substring(indexFrom, indexTo));
    }
    i = period + 1;
    int blank = buffer.indexOf(' ', i, indexTo);
    if (blank == -1) {
        blank = indexTo;
    }
    int minor;
    try {
        minor = Integer.parseInt(buffer.substringTrimmed(i, blank));
    } catch (NumberFormatException e) {
        throw new ParseException("Invalid protocol minor version number: " + buffer.substring(indexFrom, indexTo));
    }
    cursor.updatePos(blank);
    return createProtocolVersion(major, minor);
}
Also used : ParseException(org.apache.http.ParseException)

Example 14 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class BasicLineParser method parseStatusLine.

// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
    if (buffer == null) {
        throw new IllegalArgumentException("Char array buffer may not be null");
    }
    if (cursor == null) {
        throw new IllegalArgumentException("Parser cursor may not be null");
    }
    int indexFrom = cursor.getPos();
    int indexTo = cursor.getUpperBound();
    try {
        // handle the HTTP-Version
        ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
        // handle the Status-Code
        skipWhitespace(buffer, cursor);
        int i = cursor.getPos();
        int blank = buffer.indexOf(' ', i, indexTo);
        if (blank < 0) {
            blank = indexTo;
        }
        int statusCode = 0;
        try {
            statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
        } catch (NumberFormatException e) {
            throw new ParseException("Unable to parse status code from status line: " + buffer.substring(indexFrom, indexTo));
        }
        //handle the Reason-Phrase
        i = blank;
        String reasonPhrase = null;
        if (i < indexTo) {
            reasonPhrase = buffer.substringTrimmed(i, indexTo);
        } else {
            reasonPhrase = "";
        }
        return createStatusLine(ver, statusCode, reasonPhrase);
    } catch (IndexOutOfBoundsException e) {
        throw new ParseException("Invalid status line: " + buffer.substring(indexFrom, indexTo));
    }
}
Also used : ParseException(org.apache.http.ParseException) ProtocolVersion(org.apache.http.ProtocolVersion)

Example 15 with ParseException

use of org.apache.http.ParseException in project platform_external_apache-http by android.

the class BasicTokenIterator method findTokenStart.

/**
     * Determines the starting position of the next token.
     * This method will iterate over headers if necessary.
     *
     * @param from      the position in the current header at which to
     *                  start the search
     *
     * @return  the position of the token start in the current header,
     *          negative if no token start could be found
     */
protected int findTokenStart(int from) {
    if (from < 0) {
        throw new IllegalArgumentException("Search position must not be negative: " + from);
    }
    boolean found = false;
    while (!found && (this.currentHeader != null)) {
        final int to = this.currentHeader.length();
        while (!found && (from < to)) {
            final char ch = this.currentHeader.charAt(from);
            if (isTokenSeparator(ch) || isWhitespace(ch)) {
                // whitspace and token separators are skipped
                from++;
            } else if (isTokenChar(this.currentHeader.charAt(from))) {
                // found the start of a token
                found = true;
            } else {
                throw new ParseException("Invalid character before token (pos " + from + "): " + this.currentHeader);
            }
        }
        if (!found) {
            if (this.headerIt.hasNext()) {
                this.currentHeader = this.headerIt.nextHeader().getValue();
                from = 0;
            } else {
                this.currentHeader = null;
            }
        }
    }
    return found ? from : -1;
}
Also used : ParseException(org.apache.http.ParseException)

Aggregations

ParseException (org.apache.http.ParseException)38 Header (org.apache.http.Header)14 ProtocolVersion (org.apache.http.ProtocolVersion)12 IOException (java.io.IOException)10 ProtocolException (org.apache.http.ProtocolException)9 HttpException (org.apache.http.HttpException)6 HttpParams (org.apache.http.params.HttpParams)6 Test (org.junit.Test)5 HttpEntity (org.apache.http.HttpEntity)4 Socket (java.net.Socket)3 UnknownHostException (java.net.UnknownHostException)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 SSLSocket (javax.net.ssl.SSLSocket)3 HeaderElement (org.apache.http.HeaderElement)3 HeaderIterator (org.apache.http.HeaderIterator)3 HttpConnection (org.apache.http.HttpConnection)3 HttpMessage (org.apache.http.HttpMessage)3 HttpResponse (org.apache.http.HttpResponse)3