Skip to content

Commit 1d96f6a

Browse files
committed
HttpHeaderResponseDecorator checks for "Transfer-Encoding"
This commit extends the fix from b86c11c by checking for both existing Content-Length and Transfer-Encoding. Closes gh-25908
1 parent 7b6293f commit 1d96f6a

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/HttpHeadResponseDecorator.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.http.server.reactive;
1818

19-
import java.util.function.BiFunction;
20-
2119
import org.reactivestreams.Publisher;
2220
import reactor.core.publisher.Flux;
2321
import reactor.core.publisher.Mono;
@@ -41,24 +39,30 @@ public HttpHeadResponseDecorator(ServerHttpResponse delegate) {
4139

4240

4341
/**
44-
* Apply {@link Flux#reduce(Object, BiFunction) reduce} on the body, count
45-
* the number of bytes produced, release data buffers without writing, and
46-
* set the {@literal Content-Length} header.
42+
* Consume and release the body without writing.
43+
* <p>If the headers contain neither Content-Length nor Transfer-Encoding,
44+
* count the bytes and set Content-Length.
4745
*/
4846
@Override
4947
public final Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
50-
return Flux.from(body)
51-
.reduce(0, (current, buffer) -> {
52-
int next = current + buffer.readableByteCount();
53-
DataBufferUtils.release(buffer);
54-
return next;
55-
})
56-
.doOnNext(length -> {
57-
if (length > 0 || getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH) == null) {
58-
getHeaders().setContentLength(length);
59-
}
60-
})
61-
.then();
48+
if (shouldSetContentLength()) {
49+
return Flux.from(body)
50+
.reduce(0, (current, buffer) -> {
51+
int next = current + buffer.readableByteCount();
52+
DataBufferUtils.release(buffer);
53+
return next;
54+
})
55+
.doOnNext(length -> getHeaders().setContentLength(length))
56+
.then();
57+
}
58+
else {
59+
return Flux.from(body).then();
60+
}
61+
}
62+
63+
private boolean shouldSetContentLength() {
64+
return (getHeaders().getFirst(HttpHeaders.CONTENT_LENGTH) == null &&
65+
getHeaders().getFirst(HttpHeaders.TRANSFER_ENCODING) == null);
6266
}
6367

6468
/**

spring-web/src/test/java/org/springframework/http/server/reactive/HttpHeadResponseDecoratorTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.core.io.buffer.DataBuffer;
2626
import org.springframework.core.io.buffer.NettyDataBufferFactory;
2727
import org.springframework.core.testfixture.io.buffer.LeakAwareDataBufferFactory;
28+
import org.springframework.http.HttpHeaders;
2829
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -63,6 +64,12 @@ public void writeWithGivenContentLength() {
6364
assertThat(this.response.getHeaders().getContentLength()).isEqualTo(length);
6465
}
6566

67+
@Test // gh-25908
68+
public void writeWithGivenTransferEncoding() {
69+
this.response.getHeaders().add(HttpHeaders.TRANSFER_ENCODING, "chunked");
70+
this.response.writeWith(Flux.empty()).block();
71+
assertThat(this.response.getHeaders().getContentLength()).isEqualTo(-1);
72+
}
6673

6774
private DataBuffer toDataBuffer(String s) {
6875
DataBuffer buffer = this.bufferFactory.allocateBuffer();

0 commit comments

Comments
 (0)