/
ReactorNettyRequestUpgradeStrategy.java
193 lines (168 loc) · 6.83 KB
/
ReactorNettyRequestUpgradeStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright 2002-2020 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.web.reactive.socket.server.upgrade;
import java.net.URI;
import java.util.function.Supplier;
import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServerResponse;
import reactor.netty.http.server.WebsocketServerSpec;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.reactive.socket.HandshakeInfo;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession;
import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
import org.springframework.web.server.ServerWebExchange;
/**
* A {@link RequestUpgradeStrategy} for use with Reactor Netty.
*
* @author Rossen Stoyanchev
* @since 5.0
*/
public class ReactorNettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
private final Supplier<WebsocketServerSpec.Builder> specBuilderSupplier;
@Nullable
private Integer maxFramePayloadLength;
@Nullable
private Boolean handlePing;
/**
* Create an instances with a default {@link WebsocketServerSpec.Builder}.
* @since 5.2.6
*/
public ReactorNettyRequestUpgradeStrategy() {
this(WebsocketServerSpec::builder);
}
/**
* Create an instance with a pre-configured {@link WebsocketServerSpec.Builder}
* to use for WebSocket upgrades.
* @since 5.2.6
*/
public ReactorNettyRequestUpgradeStrategy(Supplier<WebsocketServerSpec.Builder> builderSupplier) {
Assert.notNull(builderSupplier, "WebsocketServerSpec.Builder is required");
this.specBuilderSupplier = builderSupplier;
}
/**
* Build an instance of {@code WebsocketServerSpec} that reflects the current
* configuration. This can be used to check the configured parameters except
* for sub-protocols which depend on the {@link WebSocketHandler} that is used
* for a given upgrade.
* @since 5.2.6
*/
public WebsocketServerSpec getWebsocketServerSpec() {
return buildSpec(null);
}
WebsocketServerSpec buildSpec(@Nullable String subProtocol) {
WebsocketServerSpec.Builder builder = this.specBuilderSupplier.get();
if (subProtocol != null) {
builder.protocols(subProtocol);
}
if (this.maxFramePayloadLength != null) {
builder.maxFramePayloadLength(this.maxFramePayloadLength);
}
if (this.handlePing != null) {
builder.handlePing(this.handlePing);
}
return builder.build();
}
/**
* Configure the maximum allowable frame payload length. Setting this value
* to your application's requirement may reduce denial of service attacks
* using long data frames.
* <p>Corresponds to the argument with the same name in the constructor of
* {@link io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory
* WebSocketServerHandshakerFactory} in Netty.
* <p>By default set to 65536 (64K).
* @param maxFramePayloadLength the max length for frames.
* @since 5.1
* @deprecated as of 5.2.6 in favor of providing a supplier of
* {@link WebsocketServerSpec.Builder} wiht a constructor argument.
*/
@Deprecated
public void setMaxFramePayloadLength(Integer maxFramePayloadLength) {
this.maxFramePayloadLength = maxFramePayloadLength;
}
/**
* Return the configured max length for frames.
* @since 5.1
* @deprecated as of 5.2.6 in favor of {@link #getWebsocketServerSpec()}
*/
@Deprecated
public int getMaxFramePayloadLength() {
return getWebsocketServerSpec().maxFramePayloadLength();
}
/**
* Configure whether to let ping frames through to be handled by the
* {@link WebSocketHandler} given to the upgrade method. By default, Reactor
* Netty automatically replies with pong frames in response to pings. This is
* useful in a proxy for allowing ping and pong frames through.
* <p>By default this is set to {@code false} in which case ping frames are
* handled automatically by Reactor Netty. If set to {@code true}, ping
* frames will be passed through to the {@link WebSocketHandler}.
* @param handlePing whether to let Ping frames through for handling
* @since 5.2.4
* @deprecated as of 5.2.6 in favor of providing a supplier of
* {@link WebsocketServerSpec.Builder} wiht a constructor argument.
*/
@Deprecated
public void setHandlePing(boolean handlePing) {
this.handlePing = handlePing;
}
/**
* Return the configured {@link #setHandlePing(boolean)}.
* @since 5.2.4
* @deprecated as of 5.2.6 in favor of {@link #getWebsocketServerSpec()}
*/
@Deprecated
public boolean getHandlePing() {
return getWebsocketServerSpec().handlePing();
}
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
@Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
ServerHttpResponse response = exchange.getResponse();
HttpServerResponse reactorResponse = getNativeResponse(response);
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
URI uri = exchange.getRequest().getURI();
// Trigger WebFlux preCommit actions and upgrade
return response.setComplete()
.then(Mono.defer(() -> {
WebsocketServerSpec spec = buildSpec(subProtocol);
return reactorResponse.sendWebsocket((in, out) -> {
ReactorNettyWebSocketSession session =
new ReactorNettyWebSocketSession(
in, out, handshakeInfo, bufferFactory, spec.maxFramePayloadLength());
return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]");
}, spec);
}));
}
private static HttpServerResponse getNativeResponse(ServerHttpResponse response) {
if (response instanceof AbstractServerHttpResponse) {
return ((AbstractServerHttpResponse) response).getNativeResponse();
}
else if (response instanceof ServerHttpResponseDecorator) {
return getNativeResponse(((ServerHttpResponseDecorator) response).getDelegate());
}
else {
throw new IllegalArgumentException(
"Couldn't find native response in " + response.getClass().getName());
}
}
}