HttpHeadResponseDecorator sets Content-Length for Mono only

See gh-25908
This commit is contained in:
Rossen Stoyanchev
2020-10-13 23:45:51 +01:00
parent abd79d43af
commit 7e647ab1d8
2 changed files with 22 additions and 10 deletions

View File

@@ -41,18 +41,22 @@ public class HttpHeadResponseDecorator extends ServerHttpResponseDecorator {
/**
* Consume and release the body without writing.
* <p>If the headers contain neither Content-Length nor Transfer-Encoding,
* count the bytes and set Content-Length.
* and the body is a {@link Mono}, count the bytes and set Content-Length.
*/
@Override
@SuppressWarnings("unchecked")
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (shouldSetContentLength()) {
return Flux.from(body)
.reduce(0, (current, buffer) -> {
int next = current + buffer.readableByteCount();
DataBufferUtils.release(buffer);
return next;
if (shouldSetContentLength() && body instanceof Mono) {
return ((Mono<? extends DataBuffer>) body)
.doOnSuccess(buffer -> {
if (buffer != null) {
getHeaders().setContentLength(buffer.readableByteCount());
DataBufferUtils.release(buffer);
}
else {
getHeaders().setContentLength(0);
}
})
.doOnNext(length -> getHeaders().setContentLength(length))
.then();
}
else {