From eab5977ee19658144ebda51889c2ea7aef9f8379 Mon Sep 17 00:00:00 2001 From: Bruno Massa Date: Tue, 9 Apr 2024 01:13:17 -0500 Subject: [PATCH] fix: 404 page now exception on disposed resource --- source/ServeCommand.cs | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/source/ServeCommand.cs b/source/ServeCommand.cs index 5a0c0d8..4dacbaf 100644 --- a/source/ServeCommand.cs +++ b/source/ServeCommand.cs @@ -182,37 +182,34 @@ public sealed class ServeCommand : BaseGeneratorCommand, IDisposable string? resultType = null; if (handlers is not null) { - try + var response = new HttpListenerResponseWrapper(context.Response); + foreach (var item in handlers) { - var response = new HttpListenerResponseWrapper(context.Response); - foreach (var item in handlers) + if (!item.Check(requestPath)) { - if (!item.Check(requestPath)) - { - continue; - } + continue; + } - try - { - resultType = await item.Handle(response, requestPath, serverStartTime).ConfigureAwait(false); - } - catch (Exception ex) - { - logger.Debug(ex, "Error handling the request."); - } + try + { + resultType = await item.Handle(response, requestPath, serverStartTime).ConfigureAwait(false); break; } - } - finally - { - context.Response.OutputStream.Close(); + catch (Exception ex) + { + logger.Debug(ex, "Error handling the request."); + } } } if (resultType is null) { resultType = "404"; - await HandleNotFoundRequest(context).ConfigureAwait(false); + await HandleNotFoundRequest(context).ConfigureAwait(true); + } + else + { + context.Response.OutputStream.Close(); } logger.Debug("Request {type}\tfor {RequestPath}", resultType, requestPath); } -- GitLab