From dab29dbf9f842bcfea475a356a44789b09705f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Sat, 8 Apr 2023 12:05:19 +0200 Subject: [PATCH] fix HTTP file upload cleanup on client and server --- .../http/client/netty/NettyHttpClient.java | 10 ++++++++ .../client/netty/NettyHttpClientConfig.java | 4 ++++ .../client/netty/http1/Http1Interaction.java | 17 +++----------- .../netty/http1/HttpFileUploadHandler.java | 23 +++++++------------ 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClient.java b/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClient.java index f293fcb..ef9132f 100644 --- a/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClient.java +++ b/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClient.java @@ -2,9 +2,11 @@ package org.xbib.net.http.client.netty; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.pool.ChannelPoolHandler; +import io.netty.handler.codec.http.multipart.HttpDataFactory; import io.netty.util.concurrent.Future; import java.io.Closeable; import java.io.IOException; @@ -84,6 +86,14 @@ public class NettyHttpClient implements HttpClient, C return new ChannelInitializer<>() { @Override protected void initChannel(Channel channel) throws Exception { + channel.closeFuture().addListener((ChannelFuture future) -> { + Channel ch = future.channel(); + HttpDataFactory httpDataFactory = ch.attr(NettyHttpClientConfig.ATTRIBUTE_HTTP_DATAFACTORY).get(); + if (httpDataFactory != null) { + logger.log(Level.FINEST, "cleaning http data factory"); + httpDataFactory.cleanAllHttpData(); + } + }); interaction.setSettingsPromise(channel.newPromise()); lookupChannelInitializer(httpAddress) .init(channel, httpAddress, getClient(), builder.nettyCustomizer, interaction); diff --git a/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClientConfig.java b/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClientConfig.java index e6dcdfe..771284d 100644 --- a/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClientConfig.java +++ b/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/NettyHttpClientConfig.java @@ -2,11 +2,13 @@ package org.xbib.net.http.client.netty; import io.netty.buffer.ByteBufAllocator; import io.netty.channel.WriteBufferWaterMark; +import io.netty.handler.codec.http.multipart.HttpDataFactory; import io.netty.handler.codec.http2.Http2Settings; import io.netty.handler.logging.LogLevel; import io.netty.handler.proxy.HttpProxyHandler; import io.netty.handler.proxy.Socks4ProxyHandler; import io.netty.handler.proxy.Socks5ProxyHandler; +import io.netty.util.AttributeKey; import java.util.ArrayList; import java.util.List; import org.xbib.net.SocketConfig; @@ -16,6 +18,8 @@ import org.xbib.net.http.client.BackOff; public class NettyHttpClientConfig { + public static final AttributeKey ATTRIBUTE_HTTP_DATAFACTORY = AttributeKey.valueOf("http_datafactory"); + /** * If frame logging /traffic logging is enabled or not. */ diff --git a/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/http1/Http1Interaction.java b/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/http1/Http1Interaction.java index 60fdf54..c510267 100644 --- a/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/http1/Http1Interaction.java +++ b/net-http-client-netty/src/main/java/org/xbib/net/http/client/netty/http1/Http1Interaction.java @@ -1,12 +1,9 @@ package org.xbib.net.http.client.netty.http1; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; -import io.netty.handler.codec.http.HttpChunkedInput; import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpVersion; @@ -34,6 +31,7 @@ import org.xbib.net.http.HttpAddress; import org.xbib.net.http.HttpHeaders; import org.xbib.net.http.HttpResponseStatus; import org.xbib.net.http.client.Part; +import org.xbib.net.http.client.netty.NettyHttpClientConfig; import org.xbib.net.http.cookie.Cookie; import org.xbib.net.http.client.cookie.CookieDecoder; import org.xbib.net.http.client.cookie.CookieEncoder; @@ -131,25 +129,16 @@ public class Http1Interaction extends BaseInteraction { } io.netty.handler.codec.http.HttpRequest httpRequest = httpPostRequestEncoder.finalizeRequest(); channel.write(httpRequest); - } else { - channel.write(fullHttpRequest); } + channel.write(fullHttpRequest); if (httpPostRequestEncoder != null && httpPostRequestEncoder.isChunked()) { - logger.log(Level.FINEST, "finish chunked HTTP POST encoder"); channel.write(httpPostRequestEncoder); - } else { - logger.log(Level.FINEST, "HTTP POST encoder not chunked"); } channel.flush(); } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) { throw new IOException(e); } finally { - if (httpPostRequestEncoder != null) { - logger.log(Level.FINEST, "cleaning files of HTTP POST encoder"); - //httpPostRequestEncoder.cleanFiles(); - } - logger.log(Level.FINEST, "clean all http data"); - //httpDataFactory.cleanAllHttpData(); + channel.attr(NettyHttpClientConfig.ATTRIBUTE_HTTP_DATAFACTORY).set(httpDataFactory); } return this; } diff --git a/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/http1/HttpFileUploadHandler.java b/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/http1/HttpFileUploadHandler.java index d57d92f..004a406 100644 --- a/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/http1/HttpFileUploadHandler.java +++ b/net-http-server-netty/src/main/java/org/xbib/net/http/server/netty/http1/HttpFileUploadHandler.java @@ -39,37 +39,29 @@ public class HttpFileUploadHandler extends SimpleChannelInboundHandler