### Motivation: Introduction of `WebSocketDecoderConfig` made our server-side code more elegant and simpler for support. However there is still some problem with maintenance and new features development for WebSocket codecs (`WebSocketServerProtocolHandler`, `WebSocketServerProtocolHandler`). Particularly, it makes me ~~crying with blood~~ extremely sad to add new parameter and yet another one constructor into these handlers, when I want to contribute new feature. ### Modification: I've extracted all parameters for client and server WebSocket handlers into config/builder structures, like it was made for decoders in PR #9116. ### Result: * Fixes #9698: Simplify WebSocket handlers constructor arguments hell * Unblock further development in this module (configurable close frame handling on server-side; automatic close-frame sending, when missed; memory leaks on protocol violations; etc...) Bonuses: * All defaults are gathered in one place and could be easily found/reused. * New API greatly simplifies usage, but does NOT allow inheritance or modification. * New API would simplify long-term maintenance of WebSockets module. ### Example WebSocketClientProtocolConfig config = WebSocketClientProtocolConfig.newBuilder() .webSocketUri("wss://localhost:8443/fx-spot") .subprotocol("trading") .handshakeTimeoutMillis(15000L) .build(); ctx.pipeline().addLast(new WebSocketClientProtocolHandler(config));
166 lines
6.2 KiB
Java
166 lines
6.2 KiB
Java
/*
|
|
* Copyright 2019 The Netty Project
|
|
*
|
|
* The Netty Project licenses this file to you 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:
|
|
*
|
|
* http://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 io.netty.handler.codec.http.websocketx;
|
|
|
|
import io.netty.util.internal.ObjectUtil;
|
|
|
|
/**
|
|
* Frames decoder configuration.
|
|
*/
|
|
public final class WebSocketDecoderConfig {
|
|
|
|
static final WebSocketDecoderConfig DEFAULT =
|
|
new WebSocketDecoderConfig(65536, true, false, false, true, true);
|
|
|
|
private final int maxFramePayloadLength;
|
|
private final boolean expectMaskedFrames;
|
|
private final boolean allowMaskMismatch;
|
|
private final boolean allowExtensions;
|
|
private final boolean closeOnProtocolViolation;
|
|
private final boolean withUTF8Validator;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param maxFramePayloadLength
|
|
* Maximum length of a frame's payload. Setting this to an appropriate value for you application
|
|
* helps check for denial of services attacks.
|
|
* @param expectMaskedFrames
|
|
* Web socket servers must set this to true processed incoming masked payload. Client implementations
|
|
* must set this to false.
|
|
* @param allowMaskMismatch
|
|
* Allows to loosen the masking requirement on received frames. When this is set to false then also
|
|
* frames which are not masked properly according to the standard will still be accepted.
|
|
* @param allowExtensions
|
|
* Flag to allow reserved extension bits to be used or not
|
|
* @param closeOnProtocolViolation
|
|
* Flag to send close frame immediately on any protocol violation.ion.
|
|
* @param withUTF8Validator
|
|
* Allows you to avoid adding of Utf8FrameValidator to the pipeline on the
|
|
* WebSocketServerProtocolHandler creation. This is useful (less overhead)
|
|
* when you use only BinaryWebSocketFrame within your web socket connection.
|
|
*/
|
|
private WebSocketDecoderConfig(int maxFramePayloadLength, boolean expectMaskedFrames, boolean allowMaskMismatch,
|
|
boolean allowExtensions, boolean closeOnProtocolViolation,
|
|
boolean withUTF8Validator) {
|
|
this.maxFramePayloadLength = maxFramePayloadLength;
|
|
this.expectMaskedFrames = expectMaskedFrames;
|
|
this.allowMaskMismatch = allowMaskMismatch;
|
|
this.allowExtensions = allowExtensions;
|
|
this.closeOnProtocolViolation = closeOnProtocolViolation;
|
|
this.withUTF8Validator = withUTF8Validator;
|
|
}
|
|
|
|
public int maxFramePayloadLength() {
|
|
return maxFramePayloadLength;
|
|
}
|
|
|
|
public boolean expectMaskedFrames() {
|
|
return expectMaskedFrames;
|
|
}
|
|
|
|
public boolean allowMaskMismatch() {
|
|
return allowMaskMismatch;
|
|
}
|
|
|
|
public boolean allowExtensions() {
|
|
return allowExtensions;
|
|
}
|
|
|
|
public boolean closeOnProtocolViolation() {
|
|
return closeOnProtocolViolation;
|
|
}
|
|
|
|
public boolean withUTF8Validator() {
|
|
return withUTF8Validator;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "WebSocketDecoderConfig" +
|
|
" [maxFramePayloadLength=" + maxFramePayloadLength +
|
|
", expectMaskedFrames=" + expectMaskedFrames +
|
|
", allowMaskMismatch=" + allowMaskMismatch +
|
|
", allowExtensions=" + allowExtensions +
|
|
", closeOnProtocolViolation=" + closeOnProtocolViolation +
|
|
", withUTF8Validator=" + withUTF8Validator +
|
|
"]";
|
|
}
|
|
|
|
public Builder toBuilder() {
|
|
return new Builder(this);
|
|
}
|
|
|
|
public static Builder newBuilder() {
|
|
return new Builder(DEFAULT);
|
|
}
|
|
|
|
public static final class Builder {
|
|
private int maxFramePayloadLength;
|
|
private boolean expectMaskedFrames;
|
|
private boolean allowMaskMismatch;
|
|
private boolean allowExtensions;
|
|
private boolean closeOnProtocolViolation;
|
|
private boolean withUTF8Validator;
|
|
|
|
private Builder(WebSocketDecoderConfig decoderConfig) {
|
|
ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
|
|
maxFramePayloadLength = decoderConfig.maxFramePayloadLength();
|
|
expectMaskedFrames = decoderConfig.expectMaskedFrames();
|
|
allowMaskMismatch = decoderConfig.allowMaskMismatch();
|
|
allowExtensions = decoderConfig.allowExtensions();
|
|
closeOnProtocolViolation = decoderConfig.closeOnProtocolViolation();
|
|
withUTF8Validator = decoderConfig.withUTF8Validator();
|
|
}
|
|
|
|
public Builder maxFramePayloadLength(int maxFramePayloadLength) {
|
|
this.maxFramePayloadLength = maxFramePayloadLength;
|
|
return this;
|
|
}
|
|
|
|
public Builder expectMaskedFrames(boolean expectMaskedFrames) {
|
|
this.expectMaskedFrames = expectMaskedFrames;
|
|
return this;
|
|
}
|
|
|
|
public Builder allowMaskMismatch(boolean allowMaskMismatch) {
|
|
this.allowMaskMismatch = allowMaskMismatch;
|
|
return this;
|
|
}
|
|
|
|
public Builder allowExtensions(boolean allowExtensions) {
|
|
this.allowExtensions = allowExtensions;
|
|
return this;
|
|
}
|
|
|
|
public Builder closeOnProtocolViolation(boolean closeOnProtocolViolation) {
|
|
this.closeOnProtocolViolation = closeOnProtocolViolation;
|
|
return this;
|
|
}
|
|
|
|
public Builder withUTF8Validator(boolean withUTF8Validator) {
|
|
this.withUTF8Validator = withUTF8Validator;
|
|
return this;
|
|
}
|
|
|
|
public WebSocketDecoderConfig build() {
|
|
return new WebSocketDecoderConfig(
|
|
maxFramePayloadLength, expectMaskedFrames, allowMaskMismatch,
|
|
allowExtensions, closeOnProtocolViolation, withUTF8Validator);
|
|
}
|
|
}
|
|
}
|