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;
}
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;
}
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);
}
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));
}
}
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;
}
Aggregations