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; } }
@Override protected final void data(final CharBuffer src, final boolean endOfStream) { Args.notNull(src, "CharBuffer"); final int chunk = src.remaining(); content.ensureCapacity(chunk); src.get(content.array(), content.length(), chunk); content.setLength(content.length() + chunk); }
@Override public String getValue() { return this.buffer.substringTrimmed(this.valuePos, this.buffer.length()); }
switch (st) { case CHUNK_CRLF: lineBuffer.clear(); final int bytesRead1 = this.buffer.readLine(lineBuffer, inputStream); if (bytesRead1 == -1) { "CRLF expected at end of chunk"); if (!lineBuffer.isEmpty()) { throw new MalformedChunkCodingException( "Unexpected content at the end of chunk"); lineBuffer.clear(); final int bytesRead2 = this.buffer.readLine(lineBuffer, inputStream); if (bytesRead2 == -1) { "Premature end of chunk coded message body: closing chunk expected"); int separator = lineBuffer.indexOf(';'); if (separator < 0) { separator = lineBuffer.length(); final String s = this.lineBuffer.substringTrimmed(0, separator); try { return Long.parseLong(s, 16);
@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()); }
for (;;) { if (current == null) { current = new CharArrayBuffer(64); } else { current.clear(); if (readLen == -1 || current.length() < 1) { break; if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) { while (i < current.length()) { final char ch = current.charAt(i); if (ch != ' ' && ch != '\t') { break; && 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 { headerLines.add(current);
@Test public void testHEFringeCase3() throws Exception { final String headerValue = ",, ,, ,"; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.append(headerValue); final ParserCursor cursor = new ParserCursor(0, buf.length()); final HeaderElement[] elements = this.parser.parseElements(buf, cursor); Assert.assertEquals("Number of elements", 0, elements.length); }
lineBuffer.ensureCapacity(requiredCapacity); final int off = buffer().position(); final int len = buffer().remaining(); lineBuffer.append(b, off, len); buffer().position(off + len); } else { while (buffer().hasRemaining()) { lineBuffer.append((char) (buffer().get() & 0xff)); lineBuffer.append( this.charbuffer.array(), this.charbuffer.position(), lineBuffer.append( this.charbuffer.array(), this.charbuffer.position(), int l = lineBuffer.length(); if (l > 0) { if (lineBuffer.charAt(l - 1) == Chars.LF) { l--; lineBuffer.setLength(l); if (l > 0 && lineBuffer.charAt(l - 1) == Chars.CR) { l--; lineBuffer.setLength(l);
@Test public void testSubstring() { final CharArrayBuffer buffer = new CharArrayBuffer(16); buffer.append(" name: value "); Assert.assertEquals(5, buffer.indexOf(':')); Assert.assertEquals(" name", buffer.substring(0, 5)); Assert.assertEquals(" value ", buffer.substring(6, buffer.length())); Assert.assertEquals("name", buffer.substringTrimmed(0, 5)); Assert.assertEquals("value", buffer.substringTrimmed(6, buffer.length())); Assert.assertEquals("", buffer.substringTrimmed(13, buffer.length())); }
@Test public void testExpandAppend() throws Exception { final CharArrayBuffer buffer = new CharArrayBuffer(4); Assert.assertEquals(4, buffer.capacity()); final char[] tmp = new char[] { '1', '2', '3', '4'}; buffer.append(tmp, 0, 2); buffer.append(tmp, 0, 4); buffer.append(tmp, 0, 0); Assert.assertEquals(8, buffer.capacity()); Assert.assertEquals(6, buffer.length()); buffer.append(tmp, 0, 4); Assert.assertEquals(16, buffer.capacity()); Assert.assertEquals(10, buffer.length()); Assert.assertEquals("1212341234", buffer.toString()); }
@Test public void testHttpVersionFormatting() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); this.formatter.formatProtocolVersion(buf, HttpVersion.HTTP_1_1); Assert.assertEquals("HTTP/1.1", buf.toString()); }
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; }
@Test public void testHeaderFormatting() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); final Header header1 = new BasicHeader("name", "value"); this.formatter.formatHeader(buf, header1); Assert.assertEquals("name: value", buf.toString()); buf.clear(); final Header header2 = new BasicHeader("name", null); this.formatter.formatHeader(buf, header2); Assert.assertEquals("name: ", buf.toString()); }
List<NameValuePair> parse(final String s) { if (s == null) { return null; } final CharArrayBuffer buffer = new CharArrayBuffer(s.length()); buffer.append(s); final ParserCursor cursor = new ParserCursor(0, s.length()); return parse(buffer, cursor); }
@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 void formatHeader(final CharArrayBuffer buffer, final Header header) { Args.notNull(buffer, "Char array buffer"); Args.notNull(header, "Header"); buffer.append(header.getName()); buffer.append(": "); final String value = header.getValue(); if (value != null) { buffer.ensureCapacity(buffer.length() + value.length()); for (int valueIndex = 0; valueIndex < value.length(); valueIndex++) { char valueChar = value.charAt(valueIndex); if (valueChar == '\r' || valueChar == '\n' || valueChar == '\f' || valueChar == 0x0b) { valueChar = ' '; } buffer.append(valueChar); } } }
return Collections.emptyList(); buf = new CharArrayBuffer(len > 0 ? (int) len : 1024); final Reader reader = new InputStreamReader(inStream, charset); final char[] tmp = new char[1024]; int l; while ((l = reader.read(tmp)) != -1) { buf.append(tmp, 0, l); if (buf.isEmpty()) { return Collections.emptyList();
if (lineBuffer.length() > 0 ) { if (this.charEncoder == null) { final int requiredCapacity = buffer().position() + lineBuffer.length(); ensureCapacity(requiredCapacity); if (buffer().hasArray()) { final byte[] b = buffer().array(); final int len = lineBuffer.length(); final int off = buffer().position(); for (int i = 0; i < len; i++) { b[off + i] = (byte) lineBuffer.charAt(i); for (int i = 0; i < lineBuffer.length(); i++) { buffer().put((byte) lineBuffer.charAt(i)); int remaining = lineBuffer.length(); int offset = 0; while (remaining > 0) { this.charbuffer.put(lineBuffer.array(), offset, l); this.charbuffer.flip();