/** * Writes the cache out onto the underlying stream */ private void flushCache() throws IOException { if (this.cachePosition > 0) { this.lineBuffer.clear(); this.lineBuffer.append(Integer.toHexString(this.cachePosition)); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.buffer.write(this.cache, 0, this.cachePosition, this.outputStream); this.lineBuffer.clear(); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.cachePosition = 0; } }
/** * Writes the cache out onto the underlying stream */ private void flushCache() throws IOException { if (this.cachePosition > 0) { this.lineBuffer.clear(); this.lineBuffer.append(Integer.toHexString(this.cachePosition)); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.buffer.write(this.cache, 0, this.cachePosition, this.outputStream); this.lineBuffer.clear(); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.cachePosition = 0; } }
/** * Writes the cache and bufferToAppend to the underlying stream * as one large chunk */ private void flushCacheWithAppend(final byte[] bufferToAppend, final int off, final int len) throws IOException { this.lineBuffer.clear(); this.lineBuffer.append(Integer.toHexString(this.cachePosition + len)); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.buffer.write(this.cache, 0, this.cachePosition, this.outputStream); this.buffer.write(bufferToAppend, off, len, this.outputStream); this.lineBuffer.clear(); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.cachePosition = 0; }
/** * Writes the cache and bufferToAppend to the underlying stream * as one large chunk */ private void flushCacheWithAppend(final byte[] bufferToAppend, final int off, final int len) throws IOException { this.lineBuffer.clear(); this.lineBuffer.append(Integer.toHexString(this.cachePosition + len)); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.buffer.write(this.cache, 0, this.cachePosition, this.outputStream); this.buffer.write(bufferToAppend, off, len, this.outputStream); this.lineBuffer.clear(); this.buffer.writeLine(this.lineBuffer, this.outputStream); this.cachePosition = 0; }
private void writeTrailers(final List<? extends Header> trailers) throws IOException { if (trailers != null) { for (int i = 0; i < trailers.size(); i++) { final Header header = trailers.get(i); if (header instanceof FormattedHeader) { final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer(); buffer.writeLine(chbuffer); } else { this.lineBuffer.clear(); BasicLineFormatter.INSTANCE.formatHeader(this.lineBuffer, header); buffer.writeLine(this.lineBuffer); } } } }
@Override public void complete(final List<? extends Header> trailers) throws IOException { assertNotCompleted(); this.lineBuffer.clear(); this.lineBuffer.append("0"); this.buffer.writeLine(this.lineBuffer); writeTrailers(trailers); this.lineBuffer.clear(); this.buffer.writeLine(this.lineBuffer); super.complete(trailers); }
@Override public void complete(final List<? extends Header> trailers) throws IOException { assertNotCompleted(); this.lineBuffer.clear(); this.lineBuffer.append("0"); this.buffer.writeLine(this.lineBuffer); writeTrailers(trailers); this.lineBuffer.clear(); this.buffer.writeLine(this.lineBuffer); super.complete(trailers); }
private void writeTrailers(final List<? extends Header> trailers) throws IOException { if (trailers != null) { for (int i = 0; i < trailers.size(); i++) { final Header header = trailers.get(i); if (header instanceof FormattedHeader) { final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer(); buffer.writeLine(chbuffer); } else { this.lineBuffer.clear(); BasicLineFormatter.INSTANCE.formatHeader(this.lineBuffer, header); buffer.writeLine(this.lineBuffer); } } } }
private void writeTrailers() throws IOException { final List<? extends Header> trailers = this.trailerSupplier != null ? this.trailerSupplier.get() : null; if (trailers != null) { for (int i = 0; i < trailers.size(); i++) { final Header header = trailers.get(i); if (header instanceof FormattedHeader) { final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer(); this.buffer.writeLine(chbuffer, this.outputStream); } else { this.lineBuffer.clear(); BasicLineFormatter.INSTANCE.formatHeader(this.lineBuffer, header); this.buffer.writeLine(this.lineBuffer, this.outputStream); } } } }
private void writeTrailers() throws IOException { final List<? extends Header> trailers = this.trailerSupplier != null ? this.trailerSupplier.get() : null; if (trailers != null) { for (int i = 0; i < trailers.size(); i++) { final Header header = trailers.get(i); if (header instanceof FormattedHeader) { final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer(); this.buffer.writeLine(chbuffer, this.outputStream); } else { this.lineBuffer.clear(); BasicLineFormatter.INSTANCE.formatHeader(this.lineBuffer, header); this.buffer.writeLine(this.lineBuffer, this.outputStream); } } } }
@Override protected void writeHeadLine(final T message, final CharArrayBuffer lineBuf) throws IOException { lineBuf.clear(); final ProtocolVersion transportVersion = message.getVersion(); getLineFormatter().formatStatusLine(lineBuf, new StatusLine( transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1, message.getCode(), message.getReasonPhrase())); }
@Override protected void writeHeadLine(final T message, final CharArrayBuffer lineBuf) throws IOException { lineBuf.clear(); final ProtocolVersion transportVersion = message.getVersion(); getLineFormatter().formatRequestLine(lineBuf, new RequestLine( message.getMethod(), message.getRequestUri(), transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1)); }
@Override protected void writeHeadLine(final T message, final CharArrayBuffer lineBuf) throws IOException { lineBuf.clear(); final ProtocolVersion transportVersion = message.getVersion(); getLineFormatter().formatRequestLine(lineBuf, new RequestLine( message.getMethod(), message.getRequestUri(), transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1)); }
@Override protected void writeHeadLine(final T message, final CharArrayBuffer lineBuf) throws IOException { lineBuf.clear(); final ProtocolVersion transportVersion = message.getVersion(); getLineFormatter().formatStatusLine(lineBuf, new StatusLine( transportVersion != null ? transportVersion : HttpVersion.HTTP_1_1, message.getCode(), message.getReasonPhrase())); }
@Test public void testParamsFormatting() throws Exception { final NameValuePair param1 = new BasicNameValuePair("param", "regular_stuff"); final NameValuePair param2 = new BasicNameValuePair("param", "this\\that"); final NameValuePair param3 = new BasicNameValuePair("param", "this,that"); final NameValuePair[] params = new NameValuePair[] {param1, param2, param3}; final CharArrayBuffer buf = new CharArrayBuffer(64); buf.clear(); this.formatter.formatParameters(buf, params, false); Assert.assertEquals("param=regular_stuff; param=\"this\\\\that\"; param=\"this,that\"", buf.toString()); buf.clear(); this.formatter.formatParameters(buf, params, true); Assert.assertEquals("param=\"regular_stuff\"; param=\"this\\\\that\"; param=\"this,that\"", buf.toString()); }
@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 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()); }
@Test public void testSLFormatting() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); final StatusLine statusline1 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); this.formatter.formatStatusLine(buf, statusline1); Assert.assertEquals("HTTP/1.1 200 OK", buf.toString()); buf.clear(); final StatusLine statusline2 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null); this.formatter.formatStatusLine(buf, statusline2); Assert.assertEquals("HTTP/1.1 200 ", buf.toString()); // see "testSLParseSuccess" in TestBasicLineParser: // trailing space is correct }