BasicLineParser implements LineParser { @Override public StatusLine parseStatusLine(final CharArrayBuffer buffer) throws ParseException { Args.notNull(buffer, "Char array buffer"); final ParserCursor cursor = new ParserCursor(0, buffer.length()); this.tokenizer.skipWhiteSpace(buffer, cursor); final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); this.tokenizer.skipWhiteSpace(buffer, cursor); final String s = this.tokenizer.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); } BasicLineParser(final ProtocolVersion proto);  BasicLineParser(); @Override RequestLine parseRequestLine(final CharArrayBuffer buffer); @Override StatusLine parseStatusLine(final CharArrayBuffer buffer); @Override Header parseHeader(final CharArrayBuffer buffer); final static BasicLineParser INSTANCE; }
@Test public void testSLParse() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); buf.clear(); buf.append("HTTP/1.1 200 OK"); StatusLine statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals("HTTP/1.1 200 OK", statusLine.toString()); Assert.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion()); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("OK", statusLine.getReasonPhrase()); buf.clear(); buf.append("HTTP/1.1 404 Not Found"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(404, statusLine.getStatusCode()); Assert.assertEquals("Not Found", statusLine.getReasonPhrase()); buf.clear(); buf.append("HTTP/1.1 404 Non Trouve"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals("Non Trouve", statusLine.getReasonPhrase()); buf.clear(); buf.append("HTTP/1.1 404 Not Found\r\n"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals("Not Found", statusLine.getReasonPhrase()); buf.clear(); buf.append("HTTP/1.1 200 "); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("", statusLine.getReasonPhrase()); buf.clear(); buf.append("HTTP/1.1 200"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("", statusLine.getReasonPhrase()); buf.clear(); buf.append("HTTP/1.1 200 OK"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("OK", statusLine.getReasonPhrase()); buf.clear(); buf.append("\nHTTP/1.1 200 OK"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("OK", statusLine.getReasonPhrase()); Assert.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion()); buf.clear(); buf.append(" HTTP/1.1 200 OK"); statusLine = this.parser.parseStatusLine(buf); Assert.assertEquals(200, statusLine.getStatusCode()); Assert.assertEquals("OK", statusLine.getReasonPhrase()); Assert.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion()); }
@Test public void testSLParseFailure() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); buf.clear(); buf.append("xxx 200 OK"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("HTTP/1.1 xxx OK"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("HTTP/1.1 "); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("HTTP/1.1"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("HTTP/1.1 -200 OK"); try { this.parser.parseStatusLine(buf); Assert.fail(); } catch (final ParseException e) { } }