Skip to content

Commit 19fb0f1

Browse files
committed
Merge branch '5.2.x'
2 parents 4716e48 + 21d0696 commit 19fb0f1

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri,
116116
.next()
117117
.doOnCancel(() -> {
118118
ReactorClientHttpResponse response = responseRef.get();
119-
if (response != null && response.bodyNotSubscribed()) {
120-
response.getConnection().dispose();
119+
if (response != null) {
120+
response.releaseAfterCancel(method);
121121
}
122122
});
123123
}

spring-web/src/main/java/org/springframework/http/client/reactive/ReactorClientHttpResponse.java

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.concurrent.atomic.AtomicInteger;
2121
import java.util.function.BiFunction;
2222

23+
import io.netty.buffer.ByteBufAllocator;
24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
2326
import reactor.core.publisher.Flux;
2427
import reactor.netty.Connection;
2528
import reactor.netty.NettyInbound;
@@ -28,6 +31,7 @@
2831
import org.springframework.core.io.buffer.DataBuffer;
2932
import org.springframework.core.io.buffer.NettyDataBufferFactory;
3033
import org.springframework.http.HttpHeaders;
34+
import org.springframework.http.HttpMethod;
3135
import org.springframework.http.HttpStatus;
3236
import org.springframework.http.ResponseCookie;
3337
import org.springframework.util.CollectionUtils;
@@ -43,19 +47,21 @@
4347
*/
4448
class ReactorClientHttpResponse implements ClientHttpResponse {
4549

50+
private static final Log logger = LogFactory.getLog(ReactorClientHttpResponse.class);
51+
4652
private final HttpClientResponse response;
4753

54+
private final HttpHeaders headers;
55+
4856
private final NettyInbound inbound;
4957

5058
private final NettyDataBufferFactory bufferFactory;
5159

52-
private final Connection connection;
53-
54-
private final HttpHeaders headers;
55-
56-
// 0 - not subscribed, 1 - subscribed, 2 - cancelled
60+
// 0 - not subscribed, 1 - subscribed, 2 - cancelled, 3 - cancelled via connector (before subscribe)
5761
private final AtomicInteger state = new AtomicInteger(0);
5862

63+
private final String logPrefix;
64+
5965

6066
/**
6167
* Constructor that matches the inputs from
@@ -64,26 +70,46 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
6470
*/
6571
public ReactorClientHttpResponse(HttpClientResponse response, Connection connection) {
6672
this.response = response;
73+
MultiValueMap<String, String> adapter = new NettyHeadersAdapter(response.responseHeaders());
74+
this.headers = HttpHeaders.readOnlyHttpHeaders(adapter);
6775
this.inbound = connection.inbound();
6876
this.bufferFactory = new NettyDataBufferFactory(connection.outbound().alloc());
69-
this.connection = connection;
77+
this.logPrefix = (logger.isDebugEnabled() ? "[" + connection.channel().id().asShortText() + "] " : "");
78+
}
79+
80+
/**
81+
* Constructor with inputs extracted from a {@link Connection}.
82+
* @deprecated as of 5.2.8
83+
*/
84+
@Deprecated
85+
public ReactorClientHttpResponse(HttpClientResponse response, NettyInbound inbound, ByteBufAllocator alloc) {
86+
this.response = response;
7087
MultiValueMap<String, String> adapter = new NettyHeadersAdapter(response.responseHeaders());
7188
this.headers = HttpHeaders.readOnlyHttpHeaders(adapter);
89+
this.inbound = inbound;
90+
this.bufferFactory = new NettyDataBufferFactory(alloc);
91+
this.logPrefix = "";
7292
}
7393

7494

7595
@Override
7696
public Flux<DataBuffer> getBody() {
7797
return this.inbound.receive()
7898
.doOnSubscribe(s -> {
79-
if (!this.state.compareAndSet(0, 1)) {
80-
// https://github.com/reactor/reactor-netty/issues/503
81-
// FluxReceive rejects multiple subscribers, but not after a cancel().
82-
// Subsequent subscribers after cancel() will not be rejected, but will hang instead.
83-
// So we need to reject once in cancelled state.
84-
if (this.state.get() == 2) {
85-
throw new IllegalStateException("The client response body can only be consumed once.");
86-
}
99+
if (this.state.compareAndSet(0, 1)) {
100+
return;
101+
}
102+
// https://github.com/reactor/reactor-netty/issues/503
103+
// FluxReceive rejects multiple subscribers, but not after a cancel().
104+
// Subsequent subscribers after cancel() will not be rejected, but will hang instead.
105+
// So we need to reject once in cancelled state.
106+
if (this.state.get() == 2) {
107+
throw new IllegalStateException(
108+
"The client response body can only be consumed once.");
109+
}
110+
else if (this.state.get() == 3) {
111+
throw new IllegalStateException(
112+
"The client response body has been released already due to cancellation.");
87113
}
88114
})
89115
.doOnCancel(() -> this.state.compareAndSet(1, 2))
@@ -113,6 +139,7 @@ public MultiValueMap<String, ResponseCookie> getCookies() {
113139
MultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>();
114140
this.response.cookies().values().stream().flatMap(Collection::stream)
115141
.forEach(c ->
142+
116143
result.add(c.name(), ResponseCookie.fromClientResponse(c.name(), c.value())
117144
.domain(c.domain())
118145
.path(c.path())
@@ -124,17 +151,25 @@ public MultiValueMap<String, ResponseCookie> getCookies() {
124151
}
125152

126153
/**
127-
* For use by {@link ReactorClientHttpConnector}.
154+
* Called by {@link ReactorClientHttpConnector} when a cancellation is detected
155+
* but the content has not been subscribed to. If the subscription never
156+
* materializes then the content will remain not drained. Or it could still
157+
* materialize if the cancellation happened very early, or the response
158+
* reading was delayed for some reason.
128159
*/
129-
boolean bodyNotSubscribed() {
130-
return this.state.get() == 0;
160+
void releaseAfterCancel(HttpMethod method) {
161+
if (mayHaveBody(method) && this.state.compareAndSet(0, 3)) {
162+
if (logger.isDebugEnabled()) {
163+
logger.debug(this.logPrefix + "Releasing body, not yet subscribed.");
164+
}
165+
this.inbound.receive().doOnNext(byteBuf -> {}).subscribe(byteBuf -> {}, ex -> {});
166+
}
131167
}
132168

133-
/**
134-
* For use by {@link ReactorClientHttpConnector}.
135-
*/
136-
Connection getConnection() {
137-
return this.connection;
169+
private boolean mayHaveBody(HttpMethod method) {
170+
int code = this.getRawStatusCode();
171+
return !((code >= 100 && code < 200) || code == 204 || code == 205 ||
172+
method.equals(HttpMethod.HEAD) || getHeaders().getContentLength() == 0);
138173
}
139174

140175
@Override

0 commit comments

Comments
 (0)