Nice little directory browser :D
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix/Middleware: only swap out response stream if we are not serving a file

* previously would always write stream to own MemoryStream, but this is an issue when getting actual files since we must read it all in first, THEN find out we aren't responding with html and write our stream back to the response. --> caused weird download behaviour
* now take advantage of the other middleware and only swap out streams when we know we are doing a directory (letting Blazor respond).

+40 -36
+40 -36
Program.cs
··· 60 60 app.UseAntiforgery(); 61 61 app.UseResponseCompression(); 62 62 63 - // HTML compression 64 - app.Use(async (context, next) => { 65 - // context.Response.Body is a direct line to the client, so 66 - // swap it out for our own in-memory stream for now 67 - Stream responseStream = context.Response.Body; 68 - using var memoryStream = new MemoryStream(); 69 - context.Response.Body = memoryStream; 70 - 71 - // let downstream render the response & write to our stream 72 - await next(context); 73 - 74 - if ( 75 - context.Response.ContentType?.StartsWith("text/html") != true 76 - || context.Response.Headers.ContentDisposition.Any(x => x != null && x.StartsWith("attachment")) 77 - ) { 78 - // oops my bad gangalang 79 - // ok now put it back 80 - memoryStream.Position = 0; 81 - await memoryStream.CopyToAsync(responseStream); 82 - context.Response.Body = responseStream; 83 - 84 - return; 85 - } 86 - 87 - memoryStream.Position = 0; 88 - String html = await new StreamReader(memoryStream).ReadToEndAsync(); 89 - String minified = Utils.OptimizeHtml(html); 90 - 91 - context.Response.ContentLength = Encoding.UTF8.GetByteCount(minified); 92 - await responseStream.WriteAsync(Encoding.UTF8.GetBytes(minified)); 93 - context.Response.Body = responseStream; 94 - }); 95 - 96 63 // check paths exist 97 64 app.Use(async (context, next) => { 65 + context.Request.Headers.Append("X-Nhnd-Compress-Me", "false"); 98 66 if ( 99 67 // if reexecuting for an error, let someone else handle that 100 68 context.Features.Get<IStatusCodeReExecuteFeature>() != null ··· 107 75 return; 108 76 } 109 77 110 - var resolved = Utils.VerifyPath(context.Request.Path); 78 + var resolved = Utils.VerifyPath(Uri.UnescapeDataString(context.Request.Path)); 111 79 112 80 if (resolved.IsFailed) { 113 81 context.Response.StatusCode = StatusCodes.Status404NotFound; ··· 138 106 139 107 return; 140 108 } 141 - 142 - // Console.WriteLine($"dir={resolved.Value.IsT1} file={resolved.Value.IsT2}"); 143 109 144 110 // we are either a directory or a link to one 111 + context.Request.Headers["X-Nhnd-Compress-Me"] = "true"; 112 + await next(context); 113 + }); 114 + 115 + // HTML compression 116 + app.Use(async (context, next) => { 117 + if (context.Request.Headers["X-Nhnd-Compress-Me"] == "true") { 118 + await next(context); 119 + return; 120 + } 145 121 122 + // context.Response.Body is a direct line to the client, so 123 + // swap it out for our own in-memory stream for now 124 + Stream responseStream = context.Response.Body; 125 + using var memoryStream = new MemoryStream(); 126 + context.Response.Body = memoryStream; 127 + 128 + // let downstream render the response & write to our stream 146 129 await next(context); 130 + 131 + if ( 132 + context.Response.ContentType?.StartsWith("text/html") != true 133 + || context.Response.Headers.ContentDisposition.Any(x => x != null && x.StartsWith("attachment")) 134 + ) { 135 + // oops my bad gangalang 136 + // ok now put it back 137 + memoryStream.Position = 0; 138 + await memoryStream.CopyToAsync(responseStream); 139 + context.Response.Body = responseStream; 140 + 141 + return; 142 + } 143 + 144 + memoryStream.Position = 0; 145 + String html = await new StreamReader(memoryStream).ReadToEndAsync(); 146 + String minified = Utils.OptimizeHtml(html); 147 + 148 + context.Response.ContentLength = Encoding.UTF8.GetByteCount(minified); 149 + await responseStream.WriteAsync(Encoding.UTF8.GetBytes(minified)); 150 + context.Response.Body = responseStream; 147 151 }); 148 152 149 153 app.UseRewriter(new RewriteOptions().AddRedirect(@"^favicon\.ico$", "/.nhnd/favicon.ico"));