spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
55.58k stars 37.73k forks source link

Support HTTP trailing headers #32890

Open bmaassenee opened 1 month ago

bmaassenee commented 1 month ago

With the current WebClient implementation its not possible to get the trailer data of a chunked http response. For example for the response:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/binary
Connection: Keep-Alive
Keep-Alive: timeout-20
Date: Fri, 03 May 2024 14:26:43 GMT
Cache-Control: max-age=1
x-ee-cameraid: 100f9718
accept-ranges: bytes
trailer: x-result,x-error

0
x-result: 1
x-error: Failed to determine asset size

The ResponseEntity object has no way to access the trailer. The data you get from the body doesn't contain the trailer info, and neither does the headers object. I debugged the issue a little and found that netty correctly parses and makes the data available: image

But the info is "thrown away" in the reactor.netty.ByteBufFlux

    final static Function<Object, ByteBuf> bytebufExtractor = o -> {
        if (o instanceof ByteBuf) {
            return (ByteBuf) o;
        }
        if (o instanceof ByteBufHolder) {
            return ((ByteBufHolder) o).content();
        }
        if (o instanceof byte[]) {
            return Unpooled.wrappedBuffer((byte[]) o);
        }
        throw new IllegalArgumentException("Object " + o + " of type " + o.getClass() + " " + "cannot be converted to ByteBuf");
    };

As this only looks at the content of the LastHttpContent, and ignores the trailerHeaders.

That said, i'm also not really sure how to properly make the trailers available, best i could come up with is an extra mono in the response entity object, as the info would only be available once the full body has been read. But hopefully you can think of something better :)

bclozel commented 1 month ago

Currently, neither Spring MVC, Spring WebFlux nor any of our HTTP clients support HTTP trailing headers. This is a requirement for enhancement requests like #24425