@Override public StatusLine parseStatusLine(final CharArrayBuffer buffer) throws ParseException { Args.notNull(buffer, "Char array buffer"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); this.tokenParser.skipWhiteSpace(buffer, cursor); final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); this.tokenParser.skipWhiteSpace(buffer, cursor); final String s = this.tokenParser.parseToken(buffer, cursor, BLANKS); for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) { throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } } final int statusCode; try { statusCode = Integer.parseInt(s); } catch (final NumberFormatException e) { throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } final String text = buffer.substringTrimmed(cursor.getPos(), cursor.getUpperBound()); return new StatusLine(ver, statusCode, text); }
@Override public StatusLine parseStatusLine(final CharArrayBuffer buffer) throws ParseException { Args.notNull(buffer, "Char array buffer"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); this.tokenParser.skipWhiteSpace(buffer, cursor); final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); this.tokenParser.skipWhiteSpace(buffer, cursor); final String s = this.tokenParser.parseToken(buffer, cursor, BLANKS); for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) { throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } } final int statusCode; try { statusCode = Integer.parseInt(s); } catch (final NumberFormatException e) { throw new ParseException("Status line contains invalid status code", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } final String text = buffer.substringTrimmed(cursor.getPos(), cursor.getUpperBound()); return new StatusLine(ver, statusCode, text); }
ParserCursor cursor = new ParserCursor(0, buffer.length()); try { this.parser.parseProtocolVersion(buffer, cursor); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { cursor = new ParserCursor(0, buffer.length()); try { this.parser.parseProtocolVersion(buffer, cursor); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { cursor = new ParserCursor(0, buffer.length()); try { this.parser.parseProtocolVersion(buffer, cursor); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { cursor = new ParserCursor(0, buffer.length()); try { this.parser.parseProtocolVersion(buffer, cursor); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { cursor = new ParserCursor(0, buffer.length()); try { this.parser.parseProtocolVersion(buffer, cursor); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) {
buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); this.tokenParser.skipWhiteSpace(buffer, cursor); if (!cursor.atEnd()) {
buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); this.tokenParser.skipWhiteSpace(buffer, cursor); if (!cursor.atEnd()) {
@Test public void testHttpVersionParsing() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(16); buffer.append("HTTP/1.1"); ParserCursor cursor = new ParserCursor(0, buffer.length()); HttpVersion version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor); Assert.assertEquals("HTTP protocol name", "HTTP", version.getProtocol()); Assert.assertEquals("HTTP major version number", 1, version.getMajor()); Assert.assertEquals("HTTP minor version number", 1, version.getMinor()); Assert.assertEquals("HTTP version number", "HTTP/1.1", version.toString()); Assert.assertEquals(buffer.length(), cursor.getPos()); Assert.assertTrue(cursor.atEnd()); buffer.clear(); buffer.append("HTTP/1.123 123"); cursor = new ParserCursor(0, buffer.length()); version = (HttpVersion) parser.parseProtocolVersion(buffer, cursor); Assert.assertEquals("HTTP protocol name", "HTTP", version.getProtocol()); Assert.assertEquals("HTTP major version number", 1, version.getMajor()); Assert.assertEquals("HTTP minor version number", 123, version.getMinor()); Assert.assertEquals("HTTP version number", "HTTP/1.123", version.toString()); Assert.assertEquals(' ', buffer.charAt(cursor.getPos())); Assert.assertEquals(buffer.length() - 4, cursor.getPos()); Assert.assertFalse(cursor.atEnd()); }