private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String path, FullHttpRequest req) { final Channel channel = ctx.channel(); WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, configuration.getMaxFramePayloadLength()); WebSocketServerHandshaker handshaker = factory.newHandshaker(req); if (handshaker != null) { ChannelFuture f = handshaker.handshake(channel, req); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { log.error("Can't handshake " + sessionId, future.cause()); return; } channel.pipeline().addBefore(SocketIOChannelInitializer.WEB_SOCKET_TRANSPORT, SocketIOChannelInitializer.WEB_SOCKET_AGGREGATOR, new WebSocketFrameAggregator(configuration.getMaxFramePayloadLength())); connectClient(channel, sessionId); } }); } else { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } }
private void upgradeChannel(final ChannelHandlerContext ctx, FullHttpRequest httpRequest) { handshaker = new WebSocketServerHandshakerFactory( "ws://" + httpRequest.headers().get(HOST) + UPGRADE_CHANNEL_FOR_UI_WEB_SOCKET_URI, null, true, Integer.MAX_VALUE ).newHandshaker(httpRequest); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake( ctx.channel(), httpRequest, new DefaultHttpHeaders(), ctx.channel().newPromise() ).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { clientRegistry.put(ctx, request()); } }); } }
true, Integer.MAX_VALUE ).newHandshaker(httpRequest); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
@SuppressWarnings("unchecked") @Override public <T> T upgrade(final Class<T> type) { if (type == NativeWebSocket.class) { String protocol = ifSecure("wss", "ws"); String webSocketURL = protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path; WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( webSocketURL, null, true, wsMaxMessageSize); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); NettyWebSocket result = new NettyWebSocket(ctx, handshaker, (ws) -> { handshaker.handshake(ctx.channel(), (FullHttpRequest) req) .addListener(FIRE_EXCEPTION_ON_FAILURE) .addListener(payload -> ws.connect()) .addListener(FIRE_EXCEPTION_ON_FAILURE); }); ctx.channel().attr(NettyWebSocket.KEY).set(result); return (T) result; } else if (type == Sse.class) { NettySse sse = new NettySse(ctx, responseHeaders); return (T) sse; } else if (type == NativePushPromise.class) { return (T) new NettyPush(ctx, req.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()), header("host").orElse(ip()), ifSecure("https", "http")); } throw new UnsupportedOperationException("Not Supported: " + type); }
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) { if (isWebSocketRequest(req)) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(req.uri(), null, true); this.handshaker = wsFactory.newHandshaker(req); if (this.handshaker == null) { //Return that we need cannot not support the web socket version WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { this.handshaker.handshake(ctx.channel(), req); this.context = new WebSocketContext(ctx); this.uri = req.uri(); initHandlerWrapper(); this.handler.onConnect(this.context); } } else { ReferenceCountUtil.retain(req); ctx.fireChannelRead(req); } }
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req) { if (isWebSocketRequest(req)) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(req.uri(), null, true); this.handshaker = wsFactory.newHandshaker(req); if (this.handshaker == null) { //Return that we need cannot not support the web socket version WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { this.handshaker.handshake(ctx.channel(), req); this.context = new WebSocketContext(ctx); this.uri = req.uri(); initHandlerWrapper(); this.handler.onConnect(this.context); } } else { ReferenceCountUtil.retain(req); ctx.fireChannelRead(req); } }
getWebSocketLocation(ctx.pipeline(), req, websocketPath), subprotocols, allowExtensions, maxFramePayloadSize, allowMaskMismatch); final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
options.perMessageWebsocketCompressionSupported() || options.perFrameWebsocketCompressionSupported(), options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames()); WebSocketServerHandshaker shake = factory.newHandshaker(request); if (shake == null) {
maxFramePayloadLength ); handshaker = wsFactory.newHandshaker(req.getNativeRequest()); MutableHttpHeaders headers = response.getHeaders(); io.netty.handler.codec.http.HttpHeaders nettyHeaders;
private void handleWebSocketResponse(ChannelHandlerContext ctx, Outgoing out) { WebSocketHttpResponse response = (WebSocketHttpResponse) out.message; WebSocketServerHandshaker handshaker = response.handshakerFactory().newHandshaker(lastRequest); if (handshaker == null) { HttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); HttpUtil.setContentLength(res, 0); super.unbufferedWrite(ctx, new Outgoing(res, out.promise)); response.subscribe(new CancelledSubscriber<>()); } else { // First, insert new handlers in the chain after us for handling the websocket ChannelPipeline pipeline = ctx.pipeline(); HandlerPublisher<WebSocketFrame> publisher = new HandlerPublisher<>(ctx.executor(), WebSocketFrame.class); HandlerSubscriber<WebSocketFrame> subscriber = new HandlerSubscriber<>(ctx.executor()); pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-subscriber", subscriber); pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-publisher", publisher); // Now remove ourselves from the chain ctx.pipeline().remove(ctx.name()); // Now do the handshake // Wrap the request in an empty request because we don't need the WebSocket handshaker ignoring the body, // we already have handled the body. handshaker.handshake(ctx.channel(), new EmptyHttpRequest(lastRequest)); // And hook up the subscriber/publishers response.subscribe(subscriber); publisher.subscribe(response); } }
private void handleHttpRequest(final ChannelHandlerContext ctx, final FullHttpRequest req) throws Exception { // Allow only GET methods. if (req.getMethod() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } int maxFrameSize = ErraiConfigAttribs.WEB_SOCKET_MAX_FRAME_SIZE.getInt(svc.getConfiguration()); // Handshake final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( this.getWebSocketLocation(req), null, false, maxFrameSize); this.handshaker = wsFactory.newHandshaker(req); if (this.handshaker == null) { wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel()); } else { this.handshaker.handshake(ctx.channel(), req); } }
handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
options.perMessageWebsocketCompressionSupported() || options.perFrameWebsocketCompressionSupported(), options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames()); WebSocketServerHandshaker shake = factory.newHandshaker(request); if (shake == null) {
@Override public HandshakeFuture handshake() { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), null, true); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, 0); }
@Override public ServerHandshakeFuture handshake(String[] subProtocols) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), getSubProtocolsCSV(subProtocols), allowExtensions); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, 0, null); }
@Override public ServerHandshakeFuture handshake(String[] subProtocols, int idleTimeout) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), getSubProtocolsCSV(subProtocols), allowExtensions); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, idleTimeout, null); }
@Override public ServerHandshakeFuture handshake(String[] subProtocols, int idleTimeout, HttpHeaders responseHeaders) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), getSubProtocolsCSV(subProtocols), allowExtensions); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, idleTimeout, responseHeaders); }
@Override public ServerHandshakeFuture handshake(String[] subProtocols, int idleTimeout, HttpHeaders responseHeaders, int maxFramePayloadLength) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), getSubProtocolsCSV(subProtocols), allowExtensions, maxFramePayloadLength); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, idleTimeout, responseHeaders); }
@Override public HandshakeFuture handshake(String[] subProtocols, boolean allowExtensions) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), getSubProtocolsCSV(subProtocols), allowExtensions); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, 0); }
@Override public HandshakeFuture handshake(String[] subProtocols, boolean allowExtensions, int idleTimeout) { WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketURL(httpRequest), getSubProtocolsCSV(subProtocols), allowExtensions); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(httpRequest); return handleHandshake(handshaker, idleTimeout); }