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, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith, true);
136     }
137 
138     public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
139                                           boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch,
140                                           boolean checkStartsWith, boolean dropPongFrames) {
141         super(dropPongFrames);
142         this.websocketPath = websocketPath;
143         this.subprotocols = subprotocols;
144         this.allowExtensions = allowExtensions;
145         maxFramePayloadLength = maxFrameSize;
146         this.allowMaskMismatch = allowMaskMismatch;
147         this.checkStartsWith = checkStartsWith;
148     }
149 
150     @Override
151     public void handlerAdded(ChannelHandlerContext ctx) {
152         ChannelPipeline cp = ctx.pipeline();
153         if (cp.get(WebSocketServerProtocolHandshakeHandler.class) == null) {
154             // Add the WebSocketHandshakeHandler before this one.
155             ctx.pipeline().addBefore(ctx.name(), WebSocketServerProtocolHandshakeHandler.class.getName(),
156                     new WebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols,
157                             allowExtensions, maxFramePayloadLength, allowMaskMismatch, checkStartsWith));
158         }
159         if (cp.get(Utf8FrameValidator.class) == null) {
160             // Add the UFT8 checking before this one.
161             ctx.pipeline().addBefore(ctx.name(), Utf8FrameValidator.class.getName(),
162                     new Utf8FrameValidator());
163         }
164     }
165 
166     @Override
167     protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
168         if (frame instanceof CloseWebSocketFrame) {
169             WebSocketServerHandshaker handshaker = getHandshaker(ctx.channel());
170             if (handshaker != null) {
171                 frame.retain();
172                 handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
173             } else {
174                 ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
175             }
176             return;
177         }
178         super.decode(ctx, frame, out);
179     }
180 
181     @Override
182     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
183         if (cause instanceof WebSocketHandshakeException) {
184             FullHttpResponse response = new DefaultFullHttpResponse(
185                     HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes()));
186             ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
187         } else {
188             ctx.fireExceptionCaught(cause);
189             ctx.close();
190         }
191     }
192 
193     static WebSocketServerHandshaker getHandshaker(Channel channel) {
194         return channel.attr(HANDSHAKER_ATTR_KEY).get();
195     }
196 
197     static void setHandshaker(Channel channel, WebSocketServerHandshaker handshaker) {
198         channel.attr(HANDSHAKER_ATTR_KEY).set(handshaker);
199     }
200 
201     static ChannelHandler forbiddenHttpRequestResponder() {
202         return new ChannelInboundHandlerAdapter() {
203             @Override
204             public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
205                 if (msg instanceof FullHttpRequest) {
206                     ((FullHttpRequest) msg).release();
207                     FullHttpResponse response =
208                             new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.FORBIDDEN);
209                     ctx.channel().writeAndFlush(response);
210                 } else {
211                     ctx.fireChannelRead(msg);
212                 }
213             }
214         };
215     }
216 }