All Downloads are FREE. Search and download functionalities are using the official Maven repository.

qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.websocketx.WebSocketServerHandshaker00 Maven / Gradle / Ivy

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.websocketx;

import static qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
import static qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpHeaders.Values.*;
import static qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpVersion.*;

import qcloud.storage.ranger.thirdparty.org.jboss.netty.buffer.ChannelBuffer;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.buffer.ChannelBuffers;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.channel.Channel;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.channel.ChannelFuture;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.channel.ChannelFutureListener;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.channel.ChannelPipeline;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpHeaders.Values;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpRequest;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpResponse;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.handler.codec.http.HttpResponseStatus;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.logging.InternalLogger;
import qcloud.storage.ranger.thirdparty.org.jboss.netty.logging.InternalLoggerFactory;

/**
 * 

* Performs server side opening and closing handshakes for web socket specification version draft-ietf-hybi-thewebsocketprotocol- * 00 *

*

* A very large portion of this code was taken from the Netty 3.2 HTTP example. *

*/ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { private static final InternalLogger logger = InternalLoggerFactory.getInstance(WebSocketServerHandshaker00.class); /** * Constructor with default values * * @param webSocketURL * URL for web socket communications. e.g "ws://myhost.com/mypath". Subsequent web socket frames will be * sent to this URL. * @param subprotocols * CSV of supported protocols */ public WebSocketServerHandshaker00(String webSocketURL, String subprotocols) { this(webSocketURL, subprotocols, Long.MAX_VALUE); } /** * Constructor specifying the destination web socket location * * @param webSocketURL * URL for web socket communications. e.g "ws://myhost.com/mypath". * Subsequent web socket frames will be sent to this URL. * @param subprotocols * CSV of supported protocols * @param maxFramePayloadLength * Maximum allowable frame payload length. Setting this value to your application's requirement may * reduce denial of service attacks using long data frames. */ public WebSocketServerHandshaker00(String webSocketURL, String subprotocols, long maxFramePayloadLength) { super(WebSocketVersion.V00, webSocketURL, subprotocols, maxFramePayloadLength); } /** *

* Handle the web socket handshake for the web socket specification HyBi version 0 and lower. This standard * is really a rehash of hixie-76 and * hixie-75. *

* *

* Browser request to the server: *

* *
     * GET /demo HTTP/1.1
     * Upgrade: WebSocket
     * Connection: Upgrade
     * Host: example.com
     * Origin: http://example.com
     * Sec-WebSocket-Protocol: chat, sample
     * Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
     * Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
     *
     * ^n:ds[4U
     * 
* *

* Server response: *

* *
     * HTTP/1.1 101 WebSocket Protocol Handshake
     * Upgrade: WebSocket
     * Connection: Upgrade
     * Sec-WebSocket-Origin: http://example.com
     * Sec-WebSocket-Location: ws://example.com/demo
     * Sec-WebSocket-Protocol: sample
     *
     * 8jKS'y:G*Co,Wxa-
     * 
* * @param channel * Channel * @param req * HTTP request */ @Override public ChannelFuture handshake(Channel channel, HttpRequest req) { if (logger.isDebugEnabled()) { logger.debug(String.format("Channel %s WS Version 00 server handshake", channel.getId())); } // Serve the WebSocket handshake request. if (!Values.UPGRADE.equalsIgnoreCase(req.getHeader(CONNECTION)) || !WEBSOCKET.equalsIgnoreCase(req.getHeader(Names.UPGRADE))) { throw new WebSocketHandshakeException("not a WebSocket handshake request: missing upgrade"); } // Hixie 75 does not contain these headers while Hixie 76 does boolean isHixie76 = req.containsHeader(SEC_WEBSOCKET_KEY1) && req.containsHeader(SEC_WEBSOCKET_KEY2); // Create the WebSocket handshake response. HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake")); res.addHeader(Names.UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, Values.UPGRADE); // Fill in the headers and contents depending on handshake method. if (isHixie76) { // New handshake method with a challenge: res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketUrl()); String subprotocols = req.getHeader(SEC_WEBSOCKET_PROTOCOL); if (subprotocols != null) { String selectedSubprotocol = selectSubprotocol(subprotocols); if (selectedSubprotocol == null) { throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols); } else { res.addHeader(SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); setSelectedSubprotocol(selectedSubprotocol); } } // Calculate the answer of the challenge. String key1 = req.getHeader(SEC_WEBSOCKET_KEY1); String key2 = req.getHeader(SEC_WEBSOCKET_KEY2); int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length()); int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length()); long c = req.getContent().readLong(); ChannelBuffer input = ChannelBuffers.buffer(16); input.writeInt(a); input.writeInt(b); input.writeLong(c); res.setContent(WebSocketUtil.md5(input)); } else { // Old Hixie 75 handshake method with no challenge: res.addHeader(WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader(WEBSOCKET_LOCATION, getWebSocketUrl()); String protocol = req.getHeader(WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(WEBSOCKET_PROTOCOL, selectSubprotocol(protocol)); } } ChannelFuture future = channel.write(res); // Upgrade the connection and send the handshake response. future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.getChannel().getPipeline(); if (p.get(HttpChunkAggregator.class) != null) { p.remove(HttpChunkAggregator.class); } p.get(HttpRequestDecoder.class).replace("wsdecoder", new WebSocket00FrameDecoder(getMaxFramePayloadLength())); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket00FrameEncoder()); } }); return future; } /** * Echo back the closing frame * * @param channel * Channel * @param frame * Web Socket frame that was received */ @Override public ChannelFuture close(Channel channel, CloseWebSocketFrame frame) { return channel.write(frame); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy