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.HttpHeaders;
22 import io.netty.handler.codec.http.HttpHeaders.Names;
23 import io.netty.handler.codec.http.DefaultFullHttpResponse;
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
29
30
31
32
33 public class WebSocketServerHandshakerFactory {
34
35 private final String webSocketURL;
36
37 private final String subprotocols;
38
39 private final boolean allowExtensions;
40
41 private final int maxFramePayloadLength;
42
43
44
45
46
47
48
49
50
51
52
53
54 public WebSocketServerHandshakerFactory(
55 String webSocketURL, String subprotocols, boolean allowExtensions) {
56 this(webSocketURL, subprotocols, allowExtensions, 65536);
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public WebSocketServerHandshakerFactory(
74 String webSocketURL, String subprotocols, boolean allowExtensions,
75 int maxFramePayloadLength) {
76 this.webSocketURL = webSocketURL;
77 this.subprotocols = subprotocols;
78 this.allowExtensions = allowExtensions;
79 this.maxFramePayloadLength = maxFramePayloadLength;
80 }
81
82
83
84
85
86
87
88 public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
89
90 String version = req.headers().get(Names.SEC_WEBSOCKET_VERSION);
91 if (version != null) {
92 if (version.equals(WebSocketVersion.V13.toHttpHeaderValue())) {
93
94 return new WebSocketServerHandshaker13(
95 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
96 } else if (version.equals(WebSocketVersion.V08.toHttpHeaderValue())) {
97
98 return new WebSocketServerHandshaker08(
99 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
100 } else if (version.equals(WebSocketVersion.V07.toHttpHeaderValue())) {
101
102 return new WebSocketServerHandshaker07(
103 webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength);
104 } else {
105 return null;
106 }
107 } else {
108
109 return new WebSocketServerHandshaker00(webSocketURL, subprotocols, maxFramePayloadLength);
110 }
111 }
112
113
114
115
116 @Deprecated
117 public static void sendUnsupportedWebSocketVersionResponse(Channel channel) {
118 sendUnsupportedVersionResponse(channel);
119 }
120
121
122
123
124 public static ChannelFuture sendUnsupportedVersionResponse(Channel channel) {
125 return sendUnsupportedVersionResponse(channel, channel.newPromise());
126 }
127
128
129
130
131 public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) {
132 HttpResponse res = new DefaultFullHttpResponse(
133 HttpVersion.HTTP_1_1,
134 HttpResponseStatus.UPGRADE_REQUIRED);
135 res.headers().set(Names.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue());
136 HttpHeaders.setContentLength(res, 0);
137 return channel.writeAndFlush(res, promise);
138 }
139 }