Refine search
int toRead = Math.min(buffer.readableBytes(), maxChunkSize); if (toRead > 0) { ByteBuf content = buffer.readRetainedSlice(toRead); out.add(new DefaultHttpContent(content)); int readLimit = buffer.readableBytes(); out.add(new DefaultLastHttpContent(content, validateHeaders)); resetNow(); } else {
@Override public String toString() { StringBuilder buf = new StringBuilder(super.toString()); buf.append(StringUtil.NEWLINE); appendHeaders(buf); // Remove the last newline. buf.setLength(buf.length() - StringUtil.NEWLINE.length()); return buf.toString(); }
@Override public LastHttpContent replace(ByteBuf content) { final DefaultLastHttpContent dup = new DefaultLastHttpContent(content, validateHeaders); dup.trailingHeaders().set(trailingHeaders()); return dup; }
private HttpContent invalidChunk(ByteBuf in, Exception cause) { currentState = State.BAD_MESSAGE; // Advance the readerIndex so that ByteToMessageDecoder does not complain // when we produced an invalid message without consuming anything. in.skipBytes(in.readableBytes()); HttpContent chunk = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER); chunk.setDecoderResult(DecoderResult.failure(cause)); message = null; trailer = null; return chunk; }
@Test public void prepareResponseBody_NeedsGZipping() throws Exception { originalRequestHeaders.set("Accept-Encoding", "gzip"); byte[] originBody = "blah".getBytes(); response.getHeaders().set("Content-Length", Integer.toString(originBody.length)); Mockito.when(filter.isRightSizeForGzip(response)).thenReturn(true); //Force GZip for small response response.setHasBody(true); assertTrue(filter.shouldFilter(response)); final HttpResponseMessage result = filter.apply(response); final HttpContent hc1 = filter.processContentChunk(response, new DefaultHttpContent(Unpooled.wrappedBuffer(originBody)).retain()); final HttpContent hc2 = filter.processContentChunk(response, new DefaultLastHttpContent()); final byte[] body = new byte[hc1.content().readableBytes() + hc2.content().readableBytes()]; final int hc1Len = hc1.content().readableBytes(); final int hc2Len = hc2.content().readableBytes(); hc1.content().readBytes(body, 0, hc1Len); hc2.content().readBytes(body, hc1Len, hc2Len); // Check body is a gzipped version of the origin body. byte[] unzippedBytes = IOUtils.toByteArray(new GZIPInputStream(new ByteArrayInputStream(body))); String bodyStr = new String(unzippedBytes, "UTF-8"); assertEquals("blah", bodyStr); assertEquals("gzip", result.getHeaders().getFirst("Content-Encoding")); // Check Content-Length header has been removed.; assertEquals(0, result.getHeaders().get("Content-Length").size()); }
@Override public void transmit(HttpResponseStatus responseStatus, ByteBuf body) { if (body.readableBytes() == 0) { body.release(); transmit(responseStatus, LastHttpContent.EMPTY_LAST_CONTENT, false); } else { transmit(responseStatus, new DefaultLastHttpContent(body), false); } }
LastHttpContent trailer = this.trailer; if (trailer == null) { trailer = this.trailer = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders); List<String> current = trailer.trailingHeaders().getAll(lastHeader); if (!current.isEmpty()) { int lastPos = current.size() - 1; !HttpHeaderNames.TRANSFER_ENCODING.contentEqualsIgnoreCase(headerName) && !HttpHeaderNames.TRAILER.contentEqualsIgnoreCase(headerName)) { trailer.trailingHeaders().add(headerName, value);
LastHttpContent last = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, validateHeaders); HttpConversionUtil.addHttp2ToHttpHeaders(id, headers, last.trailingHeaders(), HttpVersion.HTTP_1_1, true, true); out.add(last); HttpMessage req = newMessage(id, headers); if (!HttpUtil.isContentLengthSet(req)) { req.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); Http2DataFrame dataFrame = (Http2DataFrame) frame; if (dataFrame.isEndStream()) { out.add(new DefaultLastHttpContent(dataFrame.content().retain(), validateHeaders)); } else { out.add(new DefaultHttpContent(dataFrame.content().retain()));
@Test public void addNamedEncoderReplaysLastHttp() { ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8); EmbeddedChannel channel = new EmbeddedChannel(); HttpClientOperations ops = new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT) .addHandler("json", new JsonObjectDecoder()); channel.writeInbound(new DefaultLastHttpContent(buf)); assertThat(channel.pipeline().names().iterator().next(), is("json$extractor")); Object content = channel.readInbound(); assertThat(content, instanceOf(ByteBuf.class)); ((ByteBuf) content).release(); content = channel.readInbound(); assertThat(content, instanceOf(LastHttpContent.class)); ((LastHttpContent) content).release(); assertThat(channel.readInbound(), nullValue()); }
@Override public LastHttpContent copy() { LastHttpContent content = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER); content.trailingHeaders().set(trailingHeaders()); return content; }
private void onDataRead(Http2DataFrame dataFrame, ChannelHandlerContext ctx) throws Http2Exception { ByteBuf data = dataFrame.content(); data.retain(); if (!dataFrame.isEndStream()) { ctx.fireChannelRead(new DefaultHttpContent(data)); } else { ctx.fireChannelRead(new DefaultLastHttpContent(data)); } }
private static LastHttpContent toLastContent(ByteBuf buf) { if (buf.isReadable()) { return new DefaultLastHttpContent(buf, false); } else { return LastHttpContent.EMPTY_LAST_CONTENT; } }
@Test public void addContentChunk_does_not_throw_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives_if_same_instance() { // given RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); requestInfo.isCompleteRequestWithAllChunks = false; LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8)); lastChunk.trailingHeaders().add("somekey", "someval"); requestInfo.trailingHeaders = lastChunk.trailingHeaders(); // when requestInfo.addContentChunk(lastChunk); // then assertThat(requestInfo.trailingHeaders, is(lastChunk.trailingHeaders())); }
if (context.channel().isOpen()) { context.pipeline() .writeAndFlush(new DefaultLastHttpContent()) .addListener(f -> { if (f.isSuccess()) { headers.add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); headers.add(HttpHeaderNames.CONTENT_TYPE, mediaType); context.writeAndFlush(streamedResponse); context.read();
private HTTPCarbonMessage createResponseMessage(String payload, int statusCode, List<Header> headers, String contentType) { HTTPCarbonMessage response = new HTTPCarbonMessage( new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)); response.addHttpContent(new DefaultLastHttpContent(Unpooled.wrappedBuffer(payload .getBytes(Charset.defaultCharset())))); HttpHeaders httpHeaders = response.getHeaders(); response.setProperty(org.wso2.transport.http.netty.common.Constants.HTTP_STATUS_CODE, statusCode); response.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION, org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE); // Set the Content-Type header as the system generated value. If the user has defined a specific Content-Type // header this will be overridden. if (contentType != null) { httpHeaders.set(HttpConstants.HTTP_CONTENT_TYPE, contentType); } if (headers != null) { for (Header header : headers) { httpHeaders.set(header.getName(), header.getValue()); } } return response; }
@Override public LastHttpContent replace(ByteBuf content) { final LastHttpContent dup = new DefaultLastHttpContent(content); dup.trailingHeaders().setAll(trailingHeaders()); return dup; }
@Override public LastHttpContent replace(ByteBuf content) { return new DefaultLastHttpContent(content); }
private LastHttpContent convertTrailingHeaders(int streamId, HttpHeaders headers) throws Http2Exception { final LastHttpContent lastContent; if (headers.isEmpty()) { lastContent = LastHttpContent.EMPTY_LAST_CONTENT; } else { lastContent = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, false); convert(streamId, headers, lastContent.trailingHeaders(), true, false); } return lastContent; }