src/packages/EmbedIO/WebServerBase`1.cs
/// <summary>
/// Asynchronously handles a received request.
/// </summary>
/// <param name="context">The context of the request.</param>
/// <returns>A <see cref="Task"/> representing the ongoing operation.</returns>
Asynchronously handles a received request.
Asynchronously handles a received request .
protected async Task DoHandleContextAsync([ValidatedNotNull] IHttpContextImpl context)
        {
            context.SupportCompressedRequests = Options.SupportCompressedRequests;
            context.MimeTypeProviders.Push(this);
            try
            {
                $"[{context.Id}] {context.Request.SafeGetRemoteEndpointStr()}: {context.Request.HttpMethod} {context.Request.Url.PathAndQuery} - {context.Request.UserAgent}"
                    .Debug(LogSource);
                if (SessionManager != null)
                    context.Session = new SessionProxy(context, SessionManager);
                try
                {
                    if (context.CancellationToken.IsCancellationRequested)
                        return;
                    try
                    {
                        await _modules.DispatchRequestAsync(context).ConfigureAwait(false);
                        if (!context.IsHandled)
                        {
                            $"[{context.Id}] No module generated a response. Sending 404 - Not Found".Error(LogSource);
                            throw HttpException.NotFound("No module was able to serve the requested path.");
                        }
                    }
                    catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
                    {
                        throw; 
                    }
                    catch (HttpListenerException)
                    {
                        throw; 
                    }
                    catch (Exception exception) when (exception is IHttpException)
                    {
                        await HttpExceptionHandler.Handle(LogSource, context, exception, _onHttpException)
                            .ConfigureAwait(false);
                    }
#pragma warning disable CA1031 
                    catch (Exception exception)
#pragma warning restore CA1031
                    {
                        await ExceptionHandler.Handle(LogSource, context, exception, _onUnhandledException, _onHttpException)
                            .ConfigureAwait(false);
                    }
                }
                finally
                {
                    await context.Response.OutputStream.FlushAsync(context.CancellationToken)
                        .ConfigureAwait(false);
                    var statusCode = context.Response.StatusCode;
                    var statusDescription = context.Response.StatusDescription;
                    var sendChunked = context.Response.SendChunked;
                    var contentLength = context.Response.ContentLength64;
                    context.Close();
                    $"[{context.Id}] {context.Request.HttpMethod} {context.Request.Url.AbsolutePath}: \"{statusCode} {statusDescription}\" sent in {context.Age}ms ({(sendChunked ? "chunked" : contentLength.ToString(CultureInfo.InvariantCulture) + " bytes")})"
                        .Info(LogSource);
                }
            }
            catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested)
            {
                $"[{context.Id}] Operation canceled.".Debug(LogSource);
            }
            catch (HttpListenerException ex)
            {
                ex.Log(LogSource, $"[{context.Id}] Listener exception.");
            }
#pragma warning disable CA1031 
            catch (Exception ex)
#pragma warning restore CA1031
            {
                ex.Log(LogSource, $"[{context.Id}] Fatal exception.");
                OnFatalException();
            }
        }
protected async Task DoHandleContextAsync ( [ ValidatedNotNull ] IHttpContextImpl context ) { context . SupportCompressedRequests = Options . SupportCompressedRequests ; context . MimeTypeProviders . Push ( this ) ; try { $" [ { context . Id } ]  { context . Request . SafeGetRemoteEndpointStr ( ) } :  { context . Request . HttpMethod } { context . Request . Url . PathAndQuery }  -  { context . Request . UserAgent } " . Debug ( LogSource ) ; if ( SessionManager != null ) context . Session = new SessionProxy ( context , SessionManager ) ; try { if ( context . CancellationToken . IsCancellationRequested ) return ; try { await _modules . DispatchRequestAsync ( context ) . ConfigureAwait ( false ) ; if ( ! context . IsHandled ) { $" [ { context . Id } ] No module generated a response. Sending 404 - Not Found " . Error ( LogSource ) ; throw HttpException . NotFound ( " " ) ; } } catch ( OperationCanceledException ) when ( context . CancellationToken . IsCancellationRequested ) { throw ; } catch ( HttpListenerException ) { throw ; } catch ( Exception exception ) when ( exception is IHttpException ) { await HttpExceptionHandler . Handle ( LogSource , context , exception , _onHttpException ) . ConfigureAwait ( false ) ; } pragma warning disable CA1031 catch ( Exception exception ) pragma warning restore CA1031 { await ExceptionHandler . Handle ( LogSource , context , exception , _onUnhandledException , _onHttpException ) . ConfigureAwait ( false ) ; } } finally { await context . Response . OutputStream . FlushAsync ( context . CancellationToken ) . ConfigureAwait ( false ) ; var statusCode = context . Response . StatusCode ; var statusDescription = context . Response . StatusDescription ; var sendChunked = context . Response . SendChunked ; var contentLength = context . Response . ContentLength64 ; context . Close ( ) ; $" [ { context . Id } ]  { context . Request . HttpMethod } { context . Request . Url . AbsolutePath } :  \" { statusCode } { statusDescription } \"  sent in  { context . Age } ms ( { ( sendChunked ? " " : contentLength . ToString ( CultureInfo . InvariantCulture ) + " " ) } ) " . Info ( LogSource ) ; } } catch ( OperationCanceledException ) when ( context . CancellationToken . IsCancellationRequested ) { $" [ { context . Id } ] Operation canceled. " . Debug ( LogSource ) ; } catch ( HttpListenerException ex ) { ex . Log ( LogSource , $" [ { context . Id } ] Listener exception. " ) ; } pragma warning disable CA1031 catch ( Exception ex ) pragma warning restore CA1031 { ex . Log ( LogSource , $" [ { context . Id } ] Fatal exception. " ) ; OnFatalException ( ) ; } }
Asynchronously handles a received request.
Asynchronously handles a received request .
// Return a 404 (Not Found) response if no module handled the response. // Let outer catch block handle it // Let outer catch block handle it // Don't catch Exception - That's exactly what we need to do here. // Don't catch Exception - That's exactly what we need to do here.
A  representing the ongoing operation.
A representing the ongoing operation .
IHttpContextImpl
[ValidatedNotNull]
context
The context of the request.
The context of the request .