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.buffer.Unpooled;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelFutureListener;
21  import io.netty.channel.ChannelHandler;
22  import io.netty.channel.ChannelHandlerContext;
23  import io.netty.channel.ChannelInboundHandler;
24  import io.netty.channel.ChannelInboundHandlerAdapter;
25  import io.netty.channel.ChannelPipeline;
26  import io.netty.handler.codec.http.DefaultFullHttpResponse;
27  import io.netty.handler.codec.http.FullHttpRequest;
28  import io.netty.handler.codec.http.FullHttpResponse;
29  import io.netty.handler.codec.http.HttpHeaders;
30  import io.netty.handler.codec.http.HttpResponseStatus;
31  import io.netty.util.AttributeKey;
32  
33  import java.util.List;
34  
35  import static io.netty.handler.codec.http.HttpVersion.*;
36  
37  /**
38   * This handler does all the heavy lifting for you to run a websocket server.
39   *
40   * It takes care of websocket handshaking as well as processing of control frames (Close, Ping, Pong). Text and Binary
41   * data frames are passed to the next handler in the pipeline (implemented by you) for processing.
42   *
43   * See <tt>io.netty.example.http.websocketx.html5.WebSocketServer</tt> for usage.
44   *
45   * The implementation of this handler assumes that you just want to run  a websocket server and not process other types
46   * HTTP requests (like GET and POST). If you wish to support both HTTP requests and websockets in the one server, refer
47   * to the <tt>io.netty.example.http.websocketx.server.WebSocketServer</tt> example.
48   *
49   * To know once a handshake was done you can intercept the
50   * {@link ChannelInboundHandler#userEventTriggered(ChannelHandlerContext, Object)} and check if the event was instance
51   * of {@link HandshakeComplete}, the event will contain extra information about the handshake such as the request and
52   * selected subprotocol.
53   */
54  public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
55  
56      /**
57       * Events that are fired to notify about handshake status
58       */
59      public enum ServerHandshakeStateEvent {
60          /**
61           * The Handshake was completed successfully and the channel was upgraded to websockets.
62           *
63           * @deprecated in favor of {@link HandshakeComplete} class,
64           * it provides extra information about the handshake
65           */
66          @Deprecated
67          HANDSHAKE_COMPLETE
68      }
69  
70      /**
71       * The Handshake was completed successfully and the channel was upgraded to websockets.
72       */
73      public static final class HandshakeComplete {
74          private final String requestUri;
75          private final HttpHeaders requestHeaders;
76          private final String selectedSubprotocol;
77  
78          HandshakeComplete(String requestUri, HttpHeaders requestHeaders, String selectedSubprotocol) {
79              this.requestUri = requestUri;
80              this.requestHeaders = requestHeaders;
81              this.selectedSubprotocol = selectedSubprotocol;
82          }
83  
84          public String requestUri() {
85              return requestUri;
86          }
87  
88          public HttpHeaders requestHeaders() {
89              return requestHeaders;
90          }
91  
92          public String selectedSubprotocol() {
93              return selectedSubprotocol;
94          }
95      }
96  
97      private static final AttributeKey<WebSocketServerHandshaker> HANDSHAKER_ATTR_KEY =
98              AttributeKey.valueOf(WebSocketServerHandshaker.class, "HANDSHAKER");
99  
100     private final String websocketPath;
101     private final String subprotocols;
102     private final boolean allowExtensions;
103     private final int maxFramePayloadLength;
104     private final boolean allowMaskMismatch;
105     private final boolean checkStartsWith;
106 
107     public WebSocketServerProtocolHandler(String websocketPath) {
108         this(websocketPath, null, false);
109     }
110 
111     public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith) {
112         this(websocketPath, null, false, 65536, false, checkStartsWith);
113     }
114 
115     public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) {
116         this(websocketPath, subprotocols, false);
117     }
118 
119     public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) {
120         this(websocketPath, subprotocols, allowExtensions, 65536);
121     }
122 
123     public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
124                                           boolean allowExtensions, int maxFrameSize) {
125         this(websocketPath, subprotocols, allowExtensions, maxFrameSize, false);
126     }
127 
128     public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
129             boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
130         this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, false);
131     }
132 
133     public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
134             boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) {
135         this.websocketPath = websocketPath;
136         this.subprotocols = subprotocols;
137         this.allowExtensions = allowExtensions;
138         maxFramePayloadLength = maxFrameSize;
139         this.allowMaskMismatch = allowMaskMismatch;
140         this.checkStartsWith = checkStartsWith;
141     }
142 
143     @Override
144     public void handlerAdded(ChannelHandlerContext ctx) {
145         ChannelPipeline cp = ctx.pipeline();
146         if (cp.get(WebSocketServerProtocolHandshakeHandler.class) == null) {
147             // Add the WebSocketHandshakeHandler before this one.
148             ctx.pipeline().addBefore(ctx.name(), WebSocketServerProtocolHandshakeHandler.class.getName(),
149                     new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols,
150                             allowExtensions, maxFramePayloadLength, allowMaskMismatch, checkStartsWith));
151         }
152         if (cp.get(Utf8FrameValidator.class) == null) {
153             // Add the UFT8 checking before this one.
154             ctx.pipeline().addBefore(ctx.name(), Utf8FrameValidator.class.getName(),
155                     new Utf8FrameValidator());
156         }
157     }
158 
159     @Override
160     protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
161         if (frame instanceof CloseWebSocketFrame) {
162             WebSocketServerHandshaker handshaker = getHandshaker(ctx.channel());
163             if (handshaker != null) {
164                 frame.retain();
165                 handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
166             } else {
167                 ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
168             }
169             return;
170         }
171         super.decode(ctx, frame, out);
172     }
173 
174     @Override
175     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
176         if (cause instanceof WebSocketHandshakeException) {
177             FullHttpResponse response = new DefaultFullHttpResponse(
178                     HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes()));
179             ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
180         } else {
181             ctx.fireExceptionCaught(cause);
182             ctx.close();
183         }
184     }
185 
186     static WebSocketServerHandshaker getHandshaker(Channel channel) {
187         return channel.attr(HANDSHAKER_ATTR_KEY).get();
188     }
189 
190     static void setHandshaker(Channel channel, WebSocketServerHandshaker handshaker) {
191         channel.attr(HANDSHAKER_ATTR_KEY).set(handshaker);
192     }
193 
194     static ChannelHandler forbiddenHttpRequestResponder() {
195         return new ChannelInboundHandlerAdapter() {
196             @Override
197             public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
198                 if (msg instanceof FullHttpRequest) {
199                     ((FullHttpRequest) msg).release();
200                     FullHttpResponse response =
201                             new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN);
202                     ctx.channel().writeAndFlush(response);
203                 } else {
204                     ctx.fireChannelRead(msg);
205                 }
206             }
207         };
208     }
209 }