1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.websocketx;
17
18 import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.*;
19 import static org.jboss.netty.handler.codec.http.HttpVersion.*;
20
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.jboss.netty.buffer.ChannelBuffers;
23 import org.jboss.netty.channel.Channel;
24 import org.jboss.netty.channel.ChannelFuture;
25 import org.jboss.netty.channel.ChannelFutureListener;
26 import org.jboss.netty.channel.ChannelPipeline;
27 import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
28 import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
29 import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
30 import org.jboss.netty.handler.codec.http.HttpRequest;
31 import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
32 import org.jboss.netty.handler.codec.http.HttpResponse;
33 import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
34 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
35 import org.jboss.netty.logging.InternalLogger;
36 import org.jboss.netty.logging.InternalLoggerFactory;
37 import org.jboss.netty.util.CharsetUtil;
38
39
40
41
42
43
44
45
46
47
48 public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
49
50 private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandshaker13.class);
51
52 public static final String WEBSOCKET_13_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
53
54 private final boolean allowExtensions;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public WebSocketServerHandshaker13(String webSocketURL, String subprotocols, boolean allowExtensions) {
70 this(webSocketURL, subprotocols, allowExtensions, Long.MAX_VALUE);
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public WebSocketServerHandshaker13(String webSocketURL, String subprotocols, boolean allowExtensions,
91 long maxFramePayloadLength) {
92 super(WebSocketVersion.V13, webSocketURL, subprotocols, maxFramePayloadLength);
93 this.allowExtensions = allowExtensions;
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 @Override
136 public ChannelFuture handshake(Channel channel, HttpRequest req) {
137
138 if (logger.isDebugEnabled()) {
139 logger.debug(String.format("Channel %s WS Version 13 server handshake", channel.getId()));
140 }
141
142 HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
143
144 String key = req.getHeader(Names.SEC_WEBSOCKET_KEY);
145 if (key == null) {
146 throw new WebSocketHandshakeException("not a WebSocket request: missing key");
147 }
148 String acceptSeed = key + WEBSOCKET_13_ACCEPT_GUID;
149 ChannelBuffer sha1 = WebSocketUtil.sha1(ChannelBuffers.copiedBuffer(acceptSeed, CharsetUtil.US_ASCII));
150 String accept = WebSocketUtil.base64(sha1);
151
152 if (logger.isDebugEnabled()) {
153 logger.debug(String.format("WS Version 13 Server Handshake key: %s. Response: %s.", key, accept));
154 }
155
156 res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS);
157 res.addHeader(Names.UPGRADE, WEBSOCKET.toLowerCase());
158 res.addHeader(Names.CONNECTION, Names.UPGRADE);
159 res.addHeader(Names.SEC_WEBSOCKET_ACCEPT, accept);
160 String subprotocols = req.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
161 if (subprotocols != null) {
162 String selectedSubprotocol = selectSubprotocol(subprotocols);
163 if (selectedSubprotocol == null) {
164 throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
165 } else {
166 res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
167 setSelectedSubprotocol(selectedSubprotocol);
168 }
169 }
170
171 ChannelFuture future = channel.write(res);
172
173
174 future.addListener(new ChannelFutureListener() {
175 public void operationComplete(ChannelFuture future) {
176 ChannelPipeline p = future.getChannel().getPipeline();
177 if (p.get(HttpChunkAggregator.class) != null) {
178 p.remove(HttpChunkAggregator.class);
179 }
180
181 p.replace(HttpRequestDecoder.class, "wsdecoder",
182 new WebSocket13FrameDecoder(true, allowExtensions, getMaxFramePayloadLength()));
183 p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false));
184 }
185 });
186
187 return future;
188 }
189
190
191
192
193
194
195
196
197
198 @Override
199 public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) {
200 ChannelFuture f = channel.write(frame);
201 f.addListener(ChannelFutureListener.CLOSE);
202 return f;
203 }
204
205 }