Index: src/main/java/org/apache/http/message/BasicLineParser.java =================================================================== --- src/main/java/org/apache/http/message/BasicLineParser.java (revision 899420) +++ src/main/java/org/apache/http/message/BasicLineParser.java (working copy) @@ -27,12 +27,12 @@ package org.apache.http.message; +import org.apache.http.Header; import org.apache.http.HttpVersion; -import org.apache.http.ProtocolVersion; import org.apache.http.ParseException; +import org.apache.http.ProtocolVersion; import org.apache.http.RequestLine; import org.apache.http.StatusLine; -import org.apache.http.Header; import org.apache.http.protocol.HTTP; import org.apache.http.util.CharArrayBuffer; @@ -96,7 +96,7 @@ public final static - ProtocolVersion parseProtocolVersion(String value, + ProtocolVersion parseProtocolVersion(final String value, LineParser parser) throws ParseException { @@ -108,16 +108,16 @@ if (parser == null) parser = BasicLineParser.DEFAULT; - CharArrayBuffer buffer = new CharArrayBuffer(value.length()); + final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); - ParserCursor cursor = new ParserCursor(0, value.length()); + final ParserCursor cursor = new ParserCursor(0, value.length()); return parser.parseProtocolVersion(buffer, cursor); } // non-javadoc, see interface LineParser public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, - final ParserCursor cursor) + final ParserCursor cursor) throws ParseException { if (buffer == null) { @@ -130,13 +130,13 @@ final String protoname = this.protocol.getProtocol(); final int protolength = protoname.length(); - int indexFrom = cursor.getPos(); - int indexTo = cursor.getUpperBound(); - + final int indexFrom = cursor.getPos(); + final int indexTo = cursor.getUpperBound(); + skipWhitespace(buffer, cursor); - int i = cursor.getPos(); - + final int i = cursor.getPos(); + // long enough for "HTTP/1.1"? if (i + protolength + 4 > indexTo) { throw new ParseException @@ -158,43 +158,46 @@ 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); + cursor.updatePos(i+protolength+1); - return createProtocolVersion(major, minor); + try + { + final int major = parseInt(buffer, cursor, indexTo, '.'); + //TODO could do a cursor.inc() method + cursor.updatePos(cursor.getPos() + 1); + final int minor = parseInt(buffer, cursor, indexTo, ' '); + return createProtocolVersion(major, minor); + } + catch (final ParseException e) + { + throw new ParseException("Invalid protocol version number: " + buffer.substring(indexFrom, indexTo)); + } } // parseProtocolVersion + //TODO could be moved into a utils class + private static int parseInt(final CharArrayBuffer buffer, final ParserCursor cursor, final int indexTo, final char charTo) + { + int value = 0; + int i = cursor.getPos(); + for ( ; i < indexTo; i++) + { + final char ch = buffer.charAt(i); + if (Character.isDigit(ch)) + { + value = 10 * value + Character.digit(ch, 10); + } + else + { + if (ch == charTo) + break; + else + throw new ParseException("Unexpected char " + ch); + } + } + cursor.updatePos(i); + return value; + } /** * Creates a protocol version. @@ -205,7 +208,7 @@ * * @return the protocol version */ - protected ProtocolVersion createProtocolVersion(int major, int minor) { + protected ProtocolVersion createProtocolVersion(final int major, final int minor) { return protocol.forVersion(major, minor); } @@ -273,9 +276,9 @@ if (parser == null) parser = BasicLineParser.DEFAULT; - CharArrayBuffer buffer = new CharArrayBuffer(value.length()); + final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); - ParserCursor cursor = new ParserCursor(0, value.length()); + final ParserCursor cursor = new ParserCursor(0, value.length()); return parser.parseRequestLine(buffer, cursor); } @@ -300,19 +303,19 @@ throw new IllegalArgumentException("Parser cursor may not be null"); } - int indexFrom = cursor.getPos(); - int indexTo = cursor.getUpperBound(); - + final int indexFrom = cursor.getPos(); + final int indexTo = cursor.getUpperBound(); + try { skipWhitespace(buffer, cursor); int i = cursor.getPos(); - + int blank = buffer.indexOf(' ', i, indexTo); if (blank < 0) { - throw new ParseException("Invalid request line: " + + throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo)); } - String method = buffer.substringTrimmed(i, blank); + final String method = buffer.substringTrimmed(i, blank); cursor.updatePos(blank); skipWhitespace(buffer, cursor); @@ -320,24 +323,24 @@ blank = buffer.indexOf(' ', i, indexTo); if (blank < 0) { - throw new ParseException("Invalid request line: " + + throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo)); } - String uri = buffer.substringTrimmed(i, blank); + final String uri = buffer.substringTrimmed(i, blank); cursor.updatePos(blank); - ProtocolVersion ver = parseProtocolVersion(buffer, cursor); - + final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); + skipWhitespace(buffer, cursor); if (!cursor.atEnd()) { - throw new ParseException("Invalid request line: " + + throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo)); } - + return createRequestLine(method, uri, ver); - } catch (IndexOutOfBoundsException e) { - throw new ParseException("Invalid request line: " + - buffer.substring(indexFrom, indexTo)); + } catch (final IndexOutOfBoundsException e) { + throw new ParseException("Invalid request line: " + + buffer.substring(indexFrom, indexTo)); } } // parseRequestLine @@ -373,16 +376,16 @@ if (parser == null) parser = BasicLineParser.DEFAULT; - CharArrayBuffer buffer = new CharArrayBuffer(value.length()); + final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); - ParserCursor cursor = new ParserCursor(0, value.length()); + final ParserCursor cursor = new ParserCursor(0, value.length()); return parser.parseStatusLine(buffer, cursor); } // non-javadoc, see interface LineParser public StatusLine parseStatusLine(final CharArrayBuffer buffer, - final ParserCursor cursor) + final ParserCursor cursor) throws ParseException { if (buffer == null) { @@ -392,39 +395,30 @@ throw new IllegalArgumentException("Parser cursor may not be null"); } - int indexFrom = cursor.getPos(); - int indexTo = cursor.getUpperBound(); - + final int indexFrom = cursor.getPos(); + final int indexTo = cursor.getUpperBound(); + try { // handle the HTTP-Version - ProtocolVersion ver = parseProtocolVersion(buffer, cursor); + final 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; - String s = buffer.substringTrimmed(i, blank); - for (int j = 0; j < s.length(); j++) { - if (!Character.isDigit(s.charAt(j))) { - throw new ParseException( - "Status line contains invalid status code: " - + buffer.substring(indexFrom, indexTo)); - } + + final int statusCode; + try + { + statusCode = parseInt(buffer, cursor, indexTo, ' '); } - try { - statusCode = Integer.parseInt(s); - } catch (NumberFormatException e) { - throw new ParseException( - "Status line contains invalid status code: " - + buffer.substring(indexFrom, indexTo)); + catch (final ParseException e) + { + throw new ParseException( + "Status line contains invalid status code: " + + buffer.substring(indexFrom, indexTo)); } + //handle the Reason-Phrase - i = blank; + final int i = cursor.getPos(); String reasonPhrase = null; if (i < indexTo) { reasonPhrase = buffer.substringTrimmed(i, indexTo); @@ -433,9 +427,9 @@ } return createStatusLine(ver, statusCode, reasonPhrase); - } catch (IndexOutOfBoundsException e) { - throw new ParseException("Invalid status line: " + - buffer.substring(indexFrom, indexTo)); + } catch (final IndexOutOfBoundsException e) { + throw new ParseException("Invalid status line: " + + buffer.substring(indexFrom, indexTo)); } } // parseStatusLine @@ -451,7 +445,7 @@ * @return a new status line with the given data */ protected StatusLine createStatusLine(final ProtocolVersion ver, - final int status, + final int status, final String reason) { return new BasicStatusLine(ver, status, reason); } @@ -459,7 +453,7 @@ public final static - Header parseHeader(final String value, + Header parseHeader(final String value, LineParser parser) throws ParseException { @@ -471,14 +465,14 @@ if (parser == null) parser = BasicLineParser.DEFAULT; - CharArrayBuffer buffer = new CharArrayBuffer(value.length()); + final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); return parser.parseHeader(buffer); } // non-javadoc, see interface LineParser - public Header parseHeader(CharArrayBuffer buffer) + public Header parseHeader(final CharArrayBuffer buffer) throws ParseException { // the actual parser code is in the constructor of BufferedHeader @@ -491,7 +485,7 @@ */ protected void skipWhitespace(final CharArrayBuffer buffer, final ParserCursor cursor) { int pos = cursor.getPos(); - int indexTo = cursor.getUpperBound(); + final int indexTo = cursor.getUpperBound(); while ((pos < indexTo) && HTTP.isWhitespace(buffer.charAt(pos))) { pos++;