@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); }
buffer.append(""); try { this.parser.parseHeader(buffer); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { buffer.append("blah"); try { this.parser.parseHeader(buffer); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { buffer.append(":"); try { this.parser.parseHeader(buffer); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { buffer.append(" :"); try { this.parser.parseHeader(buffer); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) { buffer.append(": blah"); try { this.parser.parseHeader(buffer); Assert.fail("ParseException should have been thrown"); } catch (final ParseException e) {
buf.append(" "); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append(" GET"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("GET /stuff"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("GET/stuff HTTP/1.1"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("GET /stuff HTTP/1.1 Oooooooooooppsie"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) {
buf.append("xxx 200 OK"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("HTTP/1.1 xxx OK"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("HTTP/1.1 "); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("HTTP/1.1"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { buf.append("HTTP/1.1 -200 OK"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) {
StatusLine statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals("HTTP/1.1 200 OK", statusLine.toString()); Assert.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(404, statusLine.getStatusCode()); Assert.assertEquals("Not Found", statusLine.getReasonPhrase()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals("Non Trouve", statusLine.getReasonPhrase()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals("Not Found", statusLine.getReasonPhrase()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("", statusLine.getReasonPhrase()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("", statusLine.getReasonPhrase()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("OK", statusLine.getReasonPhrase()); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("OK", statusLine.getReasonPhrase());
@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); }
@Test public void testHeaderParse() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); //typical request line buf.clear(); buf.append("header: blah"); Header header = this.parser.parseHeader(buf); Assert.assertEquals("header", header.getName()); Assert.assertEquals("blah", header.getValue()); //Lots of blanks buf.clear(); buf.append(" header: blah "); header = this.parser.parseHeader(buf); Assert.assertEquals("header", header.getName()); Assert.assertEquals("blah", header.getValue()); }
@Test public void testRLParse() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); //typical request line buf.clear(); buf.append("GET /stuff HTTP/1.1"); RequestLine requestline = this.parser.parseRequestLine(buf); Assert.assertEquals("GET /stuff HTTP/1.1", requestline.toString()); Assert.assertEquals("GET", requestline.getMethod()); Assert.assertEquals("/stuff", requestline.getUri()); Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion()); //Lots of blanks buf.clear(); buf.append(" GET /stuff HTTP/1.1 "); requestline = this.parser.parseRequestLine(buf); Assert.assertEquals("GET /stuff HTTP/1.1", requestline.toString()); Assert.assertEquals("GET", requestline.getMethod()); Assert.assertEquals("/stuff", requestline.getUri()); Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion()); //this is not strictly valid, but is lenient buf.clear(); buf.append("\rGET /stuff HTTP/1.1"); requestline = this.parser.parseRequestLine(buf); Assert.assertEquals("GET", requestline.getMethod()); Assert.assertEquals("/stuff", requestline.getUri()); Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion()); }
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()); }