use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class HttpProxyHandler method handleResponse.
@Override
protected boolean handleResponse(ChannelHandlerContext ctx, Object response) throws Exception {
if (response instanceof HttpResponse) {
if (status != null) {
throw new HttpProxyConnectException(exceptionMessage("too many responses"), /*headers=*/
null);
}
HttpResponse res = (HttpResponse) response;
status = res.status();
inboundHeaders = res.headers();
}
boolean finished = response instanceof LastHttpContent;
if (finished) {
if (status == null) {
throw new HttpProxyConnectException(exceptionMessage("missing response"), inboundHeaders);
}
if (status.code() != 200) {
throw new HttpProxyConnectException(exceptionMessage("status: " + status), inboundHeaders);
}
}
return finished;
}
use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class HttpPostStandardRequestDecoder method offer.
/**
* Initialized the internals from a new chunk
*
* @param content
* the new received chunk
* @throws ErrorDataDecoderException
* if there is a problem with the charset decoding or other
* errors
*/
@Override
public HttpPostStandardRequestDecoder offer(HttpContent content) {
checkDestroyed();
if (content instanceof LastHttpContent) {
isLastChunk = true;
}
ByteBuf buf = content.content();
if (undecodedChunk == null) {
undecodedChunk = // which is not really usable for us as we may exceed it once we add more bytes.
buf.alloc().buffer(buf.readableBytes()).writeBytes(buf);
} else {
undecodedChunk.writeBytes(buf);
}
parseBody();
if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
if (undecodedChunk.refCnt() == 1) {
// It's safe to call discardBytes() as we are the only owner of the buffer.
undecodedChunk.discardReadBytes();
} else {
// There seems to be multiple references of the buffer. Let's copy the data and release the buffer to
// ensure we can give back memory to the system.
ByteBuf buffer = undecodedChunk.alloc().buffer(undecodedChunk.readableBytes());
buffer.writeBytes(undecodedChunk);
undecodedChunk.release();
undecodedChunk = buffer;
}
}
return this;
}
use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class Http2StreamFrameToHttpObjectCodecTest method testEncodeDataEndAsClient.
@Test
public void testEncodeDataEndAsClient() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
LastHttpContent end = new DefaultLastHttpContent(hello, true);
assertTrue(ch.writeOutbound(end));
Http2DataFrame dataFrame = ch.readOutbound();
try {
assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
assertTrue(dataFrame.isEndStream());
} finally {
dataFrame.release();
}
assertThat(ch.readOutbound(), is(nullValue()));
assertFalse(ch.finish());
}
use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class Http2StreamFrameToHttpObjectCodecTest method testEncodeTrailersAsClient.
@Test
public void testEncodeTrailersAsClient() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
LastHttpContent trailers = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, true);
HttpHeaders headers = trailers.trailingHeaders();
headers.set("key", "value");
assertTrue(ch.writeOutbound(trailers));
Http2HeadersFrame headerFrame = ch.readOutbound();
assertThat(headerFrame.headers().get("key").toString(), is("value"));
assertTrue(headerFrame.isEndStream());
assertThat(ch.readOutbound(), is(nullValue()));
assertFalse(ch.finish());
}
use of io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class Http2StreamFrameToHttpObjectCodecTest method testEncodeDataEndWithTrailersAsClient.
@Test
public void testEncodeDataEndWithTrailersAsClient() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
LastHttpContent trailers = new DefaultLastHttpContent(hello, true);
HttpHeaders headers = trailers.trailingHeaders();
headers.set("key", "value");
assertTrue(ch.writeOutbound(trailers));
Http2DataFrame dataFrame = ch.readOutbound();
try {
assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
assertFalse(dataFrame.isEndStream());
} finally {
dataFrame.release();
}
Http2HeadersFrame headerFrame = ch.readOutbound();
assertThat(headerFrame.headers().get("key").toString(), is("value"));
assertTrue(headerFrame.isEndStream());
assertThat(ch.readOutbound(), is(nullValue()));
assertFalse(ch.finish());
}
Aggregations