public async Task Invoke(Request req, WebSocketDialog wsd, Response res)
 {
     if (_simple)
     {
         foreach (var handler in _simpleHandlers)
         {
             if (res.Closed)
             {
                 break;
             }
             await handler(req, wsd);
         }
     }
     else
     {
         foreach (var handler in _handlers)
         {
             if (res.Closed)
             {
                 break;
             }
             await handler(req, res, wsd);
         }
     }
 }
Exemple #2
0
        internal async Task <HandlerType> Invoke(Request req, WebSocketDialog wsd, Response res)
        {
            var status = HandlerType.Continue;

            if (_simple)
            {
                foreach (var handler in _simpleHandlers)
                {
                    status = await handler(req, wsd);

                    if (status != HandlerType.Continue)
                    {
                        break;
                    }
                }
            }
            else
            {
                foreach (var handler in _handlers)
                {
                    status = await handler(req, res, wsd);

                    if (status != HandlerType.Continue)
                    {
                        break;
                    }
                }
            }

            return(status);
        }
        private async Task <HandlerType> ExecuteHandler(HttpContext aspNetContext,
                                                        IEnumerable <Func <Request, Response, WebSocketDialog, Task <HandlerType> > > handlers)
        {
            var context  = new Context(aspNetContext, Plugins);
            var request  = new Request(context);
            var response = new Response(context);

            var status = HandlerType.Continue;

            try
            {
                if (aspNetContext.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await aspNetContext.WebSockets.AcceptWebSocketAsync();

                    var webSocketDialog = new WebSocketDialog(webSocket, request.Aborted);

                    foreach (var middleware in _wsMiddle.Concat(handlers))
                    {
                        status = await middleware(request, response, webSocketDialog);

                        if (status != HandlerType.Continue)
                        {
                            return(status);
                        }
                    }

                    await webSocketDialog.ReadFromWebSocket();

                    return(status);
                }

                response.Headers["Upgrade"] = "Websocket";
                await response.SendStatus(HttpStatusCode.UpgradeRequired);

                return(HandlerType.Error);
            }
            catch (OperationCanceledException)
            {
                return(HandlerType.Final);
            }
            catch (Exception e)
            {
                return(await HandleException(request, response, status, e));
            }
        }
Exemple #4
0
        private async Task <HandlerType> WrapWebsocketHandler(HttpContext aspNetContext, WsHandlerWrapper handlerWrapper)
        {
            var context  = new Context(aspNetContext, Plugins);
            var request  = context.Request;
            var response = context.Response;

            var status = HandlerType.Continue;

            try
            {
                if (aspNetContext.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await aspNetContext.WebSockets.AcceptWebSocketAsync();

                    var webSocketDialog = new WebSocketDialog(context, webSocket);
                    foreach (var middleware in _wsMiddlewareStack)
                    {
                        status = await middleware.Invoke(request, webSocketDialog, response);

                        if (status != HandlerType.Continue)
                        {
                            return(status);
                        }
                    }
                    if (status == HandlerType.Continue)
                    {
                        status = await handlerWrapper.Invoke(request, webSocketDialog, response);
                    }
                    await webSocketDialog.ReadFromWebSocket();

                    return(status);
                }
                else
                {
                    await response.SendStatus(HttpStatusCode.BadRequest);

                    return(HandlerType.Error);
                }
            }
            catch (Exception e)
            {
                return(await HandleException(context, status, e));
            }
        }
Exemple #5
0
        private async Task WrapWebsocketHandler(HttpContext context, WsHandlerWrapper handlerWrapper)
        {
            var req = new Request(context, Plugins);
            var res = new Response(context, Plugins);

            try
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var wsd = new WebSocketDialog(context, webSocket, Plugins);
                    foreach (var middleware in _wsMiddlewareStack)
                    {
                        if (res.Closed)
                        {
                            break;
                        }
                        await middleware.Process(req, wsd, res);
                    }
                    await handlerWrapper.Invoke(req, wsd, res);

                    await wsd.ReadFromWebSocket();
                }
                else
                {
                    await res.SendStatus(HttpStatusCode.BadRequest);
                }
            }
            catch (Exception e)
            {
                if (!res.Closed)
                {
                    if (!RespondWithExceptionDetails)
                    {
                        await res.SendStatus(HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        await res.SendString(e.ToString(), status : HttpStatusCode.InternalServerError);
                    }
                }
            }
        }
        private async Task WrapWebsocketHandler(HttpContext context, Action <Request, WebSocketDialog> handler)
        {
            var req = new Request(context.Request, Plugins);
            var res = new Response(context.Response, Plugins);

            try
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var url       = context.Request.Path.Value;
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var wsd = new WebSocketDialog(context, webSocket, Plugins);
                    foreach (var middleware in _wsMiddlewareStack)
                    {
                        if (!await middleware.Process(url, req, res, wsd) || res.Closed)
                        {
                            return;
                        }
                    }
                    handler(req, wsd);
                    await wsd.ReadFromWebSocket();
                }
                else
                {
                    if (!res.Closed)
                    {
                        await res.SendStatus(HttpStatusCode.BadRequest);
                    }
                }
            }
            catch (Exception e)
            {
                if (!res.Closed)
                {
                    await res.SendStatus(HttpStatusCode.InternalServerError);
                }
            }
        }
Exemple #7
0
 /// <summary>
 /// Parsing middleware.
 /// Attempts to parse the body using ParseBodyAsync.
 /// If unable to parse the body, responds with Bad Request status.
 /// Otherwise saves the parsed object using SetData on the request, so it can be retrieved using GetData by a later handler.
 /// </summary>
 /// <param name="req">The request object</param>
 /// <param name="res">The response object</param>
 /// <param name="wsd">The websocket dialog (not modified)</param>
 /// <typeparam name="T">The type to parse the body to</typeparam>
 /// <returns></returns>
 public static Task CanParse <T>(Request req, Response res, WebSocketDialog wsd)
     where T : class => CanParse <T>(req, res);
 /// <summary>
 ///     Parsing middleware.
 ///     Attempts to parse the body using ParseBodyAsync.
 ///     If unable to parse the body, responds with Bad Request status.
 ///     Otherwise saves the parsed object using SetData on the request, so it can be retrieved using GetData by a later
 ///     handler.
 /// </summary>
 /// <param name="req">The request object</param>
 /// <param name="res">The response object</param>
 /// <param name="_">The websocket dialog (not modified)</param>
 /// <typeparam name="T">The type to parse the body to</typeparam>
 /// <returns></returns>
 public static Task CanParse <T>(Request req, Response res, WebSocketDialog _)
     where T : class
 {
     return(CanParse <T>(req, res));
 }