xref: /unit/src/java/nginx/unit/websocket/Constants.java (revision 1157:7ae152bda303)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package nginx.unit.websocket;
18 
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 
23 import javax.websocket.Extension;
24 
25 /**
26  * Internal implementation constants.
27  */
28 public class Constants {
29 
30     // OP Codes
31     public static final byte OPCODE_CONTINUATION = 0x00;
32     public static final byte OPCODE_TEXT = 0x01;
33     public static final byte OPCODE_BINARY = 0x02;
34     public static final byte OPCODE_CLOSE = 0x08;
35     public static final byte OPCODE_PING = 0x09;
36     public static final byte OPCODE_PONG = 0x0A;
37 
38     // Internal OP Codes
39     // RFC 6455 limits OP Codes to 4 bits so these should never clash
40     // Always set bit 4 so these will be treated as control codes
41     static final byte INTERNAL_OPCODE_FLUSH = 0x18;
42 
43     // Buffers
44     static final int DEFAULT_BUFFER_SIZE = Integer.getInteger(
45             "nginx.unit.websocket.DEFAULT_BUFFER_SIZE", 8 * 1024)
46             .intValue();
47 
48     // Client connection
49     /**
50      * Property name to set to configure the value that is passed to
51      * {@link javax.net.ssl.SSLEngine#setEnabledProtocols(String[])}. The value
52      * should be a comma separated string.
53      */
54     public static final String SSL_PROTOCOLS_PROPERTY =
55             "nginx.unit.websocket.SSL_PROTOCOLS";
56     public static final String SSL_TRUSTSTORE_PROPERTY =
57             "nginx.unit.websocket.SSL_TRUSTSTORE";
58     public static final String SSL_TRUSTSTORE_PWD_PROPERTY =
59             "nginx.unit.websocket.SSL_TRUSTSTORE_PWD";
60     public static final String SSL_TRUSTSTORE_PWD_DEFAULT = "changeit";
61     /**
62      * Property name to set to configure used SSLContext. The value should be an
63      * instance of SSLContext. If this property is present, the SSL_TRUSTSTORE*
64      * properties are ignored.
65      */
66     public static final String SSL_CONTEXT_PROPERTY =
67             "nginx.unit.websocket.SSL_CONTEXT";
68     /**
69      * Property name to set to configure the timeout (in milliseconds) when
70      * establishing a WebSocket connection to server. The default is
71      * {@link #IO_TIMEOUT_MS_DEFAULT}.
72      */
73     public static final String IO_TIMEOUT_MS_PROPERTY =
74             "nginx.unit.websocket.IO_TIMEOUT_MS";
75     public static final long IO_TIMEOUT_MS_DEFAULT = 5000;
76 
77     // RFC 2068 recommended a limit of 5
78     // Most browsers have a default limit of 20
79     public static final String MAX_REDIRECTIONS_PROPERTY =
80             "nginx.unit.websocket.MAX_REDIRECTIONS";
81     public static final int MAX_REDIRECTIONS_DEFAULT = 20;
82 
83     // HTTP upgrade header names and values
84     public static final String HOST_HEADER_NAME = "Host";
85     public static final String UPGRADE_HEADER_NAME = "Upgrade";
86     public static final String UPGRADE_HEADER_VALUE = "websocket";
87     public static final String ORIGIN_HEADER_NAME = "Origin";
88     public static final String CONNECTION_HEADER_NAME = "Connection";
89     public static final String CONNECTION_HEADER_VALUE = "upgrade";
90     public static final String LOCATION_HEADER_NAME = "Location";
91     public static final String AUTHORIZATION_HEADER_NAME = "Authorization";
92     public static final String WWW_AUTHENTICATE_HEADER_NAME = "WWW-Authenticate";
93     public static final String WS_VERSION_HEADER_NAME = "Sec-WebSocket-Version";
94     public static final String WS_VERSION_HEADER_VALUE = "13";
95     public static final String WS_KEY_HEADER_NAME = "Sec-WebSocket-Key";
96     public static final String WS_PROTOCOL_HEADER_NAME = "Sec-WebSocket-Protocol";
97     public static final String WS_EXTENSIONS_HEADER_NAME = "Sec-WebSocket-Extensions";
98 
99     /// HTTP redirection status codes
100     public static final int MULTIPLE_CHOICES = 300;
101     public static final int MOVED_PERMANENTLY = 301;
102     public static final int FOUND = 302;
103     public static final int SEE_OTHER = 303;
104     public static final int USE_PROXY = 305;
105     public static final int TEMPORARY_REDIRECT = 307;
106 
107     // Configuration for Origin header in client
108     static final String DEFAULT_ORIGIN_HEADER_VALUE =
109             System.getProperty("nginx.unit.websocket.DEFAULT_ORIGIN_HEADER_VALUE");
110 
111     // Configuration for blocking sends
112     public static final String BLOCKING_SEND_TIMEOUT_PROPERTY =
113             "nginx.unit.websocket.BLOCKING_SEND_TIMEOUT";
114     // Milliseconds so this is 20 seconds
115     public static final long DEFAULT_BLOCKING_SEND_TIMEOUT = 20 * 1000;
116 
117     // Configuration for background processing checks intervals
118     static final int DEFAULT_PROCESS_PERIOD = Integer.getInteger(
119             "nginx.unit.websocket.DEFAULT_PROCESS_PERIOD", 10)
120             .intValue();
121 
122     public static final String WS_AUTHENTICATION_USER_NAME = "nginx.unit.websocket.WS_AUTHENTICATION_USER_NAME";
123     public static final String WS_AUTHENTICATION_PASSWORD = "nginx.unit.websocket.WS_AUTHENTICATION_PASSWORD";
124 
125     /* Configuration for extensions
126      * Note: These options are primarily present to enable this implementation
127      *       to pass compliance tests. They are expected to be removed once
128      *       the WebSocket API includes a mechanism for adding custom extensions
129      *       and disabling built-in extensions.
130      */
131     static final boolean DISABLE_BUILTIN_EXTENSIONS =
132             Boolean.getBoolean("nginx.unit.websocket.DISABLE_BUILTIN_EXTENSIONS");
133     static final boolean ALLOW_UNSUPPORTED_EXTENSIONS =
134             Boolean.getBoolean("nginx.unit.websocket.ALLOW_UNSUPPORTED_EXTENSIONS");
135 
136     // Configuration for stream behavior
137     static final boolean STREAMS_DROP_EMPTY_MESSAGES =
138             Boolean.getBoolean("nginx.unit.websocket.STREAMS_DROP_EMPTY_MESSAGES");
139 
140     public static final boolean STRICT_SPEC_COMPLIANCE =
141             Boolean.getBoolean("nginx.unit.websocket.STRICT_SPEC_COMPLIANCE");
142 
143     public static final List<Extension> INSTALLED_EXTENSIONS;
144 
145     static {
146         if (DISABLE_BUILTIN_EXTENSIONS) {
147             INSTALLED_EXTENSIONS = Collections.unmodifiableList(new ArrayList<Extension>());
148         } else {
149             List<Extension> installed = new ArrayList<>(1);
installed.add(new WsExtension(R))150             installed.add(new WsExtension("permessage-deflate"));
151             INSTALLED_EXTENSIONS = Collections.unmodifiableList(installed);
152         }
153     }
154 
Constants()155     private Constants() {
156         // Hide default constructor
157     }
158 }
159