|
21 | 21 | import java.time.ZonedDateTime;
|
22 | 22 | import java.time.format.DateTimeFormatter;
|
23 | 23 | import java.time.format.DateTimeParseException;
|
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.List; |
24 | 26 |
|
25 | 27 | import org.apache.hc.client5.http.cookie.Cookie;
|
| 28 | +import org.apache.hc.client5.http.cookie.CookieOrigin; |
| 29 | +import org.apache.hc.client5.http.cookie.CookieSpec; |
| 30 | +import org.apache.hc.client5.http.cookie.MalformedCookieException; |
26 | 31 | import org.apache.hc.client5.http.protocol.HttpClientContext;
|
| 32 | +import org.apache.hc.core5.http.Header; |
27 | 33 | import org.apache.hc.core5.http.HttpResponse;
|
28 | 34 | import org.apache.hc.core5.http.Message;
|
29 | 35 | import org.reactivestreams.Publisher;
|
@@ -53,23 +59,47 @@ public HttpComponentsClientHttpResponse(DataBufferFactory dataBufferFactory,
|
53 | 59 |
|
54 | 60 | super(HttpStatusCode.valueOf(message.getHead().getCode()),
|
55 | 61 | HttpHeaders.readOnlyHttpHeaders(new HttpComponentsHeadersAdapter(message.getHead())),
|
56 |
| - adaptCookies(context), |
| 62 | + adaptCookies(message.getHead(), context), |
57 | 63 | Flux.from(message.getBody()).map(dataBufferFactory::wrap)
|
58 | 64 | );
|
59 | 65 | }
|
60 | 66 |
|
61 |
| - private static MultiValueMap<String, ResponseCookie> adaptCookies(HttpClientContext context) { |
| 67 | + private static MultiValueMap<String, ResponseCookie> adaptCookies( |
| 68 | + HttpResponse response, HttpClientContext context) { |
| 69 | + |
62 | 70 | LinkedMultiValueMap<String, ResponseCookie> result = new LinkedMultiValueMap<>();
|
63 |
| - context.getCookieStore().getCookies().forEach(cookie -> |
64 |
| - result.add(cookie.getName(), |
65 |
| - ResponseCookie.fromClientResponse(cookie.getName(), cookie.getValue()) |
66 |
| - .domain(cookie.getDomain()) |
67 |
| - .path(cookie.getPath()) |
68 |
| - .maxAge(getMaxAgeSeconds(cookie)) |
69 |
| - .secure(cookie.isSecure()) |
70 |
| - .httpOnly(cookie.containsAttribute("httponly")) |
71 |
| - .sameSite(cookie.getAttribute("samesite")) |
72 |
| - .build())); |
| 71 | + |
| 72 | + CookieSpec cookieSpec = context.getCookieSpec(); |
| 73 | + CookieOrigin cookieOrigin = context.getCookieOrigin(); |
| 74 | + |
| 75 | + Iterator<Header> itr = response.headerIterator(HttpHeaders.SET_COOKIE); |
| 76 | + while (itr.hasNext()) { |
| 77 | + Header header = itr.next(); |
| 78 | + try { |
| 79 | + List<Cookie> cookies = cookieSpec.parse(header, cookieOrigin); |
| 80 | + for (Cookie cookie : cookies) { |
| 81 | + try { |
| 82 | + cookieSpec.validate(cookie, cookieOrigin); |
| 83 | + result.add(cookie.getName(), |
| 84 | + ResponseCookie.fromClientResponse(cookie.getName(), cookie.getValue()) |
| 85 | + .domain(cookie.getDomain()) |
| 86 | + .path(cookie.getPath()) |
| 87 | + .maxAge(getMaxAgeSeconds(cookie)) |
| 88 | + .secure(cookie.isSecure()) |
| 89 | + .httpOnly(cookie.containsAttribute("httponly")) |
| 90 | + .sameSite(cookie.getAttribute("samesite")) |
| 91 | + .build()); |
| 92 | + } |
| 93 | + catch (final MalformedCookieException ex) { |
| 94 | + // ignore invalid cookie |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + catch (final MalformedCookieException ex) { |
| 99 | + // ignore invalid cookie |
| 100 | + } |
| 101 | + } |
| 102 | + |
73 | 103 | return result;
|
74 | 104 | }
|
75 | 105 |
|
|
0 commit comments