use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project vert.x by eclipse.
the class ClientHandler method doMessageReceived.
@Override
protected void doMessageReceived(ClientConnection conn, ChannelHandlerContext ctx, Object msg) {
if (conn == null) {
return;
}
if (msg instanceof HttpObject) {
HttpObject obj = (HttpObject) msg;
DecoderResult result = obj.decoderResult();
if (result.isFailure()) {
// Close the connection as Netty's HttpResponseDecoder will not try further processing
// see https://github.com/netty/netty/issues/3362
conn.handleException(result.cause());
conn.close();
return;
}
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) obj;
conn.handleResponse(response);
return;
}
if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) obj;
if (chunk.content().isReadable()) {
Buffer buff = Buffer.buffer(chunk.content().slice());
conn.handleResponseChunk(buff);
}
if (chunk instanceof LastHttpContent) {
conn.handleResponseEnd((LastHttpContent) chunk);
}
return;
}
} else if (msg instanceof WebSocketFrameInternal) {
WebSocketFrameInternal frame = (WebSocketFrameInternal) msg;
switch(frame.type()) {
case BINARY:
case CONTINUATION:
case TEXT:
conn.handleWsFrame(frame);
break;
case PING:
// Echo back the content of the PING frame as PONG frame as specified in RFC 6455 Section 5.5.2
ctx.writeAndFlush(new WebSocketFrameImpl(FrameType.PONG, frame.getBinaryData()));
break;
case PONG:
// Just ignore it
break;
case CLOSE:
if (!closeFrameSent) {
// Echo back close frame and close the connection once it was written.
// This is specified in the WebSockets RFC 6455 Section 5.4.1
ctx.writeAndFlush(frame).addListener(ChannelFutureListener.CLOSE);
closeFrameSent = true;
}
break;
default:
throw new IllegalStateException("Invalid type: " + frame.type());
}
return;
}
throw new IllegalStateException("Invalid object " + msg);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class Http2ServerDowngrader method encode.
@Override
protected void encode(ChannelHandlerContext ctx, HttpObject obj, List<Object> out) throws Exception {
if (obj instanceof HttpResponse) {
Http2Headers headers = HttpConversionUtil.toHttp2Headers((HttpResponse) obj, validateHeaders);
boolean noMoreFrames = false;
if (obj instanceof FullHttpResponse) {
FullHttpResponse full = (FullHttpResponse) obj;
noMoreFrames = !full.content().isReadable() && full.trailingHeaders().isEmpty();
}
out.add(new DefaultHttp2HeadersFrame(headers, noMoreFrames));
}
if (obj instanceof LastHttpContent) {
LastHttpContent last = (LastHttpContent) obj;
encodeLastContent(last, out);
} else if (obj instanceof HttpContent) {
HttpContent cont = (HttpContent) obj;
out.add(new DefaultHttp2DataFrame(cont.content(), false));
}
ReferenceCountUtil.retain(obj);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class Http2ServerDowngraderTest method testDowngradeEndData.
@Test
public void testDowngradeEndData() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
assertTrue(ch.writeInbound(new DefaultHttp2DataFrame(hello, true)));
LastHttpContent content = ch.readInbound();
try {
assertThat(content.content().toString(CharsetUtil.UTF_8), is("hello world"));
assertTrue(content.trailingHeaders().isEmpty());
} finally {
content.release();
}
assertThat(ch.readInbound(), is(nullValue()));
assertFalse(ch.finish());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project netty by netty.
the class Http2ServerDowngraderTest method testDowngradeData.
@Test
public void testDowngradeData() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new Http2ServerDowngrader());
ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
assertTrue(ch.writeInbound(new DefaultHttp2DataFrame(hello)));
HttpContent content = ch.readInbound();
try {
assertThat(content.content().toString(CharsetUtil.UTF_8), is("hello world"));
assertFalse(content instanceof LastHttpContent);
} finally {
content.release();
}
assertThat(ch.readInbound(), is(nullValue()));
assertFalse(ch.finish());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project ambry by linkedin.
the class ChannelWriteCallback method noResponseBodyTest.
/**
* Checks the case where no body needs to be returned but just a
* {@link RestResponseChannel#onResponseComplete(Exception)} is called on the server. This should return just
* response metadata.
*/
@Test
public void noResponseBodyTest() {
EmbeddedChannel channel = createEmbeddedChannel();
// with Transfer-Encoding:Chunked
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ImmediateResponseComplete.toString(), null);
channel.writeInbound(httpRequest);
// There should be a response.
HttpResponse response = (HttpResponse) channel.readOutbound();
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
assertTrue("Response must say 'Transfer-Encoding : chunked'", HttpUtil.isTransferEncodingChunked(response));
// since this is Transfer-Encoding:chunked, there should be a LastHttpContent
assertTrue("Did not receive end marker", channel.readOutbound() instanceof LastHttpContent);
assertTrue("Channel should be alive", channel.isActive());
// with Content-Length set
HttpHeaders headers = new DefaultHttpHeaders();
headers.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, 0);
httpRequest = RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ImmediateResponseComplete.toString(), headers);
HttpUtil.setKeepAlive(httpRequest, false);
channel.writeInbound(httpRequest);
// There should be a response.
response = (HttpResponse) channel.readOutbound();
assertEquals("Response must have Content-Length set to 0", 0, HttpUtil.getContentLength(response, -1));
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
// since Content-Length is set, the response should be an instance of FullHttpResponse.
assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse);
assertFalse("Channel should not be alive", channel.isActive());
}
Aggregations