use of io.netty.handler.codec.http.HttpContent in project rest.li by linkedin.
the class RAPResponseDecoder method channelRead0.
@Override
protected void channelRead0(final ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof HttpResponse) {
HttpResponse m = (HttpResponse) msg;
_shouldCloseConnection = !HttpUtil.isKeepAlive(m);
if (HttpUtil.is100ContinueExpected(m)) {
ctx.writeAndFlush(CONTINUE).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
ctx.fireExceptionCaught(future.cause());
}
}
});
}
if (!m.decoderResult().isSuccess()) {
ctx.fireExceptionCaught(m.decoderResult().cause());
return;
}
// remove chunked encoding.
if (HttpUtil.isTransferEncodingChunked(m)) {
HttpUtil.setTransferEncodingChunked(m, false);
}
Timeout<None> timeout = ctx.channel().attr(TIMEOUT_ATTR_KEY).getAndRemove();
if (timeout == null) {
LOG.debug("dropped a response after channel inactive or exception had happened.");
return;
}
final TimeoutBufferedWriter writer = new TimeoutBufferedWriter(ctx, _maxContentLength, BUFFER_HIGH_WATER_MARK, BUFFER_LOW_WATER_MARK, timeout);
EntityStream entityStream = EntityStreams.newEntityStream(writer);
_chunkedMessageWriter = writer;
StreamResponseBuilder builder = new StreamResponseBuilder();
builder.setStatus(m.status().code());
for (Map.Entry<String, String> e : m.headers()) {
String key = e.getKey();
String value = e.getValue();
if (key.equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
builder.addCookie(value);
} else {
builder.unsafeAddHeaderValue(key, value);
}
}
ctx.fireChannelRead(builder.build(entityStream));
} else if (msg instanceof HttpContent) {
HttpContent chunk = (HttpContent) msg;
TimeoutBufferedWriter currentWriter = _chunkedMessageWriter;
// Sanity check
if (currentWriter == null) {
throw new IllegalStateException("received " + HttpContent.class.getSimpleName() + " without " + HttpResponse.class.getSimpleName());
}
if (!chunk.decoderResult().isSuccess()) {
this.exceptionCaught(ctx, chunk.decoderResult().cause());
}
currentWriter.processHttpChunk(chunk);
if (chunk instanceof LastHttpContent) {
_chunkedMessageWriter = null;
}
} else {
// something must be wrong, but let's proceed so that
// handler after us has a chance to process it.
ctx.fireChannelRead(msg);
}
}
use of io.netty.handler.codec.http.HttpContent in project rest.li by linkedin.
the class TestChannelPoolStreamHandler method testConnectionClose.
@Test(dataProvider = "connectionClose")
public void testConnectionClose(String headerName, List<String> headerValue) {
EmbeddedChannel ch = getChannel();
FakePool pool = new FakePool();
ch.attr(ChannelPoolStreamHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED);
HttpContent lastChunk = new DefaultLastHttpContent();
response.headers().set(headerName, headerValue);
ch.writeInbound(response);
ch.writeInbound(lastChunk);
Assert.assertTrue(pool.isDisposeCalled());
Assert.assertFalse(pool.isPutCalled());
}
use of io.netty.handler.codec.http.HttpContent in project rest.li by linkedin.
the class TestChannelPoolStreamHandler method testConnectionKeepAlive.
@Test(dataProvider = "connectionKeepAlive")
public void testConnectionKeepAlive(String headerName, List<String> headerValue) {
EmbeddedChannel ch = getChannel();
FakePool pool = new FakePool();
ch.attr(ChannelPoolStreamHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.ACCEPTED);
HttpContent lastChunk = new DefaultLastHttpContent();
response.headers().set(headerName, headerValue);
ch.writeInbound(response);
ch.writeInbound(lastChunk);
Assert.assertFalse(pool.isDisposeCalled());
Assert.assertTrue(pool.isPutCalled());
}
use of io.netty.handler.codec.http.HttpContent in project camel by apache.
the class HttpServerMultiplexChannelHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// store request, as this channel handler is created per pipeline
HttpRequest request = (HttpRequest) msg;
LOG.debug("Message received: {}", request);
HttpServerChannelHandler handler = getHandler(request);
if (handler != null) {
Attribute<HttpServerChannelHandler> attr = ctx.channel().attr(SERVER_HANDLER_KEY);
// store handler as attachment
attr.set(handler);
if (msg instanceof HttpContent) {
// need to hold the reference of content
HttpContent httpContent = (HttpContent) msg;
httpContent.content().retain();
}
handler.channelRead(ctx, request);
} else {
// this resource is not found, so send empty response back
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND);
response.headers().set(Exchange.CONTENT_TYPE, "text/plain");
response.headers().set(Exchange.CONTENT_LENGTH, 0);
ctx.writeAndFlush(response);
ctx.close();
}
}
use of io.netty.handler.codec.http.HttpContent in project proxyee-down by monkeyWie.
the class AbstractHttpDownBootstrap method startChunkDown.
protected void startChunkDown(ChunkInfo chunkInfo, int updateStatus) throws Exception {
HttpRequestInfo requestInfo = (HttpRequestInfo) httpDownInfo.getRequest();
RequestProto requestProto = requestInfo.requestProto();
LOGGER.debug("开始下载:" + chunkInfo);
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(clientLoopGroup).handler(new HttpDownInitializer(requestProto.getSsl(), this, chunkInfo));
if (httpDownInfo.getProxyConfig() != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
if (callback != null) {
callback.onChunkConnecting(httpDownInfo, chunkInfo);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
chunkInfo.setStatus(updateStatus);
// 重置最后下载时间
chunkInfo.setLastDownTime(System.currentTimeMillis());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
synchronized (chunkInfo) {
setChannel(chunkInfo, future.channel());
}
synchronized (requestInfo) {
LOGGER.debug("下载连接成功:channelId[" + future.channel().id() + "]\t" + chunkInfo);
if (httpDownInfo.getTaskInfo().isSupportRange()) {
requestInfo.headers().set(HttpHeaderNames.RANGE, "bytes=" + chunkInfo.getNowStartPosition() + "-" + chunkInfo.getEndPosition());
} else {
requestInfo.headers().remove(HttpHeaderNames.RANGE);
}
future.channel().writeAndFlush(httpDownInfo.getRequest());
}
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
future.channel().writeAndFlush(content);
}
} else {
LOGGER.debug("下载连接失败:" + chunkInfo);
chunkInfo.setStatus(HttpDownStatus.FAIL);
future.channel().close();
}
});
}
Aggregations