16
16
17
17
package org .springframework .http .client .reactive ;
18
18
19
- import java .net .HttpCookie ;
20
19
import java .net .http .HttpClient ;
21
20
import java .net .http .HttpResponse ;
22
21
import java .nio .ByteBuffer ;
25
24
import java .util .Map ;
26
25
import java .util .concurrent .Flow ;
27
26
import java .util .function .Function ;
28
- import java .util .regex .Matcher ;
29
- import java .util .regex .Pattern ;
30
27
31
- import org .jspecify .annotations .Nullable ;
32
28
import reactor .adapter .JdkFlowAdapter ;
33
29
import reactor .core .publisher .Flux ;
34
30
38
34
import org .springframework .http .HttpHeaders ;
39
35
import org .springframework .http .HttpStatusCode ;
40
36
import org .springframework .http .ResponseCookie ;
37
+ import org .springframework .http .support .HttpCookieParser ;
41
38
import org .springframework .util .CollectionUtils ;
42
39
import org .springframework .util .LinkedCaseInsensitiveMap ;
43
40
import org .springframework .util .LinkedMultiValueMap ;
52
49
*/
53
50
class JdkClientHttpResponse extends AbstractClientHttpResponse {
54
51
55
- private static final Pattern SAME_SITE_PATTERN = Pattern .compile ("(?i).*SameSite=(Strict|Lax|None).*" );
56
-
57
-
58
-
59
52
public JdkClientHttpResponse (HttpResponse <Flow .Publisher <List <ByteBuffer >>> response ,
60
- DataBufferFactory bufferFactory ) {
53
+ DataBufferFactory bufferFactory , HttpCookieParser httpCookieParser ) {
61
54
62
55
super (HttpStatusCode .valueOf (response .statusCode ()),
63
56
adaptHeaders (response ),
64
- adaptCookies (response ),
57
+ adaptCookies (response , httpCookieParser ),
65
58
adaptBody (response , bufferFactory )
66
59
);
67
60
}
@@ -74,29 +67,15 @@ private static HttpHeaders adaptHeaders(HttpResponse<Flow.Publisher<List<ByteBuf
74
67
return HttpHeaders .readOnlyHttpHeaders (multiValueMap );
75
68
}
76
69
77
- private static MultiValueMap <String , ResponseCookie > adaptCookies (HttpResponse <Flow .Publisher <List <ByteBuffer >>> response ) {
70
+ private static MultiValueMap <String , ResponseCookie > adaptCookies (HttpResponse <Flow .Publisher <List <ByteBuffer >>> response ,
71
+ HttpCookieParser httpCookieParser ) {
78
72
return response .headers ().allValues (HttpHeaders .SET_COOKIE ).stream ()
79
- .flatMap (header -> {
80
- Matcher matcher = SAME_SITE_PATTERN .matcher (header );
81
- String sameSite = (matcher .matches () ? matcher .group (1 ) : null );
82
- return HttpCookie .parse (header ).stream ().map (cookie -> toResponseCookie (cookie , sameSite ));
83
- })
73
+ .flatMap (httpCookieParser ::parse )
84
74
.collect (LinkedMultiValueMap ::new ,
85
75
(cookies , cookie ) -> cookies .add (cookie .getName (), cookie ),
86
76
LinkedMultiValueMap ::addAll );
87
77
}
88
78
89
- private static ResponseCookie toResponseCookie (HttpCookie cookie , @ Nullable String sameSite ) {
90
- return ResponseCookie .from (cookie .getName (), cookie .getValue ())
91
- .domain (cookie .getDomain ())
92
- .httpOnly (cookie .isHttpOnly ())
93
- .maxAge (cookie .getMaxAge ())
94
- .path (cookie .getPath ())
95
- .secure (cookie .getSecure ())
96
- .sameSite (sameSite )
97
- .build ();
98
- }
99
-
100
79
private static Flux <DataBuffer > adaptBody (HttpResponse <Flow .Publisher <List <ByteBuffer >>> response , DataBufferFactory bufferFactory ) {
101
80
return JdkFlowAdapter .flowPublisherToFlux (response .body ())
102
81
.flatMapIterable (Function .identity ())
0 commit comments