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 org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.jboss.netty.channel.Channel;
21 import org.jboss.netty.channel.ChannelFuture;
22 import org.jboss.netty.channel.ChannelFutureListener;
23 import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
24 import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
25 import org.jboss.netty.handler.codec.http.HttpRequest;
26 import org.jboss.netty.handler.codec.http.HttpResponse;
27 import org.jboss.netty.handler.codec.http.HttpResponseStatus;
28 import org.jboss.netty.logging.InternalLogger;
29 import org.jboss.netty.logging.InternalLoggerFactory;
30 import org.jboss.netty.util.CharsetUtil;
31
32 import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.*;
33 import static org.jboss.netty.handler.codec.http.HttpVersion.*;
34
35
36
37
38
39
40
41
42 public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
43
44 private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandshaker07.class);
45
46 public static final String WEBSOCKET_07_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
47
48 private final boolean allowExtensions;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public WebSocketServerHandshaker07(
65 String webSocketURL, String subprotocols, boolean allowExtensions, long maxFramePayloadLength) {
66 super(WebSocketVersion.V07, webSocketURL, subprotocols, maxFramePayloadLength);
67 this.allowExtensions = allowExtensions;
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 @Override
109 public ChannelFuture handshake(Channel channel, HttpRequest req) {
110
111 if (logger.isDebugEnabled()) {
112 logger.debug(String.format("Channel %s WS Version 7 server handshake", channel.getId()));
113 }
114 HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
115
116 String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
117 if (key == null) {
118 throw new WebSocketHandshakeException("not a WebSocket request: missing key");
119 }
120 String acceptSeed = key + WEBSOCKET_07_ACCEPT_GUID;
121 ChannelBuffer sha1 = WebSocketUtil.sha1(ChannelBuffers.copiedBuffer(acceptSeed, CharsetUtil.US_ASCII));
122 String accept = WebSocketUtil.base64(sha1);
123
124 if (logger.isDebugEnabled()) {
125 logger.debug(String.format("WS Version 7 Server Handshake key: %s. Response: %s.", key, accept));
126 }
127
128 res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS);
129 res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase());
130 res.headers().add(Names.CONNECTION, Names.UPGRADE);
131 res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept);
132 String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL);
133 if (subprotocols != null) {
134 String selectedSubprotocol = selectSubprotocol(subprotocols);
135 if (selectedSubprotocol == null) {
136 throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
137 } else {
138 res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
139 setSelectedSubprotocol(selectedSubprotocol);
140 }
141 }
142
143 return writeHandshakeResponse(
144 channel, res, new WebSocket07FrameEncoder(false),
145 new WebSocket07FrameDecoder(true, allowExtensions, getMaxFramePayloadLength()));
146 }
147
148
149
150
151
152
153
154
155
156 @Override
157 public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) {
158 ChannelFuture future = channel.write(frame);
159 future.addListener(ChannelFutureListener.CLOSE);
160 return future;
161 }
162 }