BasicLineParser implements LineParser { @Override public RequestLine parseRequestLine(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 String method = this.tokenizer.parseToken(buffer, cursor, BLANKS); if (TextUtils.isEmpty(method)) { throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } this.tokenizer.skipWhiteSpace(buffer, cursor); final String uri = this.tokenizer.parseToken(buffer, cursor, BLANKS); if (TextUtils.isEmpty(uri)) { throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } final ProtocolVersion ver = parseProtocolVersion(buffer, cursor); this.tokenizer.skipWhiteSpace(buffer, cursor); if (!cursor.atEnd()) { throw new ParseException("Invalid request line", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } return new RequestLine(method, uri, ver); } 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 testRLParse() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); 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(Method.GET.name(), requestline.getMethod()); Assert.assertEquals("/stuff", requestline.getUri()); Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion()); 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(Method.GET.name(), requestline.getMethod()); Assert.assertEquals("/stuff", requestline.getUri()); Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion()); buf.clear(); buf.append("\rGET /stuff HTTP/1.1"); requestline = this.parser.parseRequestLine(buf); Assert.assertEquals(Method.GET.name(), requestline.getMethod()); Assert.assertEquals("/stuff", requestline.getUri()); Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion()); }
@Test public void testRLParseFailure() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); buf.clear(); buf.append(" "); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append(" GET"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("GET /stuff"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("GET/stuff HTTP/1.1"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { } buf.clear(); buf.append("GET /stuff HTTP/1.1 Oooooooooooppsie"); try { this.parser.parseRequestLine(buf); Assert.fail(); } catch (final ParseException e) { } }