diff --git a/SimpleHttpServer/HttpServer.cs b/SimpleHttpServer/HttpServer.cs index 5a16308..b8e77f9 100644 --- a/SimpleHttpServer/HttpServer.cs +++ b/SimpleHttpServer/HttpServer.cs @@ -252,7 +252,7 @@ public sealed class HttpServer { if (reqPath.StartsWith(k)) { // do a static serve wasStaticlyServed = true; var relativeStaticReqPath = reqPath[k.Length..]; - var staticResponsePath = Path.Combine(v, relativeStaticReqPath); + var staticResponsePath = Path.Combine(v, relativeStaticReqPath.TrimStart('/')); if (Path.GetRelativePath(v, staticResponsePath).Contains("..")) { requestLogger.Warning($"Blocked GET request to {reqPath} as somehow the target file does not lie inside the static serve folder? Are you using symlinks?"); @@ -262,7 +262,7 @@ public sealed class HttpServer { if (File.Exists(staticResponsePath)) { rc.SetStatusCode(HttpStatusCode.OK); - using var f = File.OpenRead(v); + using var f = File.OpenRead(staticResponsePath); await f.CopyToAsync(rc.ListenerContext.Response.OutputStream); } else { await rc.SetStatusCodeAndDisposeAsync(HttpStatusCode.NotFound); @@ -278,8 +278,8 @@ public sealed class HttpServer { } catch (Exception ex) { await HandleDefaultErrorPageAsync(rc, 500); mainLogger.Fatal($"Caught otherwise uncaught exception while ProcessingRequest:\n{ex}"); - } - finally { + } finally { + try { await rc.RespWriter.FlushAsync(); } catch (ObjectDisposedException) { } rc.ListenerContext.Response.Close(); } @@ -288,6 +288,7 @@ public sealed class HttpServer { private static async Task HandleDefaultErrorPageAsync(RequestContext ctx, int errorCode) { + ctx.SetStatusCode(errorCode); await ctx.WriteLineToRespAsync($"""

Oh no, an error occurred!