javaspringspring-bootkotlinundertow

Configuring Undertow RequestEncodingHandler in Spring Boot


I am trying to enable request decompressing (gzip) in Undertow. In the documentation its specified that this is handled by RequestEncodingHandler. I have created following component in 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)
                }
            }
        )
    }
}

Looks like Spring is executing this code but when I try to upload gzipped file with Content-Encoding: gzip header I get following error:

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$1.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)

What is the correct way to setup this customization?


Solution

  • Apparently posted code is correct, problem was with gzipped file that I was testing this with