@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { // Remove ourself and do the actual handshake ctx.pipeline().remove(this); handshake(channel, msg, responseHeaders, promise); }
/** * Performs the opening handshake. When call this method you <strong>MUST NOT</strong> retain the * {@link FullHttpRequest} which is passed in. * * @param channel * Channel * @param req * HTTP Request * @return future * The {@link ChannelFuture} which is notified once the opening handshake completes */ public ChannelFuture handshake(Channel channel, FullHttpRequest req) { return handshake(channel, req, null, channel.newPromise()); }
/** * Performs the opening handshake. When call this method you <strong>MUST NOT</strong> retain the * {@link FullHttpRequest} which is passed in. * * @param channel * Channel * @param req * HTTP Request * @return future * The {@link ChannelFuture} which is notified once the opening handshake completes */ public ChannelFuture handshake(Channel channel, HttpRequest req) { return handshake(channel, req, null, channel.newPromise()); }
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()); } }); } }
final String clientId = UUID.randomUUID().toString(); handshaker .handshake( ctx.channel(), httpRequest,
@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); } }
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req); handshakeFuture.addListener(new ChannelFutureListener() { @Override
ServerWebSocketImpl createWebSocket(HttpServerRequestImpl request) { if (ws != null) { return ws; } if (!(request.getRequest() instanceof FullHttpRequest)) { throw new IllegalStateException(); } FullHttpRequest nettyReq = (FullHttpRequest) request.getRequest(); WebSocketServerHandshaker handshaker = createHandshaker(nettyReq); if (handshaker == null) { return null; } Function<ServerWebSocketImpl, String> f = ws -> { handshaker.handshake(chctx.channel(), nettyReq); // remove compressor as its not needed anymore once connection was upgraded to websockets ChannelHandler handler = chctx.pipeline().get(HttpChunkContentCompressor.class); if (handler != null) { chctx.pipeline().remove(handler); } ws.registerHandler(vertx.eventBus()); return handshaker.selectedSubprotocol(); }; ws = new ServerWebSocketImpl(vertx, request.uri(), request.path(), request.query(), request.headers(), this, handshaker.version() != WebSocketVersion.V00, f, options.getMaxWebsocketFrameSize(), options.getMaxWebsocketMessageSize()); if (METRICS_ENABLED && metrics != null) { ws.setMetric(metrics.connected(metric(), request.metric(), ws)); } return ws; }
return WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel); } else { return handshaker.handshake( channel, req.getNativeRequest(),
return handshake(channel, (FullHttpRequest) req, responseHeaders, promise);
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); } }
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req);
ServerWebSocketImpl createWebSocket(HttpServerRequestImpl request) { if (ws != null) { return ws; } if (!(request.getRequest() instanceof FullHttpRequest)) { throw new IllegalStateException(); } FullHttpRequest nettyReq = (FullHttpRequest) request.getRequest(); WebSocketServerHandshaker handshaker = createHandshaker(nettyReq); if (handshaker == null) { return null; } Function<ServerWebSocketImpl, String> f = ws -> { handshaker.handshake(chctx.channel(), nettyReq); // remove compressor as its not needed anymore once connection was upgraded to websockets ChannelHandler handler = chctx.pipeline().get(HttpChunkContentCompressor.class); if (handler != null) { chctx.pipeline().remove(handler); } ws.registerHandler(vertx.eventBus()); return handshaker.selectedSubprotocol(); }; ws = new ServerWebSocketImpl(vertx, request.uri(), request.path(), request.query(), request.headers(), this, handshaker.version() != WebSocketVersion.V00, f, options.getMaxWebsocketFrameSize(), options.getMaxWebsocketMessageSize()); if (METRICS_ENABLED && metrics != null) { ws.setMetric(metrics.connected(metric(), request.metric(), ws)); } return ws; }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { // Remove ourself and do the actual handshake ctx.pipeline().remove(this); handshake(channel, msg, responseHeaders, promise); }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { // Remove ourself and do the actual handshake ctx.pipeline().remove(this); handshake(channel, msg, responseHeaders, promise); }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception { // Remove ourself and do the actual handshake ctx.pipeline().remove(this); handshake(channel, msg, responseHeaders, promise); }