View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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.HttpHeaders;
22  import io.netty.handler.codec.http.HttpHeaders.Names;
23  import io.netty.handler.codec.http.HttpResponseStatus;
24  import io.netty.util.CharsetUtil;
25  
26  import static io.netty.handler.codec.http.HttpHeaders.Values.*;
27  import static io.netty.handler.codec.http.HttpVersion.*;
28  
29  /**
30   * <p>
31   * Performs server side opening and closing handshakes for web socket specification version <a
32   * href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" >draft-ietf-hybi-thewebsocketprotocol-
33   * 10</a>
34   * </p>
35   */
36  public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
37  
38      public static final String WEBSOCKET_08_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
39  
40      private final boolean allowExtensions;
41  
42      /**
43       * Constructor specifying the destination web socket location
44       *
45       * @param webSocketURL
46       *            URL for web socket communications. e.g "ws://myhost.com/mypath".
47       *            Subsequent web socket frames will be sent to this URL.
48       * @param subprotocols
49       *            CSV of supported protocols
50       * @param allowExtensions
51       *            Allow extensions to be used in the reserved bits of the web socket frame
52       * @param maxFramePayloadLength
53       *            Maximum allowable frame payload length. Setting this value to your application's
54       *            requirement may reduce denial of service attacks using long data frames.
55       */
56      public WebSocketServerHandshaker08(
57              String webSocketURL, String subprotocols, boolean allowExtensions, int maxFramePayloadLength) {
58          super(WebSocketVersion.V08, webSocketURL, subprotocols, maxFramePayloadLength);
59          this.allowExtensions = allowExtensions;
60      }
61  
62      /**
63       * <p>
64       * Handle the web socket handshake for the web socket specification <a href=
65       * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi version 8 to 10</a>. Version 8, 9 and
66       * 10 share the same wire protocol.
67       * </p>
68       *
69       * <p>
70       * Browser request to the server:
71       * </p>
72       *
73       * <pre>
74       * GET /chat HTTP/1.1
75       * Host: server.example.com
76       * Upgrade: websocket
77       * Connection: Upgrade
78       * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
79       * Sec-WebSocket-Origin: http://example.com
80       * Sec-WebSocket-Protocol: chat, superchat
81       * Sec-WebSocket-Version: 8
82       * </pre>
83       *
84       * <p>
85       * Server response:
86       * </p>
87       *
88       * <pre>
89       * HTTP/1.1 101 Switching Protocols
90       * Upgrade: websocket
91       * Connection: Upgrade
92       * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
93       * Sec-WebSocket-Protocol: chat
94       * </pre>
95       */
96      @Override
97      protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) {
98          FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS);
99  
100         if (headers != null) {
101             res.headers().add(headers);
102         }
103 
104         String key = req.headers().get(Names.SEC_WEBSOCKET_KEY);
105         if (key == null) {
106             throw new WebSocketHandshakeException("not a WebSocket request: missing key");
107         }
108         String acceptSeed = key + WEBSOCKET_08_ACCEPT_GUID;
109         byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII));
110         String accept = WebSocketUtil.base64(sha1);
111 
112         if (logger.isDebugEnabled()) {
113             logger.debug("WebSocket version 08 server handshake key: {}, response: {}", key, accept);
114         }
115 
116         res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase());
117         res.headers().add(Names.CONNECTION, Names.UPGRADE);
118         res.headers().add(Names.SEC_WEBSOCKET_ACCEPT, accept);
119         String subprotocols = req.headers().get(Names.SEC_WEBSOCKET_PROTOCOL);
120         if (subprotocols != null) {
121             String selectedSubprotocol = selectSubprotocol(subprotocols);
122             if (selectedSubprotocol == null) {
123                 if (logger.isDebugEnabled()) {
124                     logger.debug("Requested subprotocol(s) not supported: {}", subprotocols);
125                 }
126             } else {
127                 res.headers().add(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
128             }
129         }
130         return res;
131     }
132 
133     @Override
134     protected WebSocketFrameDecoder newWebsocketDecoder() {
135         return new WebSocket08FrameDecoder(true, allowExtensions, maxFramePayloadLength());
136     }
137 
138     @Override
139     protected WebSocketFrameEncoder newWebSocketEncoder() {
140         return new WebSocket08FrameEncoder(false);
141     }
142 }