|
27 | 27 | import org.springframework.lang.Nullable;
|
28 | 28 | import org.springframework.web.reactive.socket.HandshakeInfo;
|
29 | 29 | import org.springframework.web.reactive.socket.WebSocketHandler;
|
| 30 | +import org.springframework.web.reactive.socket.adapter.NettyWebSocketSessionSupport; |
30 | 31 | import org.springframework.web.reactive.socket.adapter.ReactorNettyWebSocketSession;
|
31 | 32 | import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
|
32 | 33 | import org.springframework.web.server.ServerWebExchange;
|
|
39 | 40 | */
|
40 | 41 | public class ReactorNettyRequestUpgradeStrategy implements RequestUpgradeStrategy {
|
41 | 42 |
|
| 43 | + private int maxFramePayloadLength = NettyWebSocketSessionSupport.DEFAULT_FRAME_MAX_SIZE; |
| 44 | + |
| 45 | + |
| 46 | + /** |
| 47 | + * Configure the maximum allowable frame payload length. Setting this value |
| 48 | + * to your application's requirement may reduce denial of service attacks |
| 49 | + * using long data frames. |
| 50 | + * <p>Corresponds to the argument with the same name in the constructor of |
| 51 | + * {@link io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory |
| 52 | + * WebSocketServerHandshakerFactory} in Netty. |
| 53 | + * <p>By default set to 65536 (64K). |
| 54 | + * @param maxFramePayloadLength the max length for frames. |
| 55 | + * @since 5.1 |
| 56 | + */ |
| 57 | + public void setMaxFramePayloadLength(Integer maxFramePayloadLength) { |
| 58 | + this.maxFramePayloadLength = maxFramePayloadLength; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Return the configured max length for frames. |
| 63 | + * @since 5.1 |
| 64 | + */ |
| 65 | + public int getMaxFramePayloadLength() { |
| 66 | + return this.maxFramePayloadLength; |
| 67 | + } |
| 68 | + |
42 | 69 |
|
43 | 70 | @Override
|
44 | 71 | public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler,
|
45 | 72 | @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
|
46 | 73 |
|
47 | 74 | ServerHttpResponse response = exchange.getResponse();
|
48 |
| - HttpServerResponse nativeResponse = ((AbstractServerHttpResponse) response).getNativeResponse(); |
| 75 | + HttpServerResponse reactorResponse = ((AbstractServerHttpResponse) response).getNativeResponse(); |
49 | 76 | HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
|
50 | 77 | NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
|
51 | 78 |
|
52 |
| - return nativeResponse.sendWebsocket(subProtocol, |
53 |
| - (in, out) -> handler.handle(new ReactorNettyWebSocketSession(in, out, handshakeInfo, bufferFactory))); |
| 79 | + return reactorResponse.sendWebsocket(subProtocol, this.maxFramePayloadLength, |
| 80 | + (in, out) -> { |
| 81 | + ReactorNettyWebSocketSession session = |
| 82 | + new ReactorNettyWebSocketSession( |
| 83 | + in, out, handshakeInfo, bufferFactory, this.maxFramePayloadLength); |
| 84 | + return handler.handle(session); |
| 85 | + }); |
54 | 86 | }
|
55 | 87 |
|
56 | 88 | }
|
0 commit comments