use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project reactor-netty by reactor.
the class HttpTrafficHandler method write.
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
// modify message on way out to add headers if needed
if (msg instanceof HttpResponse) {
final HttpResponse response = (HttpResponse) msg;
nonInformationalResponse = !isInformational(response);
// Assume the response writer knows if they can persist or not and sets isKeepAlive on the response
boolean maxKeepAliveRequestsReached = maxKeepAliveRequests != -1 && HttpServerOperations.requestsCounter(ctx.channel()) == maxKeepAliveRequests;
if (maxKeepAliveRequestsReached || !isKeepAlive(response) || !isSelfDefinedMessageLength(response)) {
// No longer keep alive as the client can't tell when the message is done unless we close connection
pendingResponses = 0;
persistentConnection = false;
}
// Server might think it can keep connection alive, but we should fix response header if we know better
if (!shouldKeepAlive()) {
setKeepAlive(response, false);
}
if (response.status().equals(HttpResponseStatus.CONTINUE)) {
// "FutureReturnValueIgnored" this is deliberate
ctx.write(msg, promise);
return;
}
}
if (msg instanceof LastHttpContent) {
if (!shouldKeepAlive()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Detected non persistent http " + "connection, preparing to close"), pendingResponses);
}
ctx.write(msg, promise.unvoid()).addListener(this).addListener(ChannelFutureListener.CLOSE);
return;
}
ctx.write(msg, promise.unvoid()).addListener(this);
if (!persistentConnection) {
return;
}
if (nonInformationalResponse) {
nonInformationalResponse = false;
pendingResponses -= 1;
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Decreasing pending responses, now {}"), pendingResponses);
}
}
if (pipelined != null && !pipelined.isEmpty()) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Draining next pipelined " + "request, pending response count: {}, queued: {}"), pendingResponses, pipelined.size());
}
ctx.executor().execute(this);
} else {
ctx.read();
}
return;
}
if (persistentConnection && pendingResponses == 0) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Dropped HTTP content, " + "since response has been sent already: {}"), toPrettyHexDump(msg));
}
ReferenceCountUtil.release(msg);
promise.setSuccess();
return;
}
// "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 HttpTrafficHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (secure == null) {
secure = ctx.channel().pipeline().get(SslHandler.class) != null;
}
if (remoteAddress == null) {
remoteAddress = Optional.ofNullable(HAProxyMessageReader.resolveRemoteAddressFromProxyProtocol(ctx.channel())).orElse(ctx.channel().remoteAddress());
}
// read message and track if it was keepAlive
if (msg instanceof HttpRequest) {
IdleTimeoutHandler.removeIdleTimeoutHandler(ctx.pipeline());
final HttpRequest request = (HttpRequest) msg;
if (H2.equals(request.protocolVersion())) {
sendDecodingFailures(new IllegalStateException("Unexpected request [" + request.method() + " " + request.uri() + " HTTP/2.0]"), msg);
return;
}
if (persistentConnection) {
pendingResponses += 1;
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Increasing pending responses, now {}"), pendingResponses);
}
persistentConnection = isKeepAlive(request);
} else {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Dropping pipelined HTTP request, " + "previous response requested connection close"));
}
ReferenceCountUtil.release(msg);
return;
}
if (pendingResponses > 1) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Buffering pipelined HTTP request, " + "pending response count: {}, queue: {}"), pendingResponses, pipelined != null ? pipelined.size() : 0);
}
overflow = true;
doPipeline(ctx, msg);
return;
} else {
overflow = false;
DecoderResult decoderResult = request.decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), msg);
return;
}
HttpServerOperations ops;
try {
ops = new HttpServerOperations(Connection.from(ctx.channel()), listener, request, compress, ConnectionInfo.from(ctx.channel(), request, secure, remoteAddress, forwardedHeaderHandler), cookieDecoder, cookieEncoder, formDecoderProvider, mapHandle, secure);
} catch (RuntimeException e) {
sendDecodingFailures(e, msg);
return;
}
ops.bind();
listener.onStateChange(ops, ConnectionObserver.State.CONFIGURED);
ctx.fireChannelRead(msg);
return;
}
} else if (persistentConnection && pendingResponses == 0) {
if (msg instanceof LastHttpContent) {
DecoderResult decoderResult = ((LastHttpContent) msg).decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), msg);
return;
}
ctx.fireChannelRead(msg);
} else {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Dropped HTTP content, " + "since response has been sent already: {}"), msg);
}
ReferenceCountUtil.release(msg);
}
ctx.read();
return;
} else if (overflow) {
if (HttpServerOperations.log.isDebugEnabled()) {
HttpServerOperations.log.debug(format(ctx.channel(), "Buffering pipelined HTTP content, " + "pending response count: {}, pending pipeline:{}"), pendingResponses, pipelined != null ? pipelined.size() : 0);
}
doPipeline(ctx, msg);
return;
}
if (msg instanceof DecoderResultProvider) {
DecoderResult decoderResult = ((DecoderResultProvider) msg).decoderResult();
if (decoderResult.isFailure()) {
sendDecodingFailures(decoderResult.cause(), msg);
return;
}
}
ctx.fireChannelRead(msg);
}
use of org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForRequestAndLastContentSourceAndDumpFullRequests.
@Test
public final void testTradeForRequestAndLastContentSourceAndDumpFullRequests() throws IOException {
final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
final LastHttpContent lastcontent = new DefaultLastHttpContent(Nettys4Test.buildByteBuf(REQ_CONTENT));
final EmbeddedChannel channel = new EmbeddedChannel();
final HttpTrade trade = new DefaultHttpTrade(channel);
writeToInboundAndFlush(channel, request);
writeToInboundAndFlush(channel, lastcontent);
// final Func0<FullHttpRequest> fullRequestBuilder =
// trade.inboundHolder().fullOf(RxNettys.BUILD_FULL_REQUEST);
// expected refCnt, request -- 1 + HttpMessageHolder -- 1, total refcnt is 2
// TODO, why 4 & 5 refCnt ?
callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 4);
callByteBufHolderBuilderOnceAndAssertDumpContentAndRefCnt(trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap(), REQ_CONTENT.getBytes(Charsets.UTF_8), 5);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.LastHttpContent in project jocean-http by isdom.
the class Nettys method httpobjs2fullresp.
// retain when build fullresp
public static FullHttpResponse httpobjs2fullresp(final List<HttpObject> httpobjs) {
if (httpobjs.size() > 0 && (httpobjs.get(0) instanceof HttpResponse) && (httpobjs.get(httpobjs.size() - 1) instanceof LastHttpContent)) {
if (httpobjs.get(0) instanceof FullHttpResponse) {
return ((FullHttpResponse) httpobjs.get(0)).retainedDuplicate();
}
final HttpResponse resp = (HttpResponse) httpobjs.get(0);
final DefaultFullHttpResponse fullresp = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status(), tobuf(httpobjs));
fullresp.headers().add(resp.headers());
// ? need update Content-Length header field ?
return fullresp;
} else {
throw new RuntimeException("invalid HttpObjects");
}
}
Aggregations