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 Jetty connector did not have any safeguard against multiple body subscriptions. Such as check has now been added. See gh-32100 Closes gh-32101
- Loading branch information
Showing
4 changed files
with
127 additions
and
106 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...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,94 @@ | ||
/* | ||
* 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.HttpStatus; | ||
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 int statusCode; | ||
|
||
private final HttpHeaders headers; | ||
|
||
private final MultiValueMap<String, ResponseCookie> cookies; | ||
|
||
private final Flux<DataBuffer> body; | ||
|
||
|
||
|
||
protected AbstractClientHttpResponse(int statusCode, HttpHeaders headers, | ||
MultiValueMap<String, ResponseCookie> cookies, Flux<DataBuffer> body) { | ||
|
||
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 HttpStatus getStatusCode() { | ||
return HttpStatus.valueOf(this.statusCode); | ||
} | ||
|
||
@Override | ||
public int getRawStatusCode() { | ||
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
186d6e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@poutsma unfortunately this commit broke Jetty's Reactive HttpClient 2.0.x.
In Jetty Reactive HttpClient 2.0.x we depend on Jetty 10.0.x and Java 11, so we cannot depend on Spring 6.0.x because it requires Java 17.
The previous version of this class (v5.3.31) was doing some reflection based on the Jetty version that was present, and adapting Jetty's
HttpFields
class to aMap
.This was necessary because in 9.4.x
HttpFields
is a class, while in 10.0.x+ it is an interface.With v5.3.31 there are no problems using Spring in either Jetty Reactive HttpClient 1.1.x (based on 9.4.x) or 2.0.x (based on 10.0.x).
But with Spring v5.3.32 Jetty Reactive HttpClient 1.1.x works fine, but 2.0.x now throws this:
Would it be possible to restore the reflection code that was removed and keep the compatibility with both 9.4.x and 10.0.x?
Let me know if I have to file an issue, or you need clarifications.
Thanks!
186d6e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making us aware of this mistake, @sbordet. I have created #32337 to resolve it.