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