-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guard against multiple body subscriptions
Before this commit, the JDK and Jetty connectors do not have any safeguards against multiple body subscriptions. Such as check has now been added. See gh-32100 Closes gh-32102
- Loading branch information
Showing
5 changed files
with
140 additions
and
124 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...eb/src/main/java/org/springframework/http/client/reactive/AbstractClientHttpResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.http.client.reactive; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import reactor.core.publisher.Flux; | ||
|
||
import org.springframework.core.io.buffer.DataBuffer; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
/** | ||
* Base class for {@link ClientHttpResponse} implementations. | ||
* | ||
* @author Arjen Poutsma | ||
* @since 5.3.32 | ||
*/ | ||
public abstract class AbstractClientHttpResponse implements ClientHttpResponse { | ||
|
||
private final HttpStatusCode statusCode; | ||
|
||
private final HttpHeaders headers; | ||
|
||
private final MultiValueMap<String, ResponseCookie> cookies; | ||
|
||
private final Flux<DataBuffer> body; | ||
|
||
|
||
|
||
protected AbstractClientHttpResponse(HttpStatusCode statusCode, HttpHeaders headers, | ||
MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) { | ||
|
||
Assert.notNull(statusCode, "StatusCode must not be null"); | ||
Assert.notNull(headers, "Headers must not be null"); | ||
Assert.notNull(body, "Body must not be null"); | ||
|
||
this.statusCode = statusCode; | ||
this.headers = headers; | ||
this.cookies = cookies; | ||
this.body = singleSubscription(body); | ||
} | ||
|
||
private static Flux<DataBuffer> singleSubscription(Flux<DataBuffer> body) { | ||
AtomicBoolean subscribed = new AtomicBoolean(); | ||
return body.doOnSubscribe(s -> { | ||
if (!subscribed.compareAndSet(false, true)) { | ||
throw new IllegalStateException("The client response body can only be consumed once"); | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
public HttpStatusCode getStatusCode() { | ||
return this.statusCode; | ||
} | ||
|
||
@Override | ||
public HttpHeaders getHeaders() { | ||
return this.headers; | ||
} | ||
|
||
@Override | ||
public MultiValueMap<String, ResponseCookie> getCookies() { | ||
return this.cookies; | ||
} | ||
|
||
@Override | ||
public Flux<DataBuffer> getBody() { | ||
return this.body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.