Пример #1
0
 public Handshaker(WebSocketServerHandshaker serverHandshaker, IChannel channel, HttpHeaders responseHeaders, IPromise completion)
 {
     _serverHandshaker = serverHandshaker;
     _channel          = channel;
     _responseHeaders  = responseHeaders;
     _completion       = completion;
 }
Пример #2
0
 public Handshaker(WebSocketServerHandshaker serverHandshaker, IChannel channel, HttpHeaders responseHeaders, TaskCompletionSource completion)
 {
     this.serverHandshaker = serverHandshaker;
     this.channel          = channel;
     this.responseHeaders  = responseHeaders;
     this.completion       = completion;
 }
Пример #3
0
        protected override void Decode(IChannelHandlerContext ctx, WebSocketFrame frame, List <object> output)
        {
            switch (frame.Opcode)
            {
            case Opcode.Ping:     // 从 WebSocketProtocolHandler.Decode 直接复制
                var contect = frame.Content;
                _ = contect.Retain();
                _ = ctx.Channel.WriteAndFlushAsync(new PongWebSocketFrame(contect));
                ReadIfNeeded(ctx);
                return;

            case Opcode.Pong when DropPongFrames:     // 从 WebSocketProtocolHandler.Decode 直接复制
                // Pong frames need to get ignored
                ReadIfNeeded(ctx);
                return;

            case Opcode.Close when _serverConfig.HandleCloseFrames:
                WebSocketServerHandshaker handshaker = GetHandshaker(ctx.Channel);
                if (handshaker is object)
                {
                    _ = frame.Retain();
                    _ = handshaker.CloseAsync(ctx.Channel, (CloseWebSocketFrame)frame);
                }
                else
                {
                    _ = ctx.WriteAndFlushAsync(Unpooled.Empty).CloseOnComplete(ctx);
                }

                return;

            default:     // 从 WebSocketProtocolHandler.Decode 直接复制
                output.Add(frame.Retain());
                break;
            }
        }
        public override void ChannelRead(IChannelHandlerContext ctx, object msg)
        {
            var req = (IFullHttpRequest)msg;

            if (this.IsNotWebSocketPath(req))
            {
                ctx.FireChannelRead(msg);
                return;
            }

            try
            {
                if (!Equals(req.Method, Get))
                {
                    SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                    return;
                }

                var wsFactory = new WebSocketServerHandshakerFactory(
                    GetWebSocketLocation(ctx.Channel.Pipeline, req, this.websocketPath), this.subprotocols,
                    this.allowExtensions, this.maxFramePayloadSize, this.allowMaskMismatch);
                WebSocketServerHandshaker handshaker = wsFactory.NewHandshaker(req);
                if (handshaker == null)
                {
                    WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
                }
                else
                {
                    Task task = handshaker.HandshakeAsync(ctx.Channel, req);
                    task.ContinueWith(t =>
                    {
                        if (t.Status != TaskStatus.RanToCompletion)
                        {
                            ctx.FireExceptionCaught(t.Exception);
                        }
                        else
                        {
                            ctx.FireUserEventTriggered(new WebSocketServerProtocolHandler.HandshakeComplete(
                                                           req.Uri, req.Headers, handshaker.SelectedSubprotocol));
                        }
                    },
                                      TaskContinuationOptions.ExecuteSynchronously);

                    WebSocketServerProtocolHandler.SetHandshaker(ctx.Channel, handshaker);
                    ctx.Channel.Pipeline.Replace(this, "WS403Responder",
                                                 WebSocketServerProtocolHandler.ForbiddenHttpRequestResponder());
                }
            }
            finally
            {
                req.Release();
            }
        }
Пример #5
0
        public override void ChannelRead(IChannelHandlerContext ctx, object msg)
        {
            var req = (IFullHttpRequest)msg;

            if (IsNotWebSocketPath(req))
            {
                _ = ctx.FireChannelRead(msg);
                return;
            }

            try
            {
                if (!Equals(Get, req.Method))
                {
                    SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden, ctx.Allocator.Buffer(0)));
                    return;
                }

                var wsFactory = new WebSocketServerHandshakerFactory(
                    GetWebSocketLocation(ctx.Pipeline, req, _serverConfig.WebsocketPath), _serverConfig.Subprotocols, _serverConfig.DecoderConfig);
                WebSocketServerHandshaker handshaker = wsFactory.NewHandshaker(req);
                if (handshaker is null)
                {
                    _ = WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
                }
                else
                {
                    // Ensure we set the handshaker and replace this handler before we
                    // trigger the actual handshake. Otherwise we may receive websocket bytes in this handler
                    // before we had a chance to replace it.
                    //
                    // See https://github.com/netty/netty/issues/9471.
                    WebSocketServerProtocolHandler.SetHandshaker(ctx.Channel, handshaker);
                    _ = ctx.Pipeline.Remove(this);

                    Task task = handshaker.HandshakeAsync(ctx.Channel, req);
                    _ = task.ContinueWith(FireUserEventTriggeredAction, (ctx, req, handshaker, _handshakePromise), TaskContinuationOptions.ExecuteSynchronously);
                    ApplyHandshakeTimeout();
                }
            }
            finally
            {
                _ = req.Release();
            }
        }
Пример #6
0
        protected internal override void Decode(IChannelHandlerContext ctx, WebSocketFrame frame, List <object> output)
        {
            if (frame is CloseWebSocketFrame socketFrame)
            {
                WebSocketServerHandshaker handshaker = GetHandshaker(ctx.Channel);
                if (handshaker != null)
                {
                    frame.Retain();
                    handshaker.CloseAsync(ctx.Channel, socketFrame);
                }
                else
                {
                    ctx.WriteAndFlushAsync(Unpooled.Empty)
                    .ContinueWith((t, c) => ((IChannelHandlerContext)c).CloseAsync(),
                                  ctx, TaskContinuationOptions.ExecuteSynchronously);
                }

                return;
            }

            base.Decode(ctx, frame, output);
        }
Пример #7
0
 internal static void SetHandshaker(IChannel channel, WebSocketServerHandshaker handshaker) => channel.GetAttribute(HandshakerAttrKey).Set(handshaker);