public override async Task HandleRequestAsync(IHttpContext context) { if (context.IsHandled) { return; } if (!context.Request.IsWebSocketRequest) { return; } var absolutePath = context.Request.Url.AbsolutePath; var socket = webSockets.FirstOrDefault(f => f.Route == absolutePath); if (socket == null) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; context.Close(); return; } if (!socket.IsInstanceGenerated) { throw new NullReferenceException($"{socket.SocketType.Name} is null reference"); } if (socket.Instance != null) { var httpListenerWebSocketContext = await context.AcceptWebSocketRequest(); socket.Instance.Invoke(WebSocketConstants.OnBeforeConnectedMethod, BindingFlags.Instance | BindingFlags.NonPublic, new[] { httpListenerWebSocketContext }); } }
public async Task DispatchRequestAsync(IHttpContext httpContext) { // Determine the request log-string var request = httpContext.Request; var response = httpContext.Response; var logRequest = $"{request.HttpMethod}:{request.Url}:{request.RemoteEndPoint?.Address}"; // Log the request s_log.Log(LogLevel.Info, () => $"{logRequest} - Start processing"); try { // Set the Server header of the response response.SetHeaderValue("Server", s_serverName); // Start the stopwatch var sw = Stopwatch.StartNew(); IRequestHandler requestHandler; try { // Obtain the request handler for this message requestHandler = _requestHandlerFactory.GetRequestHandler(httpContext); // Make sure we got a request handler if (requestHandler == null) { // Log warning s_log.Log(LogLevel.Warning, () => $"{logRequest} - Not implemented."); // This request is not implemented httpContext.Response.SendResponse(DavStatusCode.NotImplemented); return; } } catch (Exception exc) { // Log error s_log.Log(LogLevel.Error, () => $"Unexpected exception while trying to obtain the request handler (method={request.HttpMethod}, url={request.Url}, source={request.RemoteEndPoint}", exc); // Abort return; } try { // Handle the request if (await requestHandler.HandleRequestAsync(httpContext, _store).ConfigureAwait(false)) { // Log processing duration s_log.Log(LogLevel.Info, () => $"{logRequest} - Finished processing ({sw.ElapsedMilliseconds}ms, HTTP result: {httpContext.Response.Status})"); } else { // Log warning s_log.Log(LogLevel.Warning, () => $"{logRequest} - Not processed."); // Set status code to bad request httpContext.Response.SendResponse(DavStatusCode.NotImplemented); } } catch (Exception exc) { // Log what's going wrong s_log.Log(LogLevel.Error, () => $"Unexpected exception while handling request (method={request.HttpMethod}, url={request.Url}, source={request.RemoteEndPoint}", exc); try { // Attempt to return 'InternalServerError' (if still possible) httpContext.Response.SendResponse(DavStatusCode.InternalServerError); } catch { // We might not be able to send the response, because a response // was already initiated by the the request handler. } } finally { // Check if we need to dispose the request handler (requestHandler as IDisposable)?.Dispose(); } } finally { // Always close the context httpContext.Close(); } }