Esempio n. 1
0
        private async Task HandleBeforeSocketAsync(HttpContext context, MariBaseWebSocketService service)
        {
            if (service.HasNoContent())
            {
                _logger.LogInformation($"No WebSocketService found for the request path:" +
                                       $" \"{context.Request.Path}\".");
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            if (service.IsDisposed)
            {
                _logger.LogError(new ObjectDisposedException(nameof(service)),
                                 "The server cannot use that service because he's disposed.");
                context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
                return;
            }

            _logger.LogDebug("Trying authorize the request...");
            if (!await service.AuthorizeAsync(context).Try(_logger, service, default))
            {
                _logger.LogInformation("The WebSocketService returned unauthorized for that request.");
                context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }

            _logger.LogInformation("Authorized the request by the WebSocketService.");
            _logger.LogTrace($"Accepting the WebSocket request for " +
                             $"{context.Connection.RemoteIpAddress}:{context.Connection.RemotePort}.");

            var nativeSocket = await context.WebSockets.AcceptWebSocketAsync()
                               .Try(_logger, service, default);

            _logger.LogInformation("Successfully accepted the WebSocket request.");

            await HandleAfterSocketAsync(nativeSocket, service, context);
        }