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.handler.codec.http.DefaultFullHttpResponse;
19 import io.netty.handler.codec.http.FullHttpRequest;
20 import io.netty.handler.codec.http.FullHttpResponse;
21 import io.netty.handler.codec.http.HttpHeaderNames;
22 import io.netty.handler.codec.http.HttpHeaderValues;
23 import io.netty.handler.codec.http.HttpHeaders;
24 import io.netty.handler.codec.http.HttpResponseStatus;
25 import io.netty.util.CharsetUtil;
26
27 import static io.netty.handler.codec.http.HttpVersion.*;
28
29
30
31
32
33
34
35
36 public class WebSocketServerHandshaker07 extends WebSocketServerHandshaker {
37
38 public static final String WEBSOCKET_07_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
39
40 private final boolean allowExtensions;
41 private final boolean allowMaskMismatch;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public WebSocketServerHandshaker07(
58 String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength) {
59 this(webSocketURL, subprotocols, allowExtensions, maxFramePayloadLength, false);
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public WebSocketServerHandshaker07(
80 String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength,
81 boolean allowMaskMismatch) {
82 super(WebSocketVersion.V07, webSocketURL, subprotocols, maxFramePayloadLength);
83 this.allowExtensions = allowExtensions;
84 this.allowMaskMismatch = allowMaskMismatch;
85 }
86
87
88
89
90
91
92
93
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 @Override
121 protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
122
123 FullHttpResponse res =
124 new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
125
126 if (headers != null) {
127 res.headers().add(headers);
128 }
129
130 CharSequence key = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_KEY);
131 if (key == null) {
132 throw new WebSocketHandshakeException("not a WebSocket request: missing key");
133 }
134 String acceptSeed = key + WEBSOCKET_07_ACCEPT_GUID;
135 byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
136 String accept = WebSocketUtil.base64(sha1);
137
138 if (logger.isDebugEnabled()) {
139 logger.debug("WebSocket version 07 server handshake key: {}, response: {}.", key, accept);
140 }
141
142 res.headers().add(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
143 res.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE);
144 res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT, accept);
145
146 String subprotocols = req.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL);
147 if (subprotocols != null) {
148 String selectedSubprotocol = selectSubprotocol(subprotocols);
149 if (selectedSubprotocol == null) {
150 if (logger.isDebugEnabled()) {
151 logger.debug("Requested subprotocol(s) not supported: {}", subprotocols);
152 }
153 } else {
154 res.headers().add(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
155 }
156 }
157 return res;
158 }
159
160 @Override
161 protected WebSocketFrameDecoder newWebsocketDecoder() {
162 return new WebSocket07FrameDecoder(true, allowExtensions, maxFramePayloadLength(), allowMaskMismatch);
163 }
164
165 @Override
166 protected WebSocketFrameEncoder newWebSocketEncoder() {
167 return new WebSocket07FrameEncoder(false);
168 }
169 }