void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.BadRequest));
                return;
            }

            // Allow only GET methods.
            if (req.Method.ToString().ToUpper() != "GET")
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.Forbidden));
                return;
            }

            //Wesocket握手协议
            if (req.Uri == _path)
            {
                var wsFactory = new WebSocketServerHandshakerFactory(
                    GetWebSocketLocation(req), null, true, 5 * 1024 * 1024);
                this.handshaker = wsFactory.NewHandshaker(req);
                if (this.handshaker == null)
                {
                    WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
                }
                else
                {
                    this.handshaker.HandshakeAsync(ctx.Channel, req);
                }
            }
        }
Esempio n. 2
0
        void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.BadRequest));
                return;
            }
            if (req.Uri.Equals(WebsocketPath))
            {
                var wsFactory = new WebSocketServerHandshakerFactory(
                    GetWebSocketLocation(req), null, true, _webSocketConfig.MaxMessageLength);
                _handShaker = wsFactory.NewHandshaker(req);
                if (_handShaker == null)
                {
                    WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
                }
                else
                {
                    _handShaker.HandshakeAsync(ctx.Channel, req);
                }
                ConsoleHelper.ConDepServerWriteLine($"新的连接{ctx.Channel.Id}");
                return;
            }
            if (req.Uri.Equals("/favicon.ico"))
            {
                var res = new DefaultFullHttpResponse(HttpVersion.Http11, HttpResponseStatus.NotFound);
                SendHttpResponse(ctx, req, res);
                return;
            }
            var httpHandler = new HttpHandler();
            DefaultFullHttpResponse httpResponse = httpHandler.GetResponse(req);

            SendHttpResponse(ctx, req, httpResponse);
        }
Esempio n. 3
0
        void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(req.Method, HttpMethod.Get))
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(
                GetWebSocketLocation(req), null, true, 5 * 1024 * 1024);

            _handshaker = wsFactory.NewHandshaker(req);
            if (this._handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                _logger.LogInformation($"New client to handshake:{ctx.Channel}");
                var appSession = new AppSession();
                appSession.Initialize(ctx.Channel, ctx.Channel.RemoteAddress);
                _appSessionContainer.Set(ctx.Channel.Id.ToString(), appSession);
                _handshaker.HandshakeAsync(ctx.Channel, req);
            }
        }
Esempio n. 4
0
        void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(req.Method, HttpMethod.Get))
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            // Send the demo page and favicon.ico
            if ("/".Equals(req.Uri))
            {
                IByteBuffer content = Unpooled.WrappedBuffer(Encoding.ASCII.GetBytes(
                                                                 "<html><head><title>XMPP WebSocket Proxy</title></head>" + Environment.NewLine +
                                                                 "<body>" + Environment.NewLine +
                                                                 "<h1>XMPP WebSocket server</h1>" + Environment.NewLine +
                                                                 "<p>running at: " + GetWebSocketLocation(req) + "</p>" + Environment.NewLine +
                                                                 "</body>" + Environment.NewLine +
                                                                 "</html>"));

                var res = new DefaultFullHttpResponse(Http11, OK, content);

                res.Headers.Set(HttpHeaderNames.ContentType, "text/html; charset=UTF-8");
                HttpUtil.SetContentLength(res, content.ReadableBytes);

                SendHttpResponse(ctx, req, res);
                return;
            }
            if ("/favicon.ico".Equals(req.Uri))
            {
                var res = new DefaultFullHttpResponse(Http11, NotFound);
                SendHttpResponse(ctx, req, res);
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(
                GetWebSocketLocation(req), "xmpp", true, 5 * 1024 * 1024);

            this.handshaker = wsFactory.NewHandshaker(req);
            if (this.handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                this.handshaker.HandshakeAsync(ctx.Channel, req);
            }
        }
        void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(req.Method, HttpMethod.Get))
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            // Send the demo page and favicon.ico
            if ("/".Equals(req.Uri))
            {
                IByteBuffer content = WebSocketServerBenchmarkPage.GetContent(GetWebSocketLocation(req));
                var         res     = new DefaultFullHttpResponse(Http11, OK, content);

                res.Headers.Set(HttpHeaderNames.ContentType, "text/html; charset=UTF-8");
                HttpUtil.SetContentLength(res, content.ReadableBytes);

                SendHttpResponse(ctx, req, res);
                return;
            }
            if ("/favicon.ico".Equals(req.Uri))
            {
                var res = new DefaultFullHttpResponse(Http11, NotFound);
                SendHttpResponse(ctx, req, res);
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(
                GetWebSocketLocation(req), null, true, 5 * 1024 * 1024);

            this.handshaker = wsFactory.NewHandshaker(req);
            if (this.handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                this.handshaker.HandshakeAsync(ctx.Channel, req);
            }
        }
        private void HandleHttpRequest(IChannelHandlerContext context, IFullHttpRequest request)
        {
            // Handle a bad request.
            if (!request.Result.IsSuccess)
            {
                SendHttpResponse(context, request, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(request.Method, HttpMethod.Get))
            {
                SendHttpResponse(context, request, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            // Send the demo page and favicon.ico
            if ("/".Equals(request.Uri, StringComparison.OrdinalIgnoreCase))
            {
                var content  = WebSocketServerBenchmarkPage.GetContent(GetWebSocketLocation(request));
                var response = new DefaultFullHttpResponse(Http11, OK, content);

                response.Headers.Set(HttpHeaderNames.ContentType, "text/html; charset=UTF-8");
                HttpUtil.SetContentLength(response, content.ReadableBytes);

                SendHttpResponse(context, request, response);
                return;
            }
            if ("/favicon.ico".Equals(request.Uri, StringComparison.OrdinalIgnoreCase))
            {
                var res = new DefaultFullHttpResponse(Http11, NotFound);
                SendHttpResponse(context, request, res);
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(
                GetWebSocketLocation(request), null, true, 5 * 1024 * 1024);

            handshaker = wsFactory.NewHandshaker(request);
            if (handshaker.IsNull())
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(context.Channel);
            }
            else
            {
                handshaker.HandshakeAsync(context.Channel, request);
            }
        }
        private void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(req.Method, HttpMethod.Get))
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            // Send the demo page and favicon.ico
            if ("/".Equals(req.Uri))
            {
                var res = new DefaultFullHttpResponse(Http11, NotFound);
                SendHttpResponse(ctx, req, res);
                return;
            }

            if ("/favicon.ico".Equals(req.Uri))
            {
                var res = new DefaultFullHttpResponse(Http11, NotFound);
                SendHttpResponse(ctx, req, res);
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(
                GetWebSocketLocation(req), null, true, 5 * 1024 * 1024);

            this._handshaker = wsFactory.NewHandshaker(req);
            if (this._handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                this._handshaker.HandshakeAsync(ctx.Channel, req);
            }
        }
 void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
 {
     // Handle a bad request.
     if (!req.Result.IsSuccess)
     {
         SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
         return;
     }
     if ("/favicon.ico".Equals(req.Uri))
     {
         var res = new DefaultFullHttpResponse(Http11, NotFound);
         SendHttpResponse(ctx, req, res);
         return;
     }
     if (iJT1078Authorization.Authorization(req, out var principal))
     {
         if (req.Uri.StartsWith(WebsocketPath))
         {
             // Handshake
             var wsFactory = new WebSocketServerHandshakerFactory(GetWebSocketLocation(req), null, true, 5 * 1024 * 1024);
             this.handshaker = wsFactory.NewHandshaker(req);
             if (this.handshaker == null)
             {
                 WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
             }
             else
             {
                 this.handshaker.HandshakeAsync(ctx.Channel, req);
                 jT1078HttpSessionManager.TryAdd(principal.Identity.Name, ctx.Channel);
                 httpMiddleware?.Next(ctx, req, principal);
             }
         }
         else
         {
             jT1078HttpSessionManager.TryAdd(principal.Identity.Name, ctx.Channel);
             httpMiddleware?.Next(ctx, req, principal);
         }
     }
     else
     {
         SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Unauthorized));
         return;
     }
 }
Esempio n. 9
0
        public void UnsupportedVersion()
        {
            var ch = new EmbeddedChannel();

            WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ch);
            ch.RunPendingTasks();
            var response = ch.ReadOutbound <IFullHttpResponse>();

            Assert.NotNull(response);

            Assert.Equal(HttpResponseStatus.UpgradeRequired, response.Status);
            Assert.True(response.Headers.TryGet(HttpHeaderNames.SecWebsocketVersion, out ICharSequence value));
            Assert.Equal(WebSocketVersion.V13.ToHttpHeaderValue(), value);
            Assert.True(HttpUtil.IsContentLengthSet(response));
            Assert.Equal(0, HttpUtil.GetContentLength(response));

            ReferenceCountUtil.Release(response);
            Assert.False(ch.Finish());
        }
Esempio n. 10
0
        /// <summary>
        /// WebSocker握手
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="request"></param>
        private void WebSockerHandshake(IChannelHandlerContext ctx, IFullHttpRequest request)
        {
            //获取websocker路径
            bool   result   = request.Headers.TryGet(HttpHeaderNames.Host, out ICharSequence value);
            string location = value.ToString() + websocketPath;
            // Handshake 握手
            var wsFactory = new WebSocketServerHandshakerFactory(
                location, null, true, 5 * 1024 * 1024);

            this.handshaker = wsFactory.NewHandshaker(request);
            if (this.handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                this.handshaker.HandshakeAsync(ctx.Channel, request);
            }
        }
Esempio n. 11
0
        private void HandleHttpRequest(IChannelHandlerContext ctx, IFullHttpRequest req)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                Extention.SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(req.Method, HttpMethod.Get))
            {
                Extention.SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            if ("/favicon.ico".Equals(req.Uri))
            {
                var res = new DefaultFullHttpResponse(Http11, NotFound);
                Extention.SendHttpResponse(ctx, req, res);
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(Extention.GetWebSocketLocation(req, _server._path, _server._useSsl), null, true, 5 * 1024 * 1024);

            handshaker = wsFactory.NewHandshaker(req);

            if (handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                handshaker.HandshakeAsync(ctx.Channel, req);
            }

            _server.connectionDict.TryGetValue(ctx.Channel.Id.AsShortText(), out WebSocketConnection conn);
            _server._event.OnConnectionConnectAction?.Invoke(conn);
        }
Esempio n. 12
0
 protected override void ChannelRead0(IChannelHandlerContext ctx, object msg)
 {
     if (msg is IFullHttpRequest request)
     {
         // Handshake
         var wsFactory = new WebSocketServerHandshakerFactory(
             GetWebSocketLocation(request), null, true, 5 * 1024 * 1024);
         this.handshaker = wsFactory.NewHandshaker(request);
         if (this.handshaker == null)
         {
             WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
         }
         else
         {
             this.handshaker.HandshakeAsync(ctx.Channel, request);
         }
     }
     else if (msg is WebSocketFrame frame)
     {
         this.HandleWebSocketFrame(ctx, frame);
     }
 }
Esempio n. 13
0
        protected override void Decode(IChannelHandlerContext ctx, IFullHttpRequest req, List <object> output)
        {
            // Handle a bad request.
            if (!req.Result.IsSuccess)
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, BadRequest));
                return;
            }

            // Allow only GET methods.
            if (!Equals(req.Method, HttpMethod.Get))
            {
                SendHttpResponse(ctx, req, new DefaultFullHttpResponse(Http11, Forbidden));
                return;
            }

            // Handshake
            var wsFactory = new WebSocketServerHandshakerFactory(
                GetWebSocketLocation(req), null, true, 5 * 1024 * 1024);
            var handshaker = wsFactory.NewHandshaker(req);

            if (handshaker == null)
            {
                WebSocketServerHandshakerFactory.SendUnsupportedVersionResponse(ctx.Channel);
            }
            else
            {
                handshaker.HandshakeAsync(ctx.Channel, req);
            }

            ctx.GetAttribute(wshsKey).Set(handshaker);
            var connectionInfo = channelGroup.FindConnectionInfo(ctx.Channel);

            if (connectionInfo == null)
            {
                channelGroup.Add(new ConnectionInfo(ctx.Channel, this.Options));
            }
            ctx.Channel.Pipeline.FireUserEventTriggered(new HandShakeEvent());
        }