View Javadoc
1   /*
2    * Copyright 2014 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    *   https://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.extensions;
17  
18  import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
19  
20  import io.netty.channel.ChannelDuplexHandler;
21  import io.netty.channel.ChannelFuture;
22  import io.netty.channel.ChannelFutureListener;
23  import io.netty.channel.ChannelHandlerContext;
24  import io.netty.channel.ChannelPromise;
25  import io.netty.handler.codec.http.HttpHeaderNames;
26  import io.netty.handler.codec.http.HttpHeaders;
27  import io.netty.handler.codec.http.HttpRequest;
28  import io.netty.handler.codec.http.HttpResponse;
29  import io.netty.handler.codec.http.HttpResponseStatus;
30  
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * This handler negotiates and initializes the WebSocket Extensions.
38   *
39   * It negotiates the extensions based on the client desired order,
40   * ensures that the successfully negotiated extensions are consistent between them,
41   * and initializes the channel pipeline with the extension decoder and encoder.
42   *
43   * Find a basic implementation for compression extensions at
44   * <tt>io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler</tt>.
45   */
46  public class WebSocketServerExtensionHandler extends ChannelDuplexHandler {
47  
48      private final List<WebSocketServerExtensionHandshaker> extensionHandshakers;
49  
50      private List<WebSocketServerExtension> validExtensions;
51  
52      /**
53       * Constructor
54       *
55       * @param extensionHandshakers
56       *      The extension handshaker in priority order. A handshaker could be repeated many times
57       *      with fallback configuration.
58       */
59      public WebSocketServerExtensionHandler(WebSocketServerExtensionHandshaker... extensionHandshakers) {
60          this.extensionHandshakers = Arrays.asList(checkNonEmpty(extensionHandshakers, "extensionHandshakers"));
61      }
62  
63      @Override
64      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
65          if (msg instanceof HttpRequest) {
66              HttpRequest request = (HttpRequest) msg;
67  
68              if (WebSocketExtensionUtil.isWebsocketUpgrade(request.headers())) {
69                  String extensionsHeader = request.headers().getAsString(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
70  
71                  if (extensionsHeader != null) {
72                      List<WebSocketExtensionData> extensions =
73                              WebSocketExtensionUtil.extractExtensions(extensionsHeader);
74                      int rsv = 0;
75  
76                      for (WebSocketExtensionData extensionData : extensions) {
77                          Iterator<WebSocketServerExtensionHandshaker> extensionHandshakersIterator =
78                                  extensionHandshakers.iterator();
79                          WebSocketServerExtension validExtension = null;
80  
81                          while (validExtension == null && extensionHandshakersIterator.hasNext()) {
82                              WebSocketServerExtensionHandshaker extensionHandshaker =
83                                      extensionHandshakersIterator.next();
84                              validExtension = extensionHandshaker.handshakeExtension(extensionData);
85                          }
86  
87                          if (validExtension != null && ((validExtension.rsv() & rsv) == 0)) {
88                              if (validExtensions == null) {
89                                  validExtensions = new ArrayList<WebSocketServerExtension>(1);
90                              }
91                              rsv = rsv | validExtension.rsv();
92                              validExtensions.add(validExtension);
93                          }
94                      }
95                  }
96              }
97          }
98  
99          super.channelRead(ctx, msg);
100     }
101 
102     @Override
103     public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
104         if (msg instanceof HttpResponse) {
105             HttpResponse httpResponse = (HttpResponse) msg;
106             //checking the status is faster than looking at headers
107             //so we do this first
108             if (HttpResponseStatus.SWITCHING_PROTOCOLS.equals(httpResponse.status())) {
109                 handlePotentialUpgrade(ctx, promise, httpResponse);
110             }
111         }
112 
113         super.write(ctx, msg, promise);
114     }
115 
116     private void handlePotentialUpgrade(final ChannelHandlerContext ctx,
117                                         ChannelPromise promise, HttpResponse httpResponse) {
118         HttpHeaders headers = httpResponse.headers();
119 
120         if (WebSocketExtensionUtil.isWebsocketUpgrade(headers)) {
121 
122             if (validExtensions != null) {
123                 String headerValue = headers.getAsString(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS);
124                 List<WebSocketExtensionData> extraExtensions =
125                   new ArrayList<WebSocketExtensionData>(extensionHandshakers.size());
126                 for (WebSocketServerExtension extension : validExtensions) {
127                     extraExtensions.add(extension.newReponseData());
128                 }
129                 String newHeaderValue = WebSocketExtensionUtil
130                   .computeMergeExtensionsHeaderValue(headerValue, extraExtensions);
131                 promise.addListener(new ChannelFutureListener() {
132                     @Override
133                     public void operationComplete(ChannelFuture future) {
134                         if (future.isSuccess()) {
135                             for (WebSocketServerExtension extension : validExtensions) {
136                                 WebSocketExtensionDecoder decoder = extension.newExtensionDecoder();
137                                 WebSocketExtensionEncoder encoder = extension.newExtensionEncoder();
138                                 String name = ctx.name();
139                                 ctx.pipeline()
140                                     .addAfter(name, decoder.getClass().getName(), decoder)
141                                     .addAfter(name, encoder.getClass().getName(), encoder);
142                             }
143                         }
144                     }
145                 });
146 
147                 if (newHeaderValue != null) {
148                     headers.set(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS, newHeaderValue);
149                 }
150             }
151 
152             promise.addListener(new ChannelFutureListener() {
153                 @Override
154                 public void operationComplete(ChannelFuture future) {
155                     if (future.isSuccess()) {
156                         ctx.pipeline().remove(WebSocketServerExtensionHandler.this);
157                     }
158                 }
159             });
160         }
161     }
162 }