use of io.netty.handler.codec.http.LastHttpContent in project proxyee-down by monkeyWie.
the class SniffIntercept method beforeRequest.
@Override
public void beforeRequest(Channel clientChannel, HttpContent httpContent, HttpProxyInterceptPipeline pipeline) throws Exception {
if (!matchFlag) {
super.beforeRequest(clientChannel, httpContent, pipeline);
return;
}
if (content != null) {
ByteBuf temp = httpContent.content().slice();
content.writeBytes(temp);
if (httpContent instanceof LastHttpContent) {
try {
byte[] contentBts = new byte[content.readableBytes()];
content.readBytes(contentBts);
((HttpRequestInfo) pipeline.getHttpRequest()).setContent(contentBts);
} finally {
ReferenceCountUtil.release(content);
}
}
}
pipeline.beforeRequest(clientChannel, httpContent);
}
use of 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 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 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 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