Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP request cookies to the WebSocket handshake info #26674

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.util.Collections;
import java.util.Map;

import org.springframework.http.HttpCookie;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import reactor.core.publisher.Mono;

import org.springframework.http.HttpHeaders;
Expand All @@ -44,6 +47,8 @@ public class HandshakeInfo {

private final HttpHeaders headers;

private final MultiValueMap<String, HttpCookie> cookies;

@Nullable
private final String protocol;

Expand All @@ -67,6 +72,7 @@ public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal, @N
this(uri, headers, principal, protocol, null, Collections.emptyMap(), null);
}


/**
* Constructor targetting server-side use with extra information about the
* handshake, the remote address, and a pre-existing log prefix for
Expand All @@ -81,17 +87,39 @@ public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal, @N
* messages, if any.
* @since 5.1
*/
@Deprecated
public HandshakeInfo(URI uri, HttpHeaders headers, Mono<Principal> principal,
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
Map<String, Object> attributes, @Nullable String logPrefix) {
@Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
Map<String, Object> attributes, @Nullable String logPrefix) {
this(uri, headers, CollectionUtils.toMultiValueMap(Collections.emptyMap()), principal, protocol, remoteAddress, attributes, logPrefix);
}

/**
* Constructor targetting server-side use with extra information about the
* handshake, the remote address, and a pre-existing log prefix for
* correlation.
* @param uri the endpoint URL
* @param headers request headers for server or response headers or client
* @param cookies request cookies for server
* @param principal the principal for the session
* @param protocol the negotiated sub-protocol (may be {@code null})
* @param remoteAddress the remote address where the handshake came from
* @param attributes initial attributes to use for the WebSocket session
* @param logPrefix log prefix used during the handshake for correlating log
* messages, if any.
* @since 5.4
*/
public HandshakeInfo(URI uri, HttpHeaders headers, MultiValueMap<String, HttpCookie> cookies,
Mono<Principal> principal, @Nullable String protocol, @Nullable InetSocketAddress remoteAddress,
Map<String, Object> attributes, @Nullable String logPrefix) {
Assert.notNull(uri, "URI is required");
Assert.notNull(headers, "HttpHeaders are required");
Assert.notNull(principal, "Principal is required");
Assert.notNull(attributes, "'attributes' is required");

this.uri = uri;
this.headers = headers;
this.cookies = cookies;
this.principalMono = principal;
this.protocol = protocol;
this.remoteAddress = remoteAddress;
Expand All @@ -115,6 +143,13 @@ public HttpHeaders getHeaders() {
return this.headers;
}

/**
* Return the handshake HTTP cookies.
*/
public MultiValueMap<String, HttpCookie> getCookies() {
return this.cookies;
}

/**
* Return the principal associated with the handshake HTTP request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
import org.springframework.context.Lifecycle;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
Expand Down Expand Up @@ -282,10 +285,12 @@ private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttp
// the server implementation once the handshake HTTP exchange is done.
HttpHeaders headers = new HttpHeaders();
headers.addAll(request.getHeaders());
MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
cookies.addAll(request.getCookies());
Mono<Principal> principal = exchange.getPrincipal();
String logPrefix = exchange.getLogPrefix();
InetSocketAddress remoteAddress = request.getRemoteAddress();
return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix);
return new HandshakeInfo(uri, headers, cookies, principal, protocol, remoteAddress, attributes, logPrefix);
}

}