private void parseHeader() throws IOException { final CharArrayBuffer current = this.lineBuf; final int count = this.trailerBufs.size(); if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) { // Handle folded header line final CharArrayBuffer previous = this.trailerBufs.get(count - 1); int i = 0; while (i < current.length()) { final char ch = current.charAt(i); if (ch != ' ' && ch != '\t') { break; } i++; } final int maxLineLen = this.h1Config.getMaxLineLength(); if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) { throw new MessageConstraintException("Maximum line length limit exceeded"); } previous.append(' '); previous.append(current, i, current.length() - i); } else { this.trailerBufs.add(current); this.lineBuf = null; } }
private void parseHeader() throws IOException { final CharArrayBuffer current = this.lineBuf; final int count = this.trailerBufs.size(); if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) { // Handle folded header line final CharArrayBuffer previous = this.trailerBufs.get(count - 1); int i = 0; while (i < current.length()) { final char ch = current.charAt(i); if (ch != ' ' && ch != '\t') { break; } i++; } final int maxLineLen = this.h1Config.getMaxLineLength(); if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) { throw new MessageConstraintException("Maximum line length limit exceeded"); } previous.append(' '); previous.append(current, i, current.length() - i); } else { this.trailerBufs.add(current); this.lineBuf = null; } }
private void parseHeader() throws IOException { final CharArrayBuffer current = this.lineBuf; final int count = this.headerBufs.size(); if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) { // Handle folded header line final CharArrayBuffer previous = this.headerBufs.get(count - 1); int i = 0; while (i < current.length()) { final char ch = current.charAt(i); if (ch != ' ' && ch != '\t') { break; } i++; } final int maxLineLen = this.messageConstraints.getMaxLineLength(); if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) { throw new MessageConstraintException("Maximum line length limit exceeded"); } previous.append(' '); previous.append(current, i, current.length() - i); } else { this.headerBufs.add(current); this.lineBuf = null; } }
private void parseHeader() throws IOException { final CharArrayBuffer current = this.lineBuf; final int count = this.headerBufs.size(); if ((this.lineBuf.charAt(0) == ' ' || this.lineBuf.charAt(0) == '\t') && count > 0) { // Handle folded header line final CharArrayBuffer previous = this.headerBufs.get(count - 1); int i = 0; while (i < current.length()) { final char ch = current.charAt(i); if (ch != ' ' && ch != '\t') { break; } i++; } final int maxLineLen = this.messageConstraints.getMaxLineLength(); if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) { throw new MessageConstraintException("Maximum line length limit exceeded"); } previous.append(' '); previous.append(current, i, current.length() - i); } else { this.headerBufs.add(current); this.lineBuf = null; } }
BufferedHeader(final CharArrayBuffer buffer, final boolean strict) throws ParseException { super(); Args.notNull(buffer, "Char array buffer"); final int colon = buffer.indexOf(':'); if (colon <= 0) { throw new ParseException("Invalid header", buffer, 0, buffer.length()); } if (strict && TokenParser.isWhitespace(buffer.charAt(colon - 1))) { throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1); } final String s = buffer.substringTrimmed(0, colon); if (s.isEmpty()) { throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon); } this.buffer = buffer; this.name = s; this.valuePos = colon + 1; }
BufferedHeader(final CharArrayBuffer buffer, final boolean strict) throws ParseException { super(); Args.notNull(buffer, "Char array buffer"); final int colon = buffer.indexOf(':'); if (colon <= 0) { throw new ParseException("Invalid header", buffer, 0, buffer.length()); } if (strict && TokenParser.isWhitespace(buffer.charAt(colon - 1))) { throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon - 1); } final String s = buffer.substringTrimmed(0, colon); if (s.isEmpty()) { throw new ParseException("Invalid header", buffer, 0, buffer.length(), colon); } this.buffer = buffer; this.name = s; this.valuePos = colon + 1; }
return Collections.emptyList(); final int valueDelim = buffer.charAt(cursor.getPos()); cursor.updatePos(cursor.getPos() + 1); if (valueDelim != '=') { String paramValue = null; if (!cursor.atEnd()) { final int paramDelim = buffer.charAt(cursor.getPos()); cursor.updatePos(cursor.getPos() + 1); if (paramDelim == EQUAL_CHAR) {
@Override public Header parseHeader(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 String name = this.tokenParser.parseToken(buffer, cursor, COLON); if (cursor.getPos() == cursor.getLowerBound() || cursor.getPos() == cursor.getUpperBound() || buffer.charAt(cursor.getPos()) != ':' || TextUtils.isEmpty(name) || TokenParser.isWhitespace(buffer.charAt(cursor.getPos() - 1))) { throw new ParseException("Invalid header", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } final String value = buffer.substringTrimmed(cursor.getPos() + 1, cursor.getUpperBound()); return new BasicHeader(name, value); }
@Override public Header parseHeader(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 String name = this.tokenParser.parseToken(buffer, cursor, COLON); if (cursor.getPos() == cursor.getLowerBound() || cursor.getPos() == cursor.getUpperBound() || buffer.charAt(cursor.getPos()) != ':' || TextUtils.isEmpty(name) || TokenParser.isWhitespace(buffer.charAt(cursor.getPos() - 1))) { throw new ParseException("Invalid header", buffer, cursor.getLowerBound(), cursor.getUpperBound(), cursor.getPos()); } final String value = buffer.substringTrimmed(cursor.getPos() + 1, cursor.getUpperBound()); return new BasicHeader(name, value); }
private NameValuePair parseParameter(final CharArrayBuffer buf, final ParserCursor cursor) { final String name = parseToken(buf, cursor, EQUAL_OR_COMMA_OR_PLUS); if (cursor.atEnd()) { return new BasicNameValuePair(name, null); } final int delim = buf.charAt(cursor.getPos()); cursor.updatePos(cursor.getPos() + 1); if (delim == ',') { return new BasicNameValuePair(name, null); } final String value = parseValue(buf, cursor, COMMA_OR_PLUS); if (!cursor.atEnd()) { cursor.updatePos(cursor.getPos() + 1); } return new BasicNameValuePair(name, value); }
@Test public void testTokenParsingWithQuotedPairs() throws Exception { final String s = "raw: \"\\\"some\\stuff\\\\\""; final CharArrayBuffer raw = createBuffer(s); final ParserCursor cursor = new ParserCursor(0, s.length()); parser.skipWhiteSpace(raw, cursor); Assert.assertFalse(cursor.atEnd()); Assert.assertEquals(0, cursor.getPos()); final StringBuilder strbuf1 = new StringBuilder(); parser.copyContent(raw, cursor, TokenParser.INIT_BITSET(':'), strbuf1); Assert.assertFalse(cursor.atEnd()); Assert.assertEquals("raw", strbuf1.toString()); Assert.assertEquals(':', raw.charAt(cursor.getPos())); cursor.updatePos(cursor.getPos() + 1); parser.skipWhiteSpace(raw, cursor); Assert.assertFalse(cursor.atEnd()); final StringBuilder strbuf2 = new StringBuilder(); parser.copyQuotedContent(raw, cursor, strbuf2); Assert.assertTrue(cursor.atEnd()); Assert.assertEquals("\"some\\stuff\\", strbuf2.toString()); }
@Test public void testBasicTokenParsing() throws Exception { final String s = " raw: \" some stuff \""; final CharArrayBuffer raw = createBuffer(s); final ParserCursor cursor = new ParserCursor(0, s.length()); parser.skipWhiteSpace(raw, cursor); Assert.assertFalse(cursor.atEnd()); Assert.assertEquals(3, cursor.getPos()); final StringBuilder strbuf1 = new StringBuilder(); parser.copyContent(raw, cursor, TokenParser.INIT_BITSET(':'), strbuf1); Assert.assertFalse(cursor.atEnd()); Assert.assertEquals(6, cursor.getPos()); Assert.assertEquals("raw", strbuf1.toString()); Assert.assertEquals(':', raw.charAt(cursor.getPos())); cursor.updatePos(cursor.getPos() + 1); parser.skipWhiteSpace(raw, cursor); Assert.assertFalse(cursor.atEnd()); Assert.assertEquals(8, cursor.getPos()); final StringBuilder strbuf2 = new StringBuilder(); parser.copyQuotedContent(raw, cursor, strbuf2); Assert.assertTrue(cursor.atEnd()); Assert.assertEquals(" some stuff ", strbuf2.toString()); parser.copyQuotedContent(raw, cursor, strbuf2); Assert.assertTrue(cursor.atEnd()); parser.skipWhiteSpace(raw, cursor); Assert.assertTrue(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()); }
@Test public void testSimpleAppend() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(16); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); final char[] b1 = buffer.toCharArray(); Assert.assertNotNull(b1); Assert.assertEquals(0, b1.length); Assert.assertTrue(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); final char[] tmp = new char[] { '1', '2', '3', '4'}; buffer.append(tmp, 0, tmp.length); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(4, buffer.length()); Assert.assertFalse(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); final char[] b2 = buffer.toCharArray(); Assert.assertNotNull(b2); Assert.assertEquals(4, b2.length); for (int i = 0; i < tmp.length; i++) { Assert.assertEquals(tmp[i], b2[i]); Assert.assertEquals(tmp[i], buffer.charAt(i)); } Assert.assertEquals("1234", buffer.toString()); buffer.clear(); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(0, buffer.length()); Assert.assertTrue(buffer.isEmpty()); Assert.assertFalse(buffer.isFull()); }