View Javadoc
1   /*
2    * Copyright 2019 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.websocketx;
17  
18  import io.netty.util.internal.ObjectUtil;
19  
20  /**
21   * Frames decoder configuration.
22   */
23  public final class WebSocketDecoderConfig {
24  
25      static final WebSocketDecoderConfig DEFAULT =
26          new WebSocketDecoderConfig(65536, true, false, false, true, true);
27  
28      private final int maxFramePayloadLength;
29      private final boolean expectMaskedFrames;
30      private final boolean allowMaskMismatch;
31      private final boolean allowExtensions;
32      private final boolean closeOnProtocolViolation;
33      private final boolean withUTF8Validator;
34  
35      /**
36       * Constructor
37       *
38       * @param maxFramePayloadLength
39       *            Maximum length of a frame's payload. Setting this to an appropriate value for you application
40       *            helps check for denial of services attacks.
41       * @param expectMaskedFrames
42       *            Web socket servers must set this to true processed incoming masked payload. Client implementations
43       *            must set this to false.
44       * @param allowMaskMismatch
45       *            Allows to loosen the masking requirement on received frames. When this is set to false then also
46       *            frames which are not masked properly according to the standard will still be accepted.
47       * @param allowExtensions
48       *            Flag to allow reserved extension bits to be used or not
49       * @param closeOnProtocolViolation
50       *            Flag to send close frame immediately on any protocol violation.ion.
51       * @param withUTF8Validator
52       *            Allows you to avoid adding of Utf8FrameValidator to the pipeline on the
53       *            WebSocketServerProtocolHandler creation. This is useful (less overhead)
54       *            when you use only BinaryWebSocketFrame within your web socket connection.
55       */
56      private WebSocketDecoderConfig(int maxFramePayloadLength, boolean expectMaskedFrames, boolean allowMaskMismatch,
57                                    boolean allowExtensions, boolean closeOnProtocolViolation,
58                                    boolean withUTF8Validator) {
59          this.maxFramePayloadLength = maxFramePayloadLength;
60          this.expectMaskedFrames = expectMaskedFrames;
61          this.allowMaskMismatch = allowMaskMismatch;
62          this.allowExtensions = allowExtensions;
63          this.closeOnProtocolViolation = closeOnProtocolViolation;
64          this.withUTF8Validator = withUTF8Validator;
65      }
66  
67      public int maxFramePayloadLength() {
68          return maxFramePayloadLength;
69      }
70  
71      public boolean expectMaskedFrames() {
72          return expectMaskedFrames;
73      }
74  
75      public boolean allowMaskMismatch() {
76          return allowMaskMismatch;
77      }
78  
79      public boolean allowExtensions() {
80          return allowExtensions;
81      }
82  
83      public boolean closeOnProtocolViolation() {
84          return closeOnProtocolViolation;
85      }
86  
87      public boolean withUTF8Validator() {
88          return withUTF8Validator;
89      }
90  
91      @Override
92      public String toString() {
93          return "WebSocketDecoderConfig" +
94              " [maxFramePayloadLength=" + maxFramePayloadLength +
95              ", expectMaskedFrames=" + expectMaskedFrames +
96              ", allowMaskMismatch=" + allowMaskMismatch +
97              ", allowExtensions=" + allowExtensions +
98              ", closeOnProtocolViolation=" + closeOnProtocolViolation +
99              ", withUTF8Validator=" + withUTF8Validator +
100             "]";
101     }
102 
103     public Builder toBuilder() {
104         return new Builder(this);
105     }
106 
107     public static Builder newBuilder() {
108         return new Builder(DEFAULT);
109     }
110 
111     public static final class Builder {
112         private int maxFramePayloadLength;
113         private boolean expectMaskedFrames;
114         private boolean allowMaskMismatch;
115         private boolean allowExtensions;
116         private boolean closeOnProtocolViolation;
117         private boolean withUTF8Validator;
118 
119         private Builder(WebSocketDecoderConfig decoderConfig) {
120             ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
121             maxFramePayloadLength = decoderConfig.maxFramePayloadLength();
122             expectMaskedFrames = decoderConfig.expectMaskedFrames();
123             allowMaskMismatch = decoderConfig.allowMaskMismatch();
124             allowExtensions = decoderConfig.allowExtensions();
125             closeOnProtocolViolation = decoderConfig.closeOnProtocolViolation();
126             withUTF8Validator = decoderConfig.withUTF8Validator();
127         }
128 
129         public Builder maxFramePayloadLength(int maxFramePayloadLength) {
130             this.maxFramePayloadLength = maxFramePayloadLength;
131             return this;
132         }
133 
134         public Builder expectMaskedFrames(boolean expectMaskedFrames) {
135             this.expectMaskedFrames = expectMaskedFrames;
136             return this;
137         }
138 
139         public Builder allowMaskMismatch(boolean allowMaskMismatch) {
140             this.allowMaskMismatch = allowMaskMismatch;
141             return this;
142         }
143 
144         public Builder allowExtensions(boolean allowExtensions) {
145             this.allowExtensions = allowExtensions;
146             return this;
147         }
148 
149         public Builder closeOnProtocolViolation(boolean closeOnProtocolViolation) {
150             this.closeOnProtocolViolation = closeOnProtocolViolation;
151             return this;
152         }
153 
154         public Builder withUTF8Validator(boolean withUTF8Validator) {
155             this.withUTF8Validator = withUTF8Validator;
156             return this;
157         }
158 
159         public WebSocketDecoderConfig build() {
160             return new WebSocketDecoderConfig(
161                     maxFramePayloadLength, expectMaskedFrames, allowMaskMismatch,
162                     allowExtensions, closeOnProtocolViolation, withUTF8Validator);
163         }
164     }
165 }