1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http.websocketx;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelPromise;
21 import io.netty.handler.codec.http.DefaultFullHttpResponse;
22 import io.netty.handler.codec.http.HttpHeaderNames;
23 import io.netty.handler.codec.http.HttpUtil;
24 import io.netty.handler.codec.http.HttpRequest;
25 import io.netty.handler.codec.http.HttpResponse;
26 import io.netty.handler.codec.http.HttpResponseStatus;
27 import io.netty.handler.codec.http.HttpVersion;
28 import io.netty.util.internal.ObjectUtil;
29
30
31
32
33
34 public class WebSocketServerHandshakerFactory {
35
36 private final String webSocketURL;
37
38 private final String subprotocols;
39
40 private final WebSocketDecoderConfig decoderConfig;
41
42
43
44
45
46
47
48
49
50
51
52
53 public WebSocketServerHandshakerFactory(
54 String webSocketURL, String subprotocols, boolean allowExtensions) {
55 this(webSocketURL, subprotocols, allowExtensions, 65536);
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public WebSocketServerHandshakerFactory(
73 String webSocketURL, String subprotocols, boolean allowExtensions,
74 int maxFramePayloadLength) {
75 this(webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, false);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public WebSocketServerHandshakerFactory(
96 String webSocketURL, String subprotocols, boolean allowExtensions,
97 int maxFramePayloadLength, boolean allowMaskMismatch) {
98 this(webSocketURL, subprotocols, WebSocketDecoderConfig.newBuilder()
99 .allowExtensions(allowExtensions)
100 .maxFramePayloadLength(maxFramePayloadLength)
101 .allowMaskMismatch(allowMaskMismatch)
102 .build());
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116 public WebSocketServerHandshakerFactory(
117 String webSocketURL, String subprotocols, WebSocketDecoderConfig decoderConfig) {
118 this.webSocketURL = webSocketURL;
119 this.subprotocols = subprotocols;
120 this.decoderConfig = ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
121 }
122
123
124
125
126
127
128
129 public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
130 return resolveHandshaker0(req, webSocketURL, subprotocols, decoderConfig);
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public static WebSocketServerHandshaker resolveHandshaker(HttpRequest req, String webSocketURL, String subprotocols,
153 WebSocketDecoderConfig decoderConfig) {
154 ObjectUtil.checkNotNull(decoderConfig, "decoderConfig");
155 return resolveHandshaker0(req, webSocketURL, subprotocols, decoderConfig);
156 }
157
158 private static WebSocketServerHandshaker resolveHandshaker0(HttpRequest req,
159 String webSocketURL,
160 String subprotocols,
161 WebSocketDecoderConfig decoderConfig) {
162 CharSequence version = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION);
163 if (version != null) {
164 if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
165
166 return new WebSocketServerHandshaker13(
167 webSocketURL, subprotocols, decoderConfig);
168 } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
169
170 return new WebSocketServerHandshaker08(
171 webSocketURL, subprotocols, decoderConfig);
172 } else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
173
174 return new WebSocketServerHandshaker07(
175 webSocketURL, subprotocols, decoderConfig);
176 } else {
177 return null;
178 }
179 } else {
180
181 return new WebSocketServerHandshaker00(webSocketURL, subprotocols, decoderConfig);
182 }
183 }
184
185
186
187
188 @Deprecated
189 public static void sendUnsupportedWebSocketVersionResponse(Channel channel) {
190 sendUnsupportedVersionResponse(channel);
191 }
192
193
194
195
196 public static ChannelFuture sendUnsupportedVersionResponse(Channel channel) {
197 return sendUnsupportedVersionResponse(channel, channel.newPromise());
198 }
199
200
201
202
203 public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) {
204 HttpResponse res = new DefaultFullHttpResponse(
205 HttpVersion.HTTP_1_1,
206 HttpResponseStatus.UPGRADE_REQUIRED, channel.alloc().buffer(0));
207 res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
208 HttpUtil.setContentLength(res, 0);
209 return channel.writeAndFlush(res, promise);
210 }
211 }