use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project async-http-client by AsyncHttpClient.
the class AsyncHttpClientHandler method channelRead.
@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
Channel channel = ctx.channel();
Object attribute = Channels.getAttribute(channel);
try {
if (attribute instanceof OnLastHttpContentCallback) {
if (msg instanceof LastHttpContent) {
((OnLastHttpContentCallback) attribute).call();
}
} else if (attribute instanceof NettyResponseFuture) {
NettyResponseFuture<?> future = (NettyResponseFuture<?>) attribute;
future.touch();
handleRead(channel, future, msg);
} else if (attribute instanceof StreamedResponsePublisher) {
StreamedResponsePublisher publisher = (StreamedResponsePublisher) attribute;
publisher.future().touch();
if (msg instanceof HttpContent) {
ByteBuf content = ((HttpContent) msg).content();
// Republish as a HttpResponseBodyPart
if (content.isReadable()) {
HttpResponseBodyPart part = config.getResponseBodyPartFactory().newResponseBodyPart(content, false);
ctx.fireChannelRead(part);
}
if (msg instanceof LastHttpContent) {
// Remove the handler from the pipeline, this will trigger
// it to finish
ctx.pipeline().remove(publisher);
// Trigger a read, just in case the last read complete
// triggered no new read
ctx.read();
// Send the last content on to the protocol, so that it can
// conclude the cleanup
handleRead(channel, publisher.future(), msg);
}
} else {
logger.info("Received unexpected message while expecting a chunk: " + msg);
ctx.pipeline().remove(publisher);
Channels.setDiscard(channel);
}
} else if (attribute != DiscardEvent.DISCARD) {
// unhandled message
logger.debug("Orphan channel {} with attribute {} received message {}, closing", channel, attribute, msg);
Channels.silentlyCloseChannel(channel);
}
} finally {
ReferenceCountUtil.release(msg);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpClientOperations method onInboundNext.
@Override
protected void onInboundNext(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
if (response.decoderResult().isFailure()) {
onInboundError(response.decoderResult().cause());
ReferenceCountUtil.release(msg);
return;
}
if (HttpResponseStatus.CONTINUE.equals(response.status())) {
is100Continue = true;
ReferenceCountUtil.release(msg);
return;
}
if (started) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "HttpClientOperations cannot proceed more than one response {}"), response.headers().toString());
}
ReferenceCountUtil.release(msg);
return;
}
is100Continue = false;
started = true;
setNettyResponse(response);
if (!isKeepAlive()) {
markPersistent(false);
}
if (isInboundCancelled()) {
ReferenceCountUtil.release(msg);
return;
}
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Received response (auto-read:{}) : {}"), channel().config().isAutoRead(), responseHeaders().entries().toString());
}
if (notRedirected(response)) {
try {
listener().onStateChange(this, HttpClientState.RESPONSE_RECEIVED);
} catch (Exception e) {
onInboundError(e);
ReferenceCountUtil.release(msg);
return;
}
} else {
// when redirecting no need of manual reading
channel().config().setAutoRead(true);
}
if (msg instanceof FullHttpResponse) {
FullHttpResponse request = (FullHttpResponse) msg;
if (request.content().readableBytes() > 0) {
super.onInboundNext(ctx, msg);
} else {
request.release();
}
terminate();
}
return;
}
if (msg instanceof LastHttpContent) {
if (is100Continue) {
ReferenceCountUtil.release(msg);
channel().read();
return;
}
if (!started) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "HttpClientOperations received an incorrect end " + "delimiter (previously used connection?)"));
}
ReferenceCountUtil.release(msg);
return;
}
if (log.isDebugEnabled()) {
log.debug(format(channel(), "Received last HTTP packet"));
}
if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
if (redirecting != null) {
ReferenceCountUtil.release(msg);
} else {
super.onInboundNext(ctx, msg);
}
}
if (redirecting == null) {
// EmitResult is ignored as it is guaranteed that there will be only one emission of LastHttpContent
// Whether there are subscribers or the subscriber cancels is not of interest
// Evaluated EmitResult: FAIL_TERMINATED, FAIL_OVERFLOW, FAIL_CANCELLED, FAIL_NON_SERIALIZED
// FAIL_ZERO_SUBSCRIBER
trailerHeaders.tryEmitValue(((LastHttpContent) msg).trailingHeaders());
}
// force auto read to enable more accurate close selection now inbound is done
channel().config().setAutoRead(true);
if (markSentBody()) {
markPersistent(false);
}
terminate();
return;
}
if (!started) {
if (log.isDebugEnabled()) {
if (msg instanceof ByteBufHolder) {
msg = ((ByteBufHolder) msg).content();
}
log.debug(format(channel(), "HttpClientOperations received an incorrect chunk {} " + "(previously used connection?)"), msg);
}
ReferenceCountUtil.release(msg);
return;
}
if (redirecting != null) {
ReferenceCountUtil.release(msg);
// when redirecting auto-read is set to true, no need of manual reading
return;
}
super.onInboundNext(ctx, msg);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class Http2StreamBridgeServerHandler method write.
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof ByteBuf) {
// "FutureReturnValueIgnored" this is deliberate
ctx.write(new DefaultHttpContent((ByteBuf) msg), promise);
} else {
// "FutureReturnValueIgnored" this is deliberate
ChannelFuture f = ctx.write(msg, promise);
if (msg instanceof LastHttpContent) {
pendingResponse = false;
f.addListener(this);
ctx.read();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class AbstractHttpClientMetricsHandler method write.
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof HttpRequest) {
method = ((HttpRequest) msg).method().name();
ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
if (channelOps instanceof HttpClientOperations) {
HttpClientOperations ops = (HttpClientOperations) channelOps;
path = uriTagValue == null ? ops.path : uriTagValue.apply(ops.path);
contextView = ops.currentContextView();
}
startWrite((HttpRequest) msg, ctx.channel().remoteAddress());
}
if (msg instanceof ByteBufHolder) {
dataSent += ((ByteBufHolder) msg).content().readableBytes();
} else if (msg instanceof ByteBuf) {
dataSent += ((ByteBuf) msg).readableBytes();
}
if (msg instanceof LastHttpContent) {
SocketAddress address = ctx.channel().remoteAddress();
promise.addListener(future -> recordWrite(address));
}
// "FutureReturnValueIgnored" this is deliberate
ctx.write(msg, promise);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpClientOperationsTest method addNamedDecoderReplaysLastHttp.
@Test
void addNamedDecoderReplaysLastHttp() {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(), ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT).addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
assertThat(channel.pipeline().names()).first().isEqualTo("json$extractor");
Object content = channel.readInbound();
assertThat(content).isInstanceOf(ByteBuf.class);
((ByteBuf) content).release();
content = channel.readInbound();
assertThat(content).isInstanceOf(LastHttpContent.class);
((LastHttpContent) content).release();
content = channel.readInbound();
assertThat(content).isNull();
}
Aggregations