在 Spring 引导中配置 Undertow RequestEncodingHandler
Configuring Undertow RequestEncodingHandler in Spring Boot
我正在尝试在 Undertow 中启用请求解压缩 (gzip)。在文档中指定这是由 RequestEncodingHandler
处理的。
我在 Spring config
中创建了以下组件
@Component
class UndertowWebServerCustomizer : WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
override fun customize(factory: UndertowServletWebServerFactory) {
factory.addDeploymentInfoCustomizers(
UndertowDeploymentInfoCustomizer { deploymentInfo ->
deploymentInfo.addInitialHandlerChainWrapper { handler ->
RequestEncodingHandler(handler)
.addEncoding("gzip", GzipStreamSourceConduit.WRAPPER)
.addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER)
}
}
)
}
}
看起来 Spring 正在执行此代码,但是当我尝试使用 Content-Encoding: gzip
header 上传 gzip 文件时,出现以下错误:
14:51:03.378 [XNIO-2 task-1] ERROR io.undertow.request.io - UT005090: Unexpected failure
java.lang.IllegalStateException: UT000091: Buffer has already been freed
at io.undertow.server.DefaultByteBufferPool$DefaultPooledBuffer.getBuffer(DefaultByteBufferPool.java:260)
at io.undertow.conduits.InflatingStreamSourceConduit.read(InflatingStreamSourceConduit.java:93)
at org.xnio.conduits.ConduitStreamSourceChannel.read(ConduitStreamSourceChannel.java:127)
at io.undertow.channels.DetachableStreamSourceChannel.read(DetachableStreamSourceChannel.java:209)
at io.undertow.server.HttpServerExchange$ReadDispatchChannel.read(HttpServerExchange.java:2341)
at org.xnio.channels.Channels.readBlocking(Channels.java:294)
at io.undertow.servlet.spec.ServletInputStreamImpl.readIntoBuffer(ServletInputStreamImpl.java:192)
at io.undertow.servlet.spec.ServletInputStreamImpl.close(ServletInputStreamImpl.java:257)
at io.undertow.servlet.spec.HttpServletRequestImpl.closeAndDrainRequest(HttpServletRequestImpl.java:682)
at io.undertow.servlet.core.ServletBlockingHttpExchange.close(ServletBlockingHttpExchange.java:89)
at io.undertow.server.HttpServerExchange.endExchange(HttpServerExchange.java:1624)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:385)
at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:830)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
设置此自定义的正确方法是什么?
显然发布的代码是正确的,问题出在我用来测试它的 gzip 文件上
我正在尝试在 Undertow 中启用请求解压缩 (gzip)。在文档中指定这是由 RequestEncodingHandler
处理的。
我在 Spring config
@Component
class UndertowWebServerCustomizer : WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
override fun customize(factory: UndertowServletWebServerFactory) {
factory.addDeploymentInfoCustomizers(
UndertowDeploymentInfoCustomizer { deploymentInfo ->
deploymentInfo.addInitialHandlerChainWrapper { handler ->
RequestEncodingHandler(handler)
.addEncoding("gzip", GzipStreamSourceConduit.WRAPPER)
.addEncoding("deflate", InflatingStreamSourceConduit.WRAPPER)
}
}
)
}
}
看起来 Spring 正在执行此代码,但是当我尝试使用 Content-Encoding: gzip
header 上传 gzip 文件时,出现以下错误:
14:51:03.378 [XNIO-2 task-1] ERROR io.undertow.request.io - UT005090: Unexpected failure
java.lang.IllegalStateException: UT000091: Buffer has already been freed
at io.undertow.server.DefaultByteBufferPool$DefaultPooledBuffer.getBuffer(DefaultByteBufferPool.java:260)
at io.undertow.conduits.InflatingStreamSourceConduit.read(InflatingStreamSourceConduit.java:93)
at org.xnio.conduits.ConduitStreamSourceChannel.read(ConduitStreamSourceChannel.java:127)
at io.undertow.channels.DetachableStreamSourceChannel.read(DetachableStreamSourceChannel.java:209)
at io.undertow.server.HttpServerExchange$ReadDispatchChannel.read(HttpServerExchange.java:2341)
at org.xnio.channels.Channels.readBlocking(Channels.java:294)
at io.undertow.servlet.spec.ServletInputStreamImpl.readIntoBuffer(ServletInputStreamImpl.java:192)
at io.undertow.servlet.spec.ServletInputStreamImpl.close(ServletInputStreamImpl.java:257)
at io.undertow.servlet.spec.HttpServletRequestImpl.closeAndDrainRequest(HttpServletRequestImpl.java:682)
at io.undertow.servlet.core.ServletBlockingHttpExchange.close(ServletBlockingHttpExchange.java:89)
at io.undertow.server.HttpServerExchange.endExchange(HttpServerExchange.java:1624)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:385)
at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:830)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
设置此自定义的正确方法是什么?
显然发布的代码是正确的,问题出在我用来测试它的 gzip 文件上