summaryrefslogtreecommitdiff
path: root/src/test/burn/WixToolsetTest.BurnE2E/WebServer
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2022-02-18 17:56:31 -0600
committerSean Hall <r.sean.hall@gmail.com>2022-02-19 11:53:06 -0700
commit28c0a27ddf03dcf07a11c291699428a32f381fbc (patch)
tree443b0cf0413d3bff3d70bde53d702372cea5caf6 /src/test/burn/WixToolsetTest.BurnE2E/WebServer
parent7750404222a7c5bb6543dd246c2cce0f7c097d8d (diff)
downloadwix-28c0a27ddf03dcf07a11c291699428a32f381fbc.tar.gz
wix-28c0a27ddf03dcf07a11c291699428a32f381fbc.tar.bz2
wix-28c0a27ddf03dcf07a11c291699428a32f381fbc.zip
Handle missing content length with range request and empty files.
Add test for server without range request support.
Diffstat (limited to 'src/test/burn/WixToolsetTest.BurnE2E/WebServer')
-rw-r--r--src/test/burn/WixToolsetTest.BurnE2E/WebServer/CoreOwinWebServer.cs44
1 files changed, 43 insertions, 1 deletions
diff --git a/src/test/burn/WixToolsetTest.BurnE2E/WebServer/CoreOwinWebServer.cs b/src/test/burn/WixToolsetTest.BurnE2E/WebServer/CoreOwinWebServer.cs
index 025e01ff..8066cea5 100644
--- a/src/test/burn/WixToolsetTest.BurnE2E/WebServer/CoreOwinWebServer.cs
+++ b/src/test/burn/WixToolsetTest.BurnE2E/WebServer/CoreOwinWebServer.cs
@@ -5,8 +5,10 @@ namespace WixToolsetTest.BurnE2E
5 using System; 5 using System;
6 using System.Collections.Generic; 6 using System.Collections.Generic;
7 using System.IO; 7 using System.IO;
8 using System.Threading.Tasks;
8 using Microsoft.AspNetCore.Builder; 9 using Microsoft.AspNetCore.Builder;
9 using Microsoft.AspNetCore.Hosting; 10 using Microsoft.AspNetCore.Hosting;
11 using Microsoft.AspNetCore.Http;
10 using Microsoft.AspNetCore.StaticFiles; 12 using Microsoft.AspNetCore.StaticFiles;
11 using Microsoft.Extensions.FileProviders; 13 using Microsoft.Extensions.FileProviders;
12 using Microsoft.Extensions.FileProviders.Physical; 14 using Microsoft.Extensions.FileProviders.Physical;
@@ -15,11 +17,14 @@ namespace WixToolsetTest.BurnE2E
15 17
16 public class CoreOwinWebServer : IWebServer, IFileProvider 18 public class CoreOwinWebServer : IWebServer, IFileProvider
17 { 19 {
20 const string StaticFileBasePath = "/e2e";
21
18 private Dictionary<string, string> PhysicalPathsByRelativeUrl { get; } = new Dictionary<string, string>(); 22 private Dictionary<string, string> PhysicalPathsByRelativeUrl { get; } = new Dictionary<string, string>();
19 23
20 private IHost WebHost { get; set; } 24 private IHost WebHost { get; set; }
21 25
22 public bool DisableHeadResponses { get; set; } 26 public bool DisableHeadResponses { get; set; }
27 public bool DisableRangeRequests { get; set; }
23 28
24 public void AddFiles(Dictionary<string, string> physicalPathsByRelativeUrl) 29 public void AddFiles(Dictionary<string, string> physicalPathsByRelativeUrl)
25 { 30 {
@@ -38,10 +43,11 @@ namespace WixToolsetTest.BurnE2E
38 webBuilder.UseUrls("http://localhost:9999"); 43 webBuilder.UseUrls("http://localhost:9999");
39 webBuilder.Configure(appBuilder => 44 webBuilder.Configure(appBuilder =>
40 { 45 {
46 appBuilder.Use(this.CustomStaticFileMiddleware);
41 appBuilder.UseStaticFiles(new StaticFileOptions 47 appBuilder.UseStaticFiles(new StaticFileOptions
42 { 48 {
43 FileProvider = this, 49 FileProvider = this,
44 RequestPath = "/e2e", 50 RequestPath = StaticFileBasePath,
45 ServeUnknownFileTypes = true, 51 ServeUnknownFileTypes = true,
46 OnPrepareResponse = this.OnPrepareStaticFileResponse, 52 OnPrepareResponse = this.OnPrepareStaticFileResponse,
47 }); 53 });
@@ -51,6 +57,42 @@ namespace WixToolsetTest.BurnE2E
51 this.WebHost.Start(); 57 this.WebHost.Start();
52 } 58 }
53 59
60 private async Task CustomStaticFileMiddleware(HttpContext context, Func<Task> next)
61 {
62 if (!this.DisableRangeRequests || (!HttpMethods.IsGet(context.Request.Method) && !HttpMethods.IsHead(context.Request.Method)))
63 {
64 await next();
65 return;
66 }
67
68 // Only send Content-Length header.
69 // Don't support range requests.
70 // https://github.com/dotnet/aspnetcore/blob/60abfafe32a4692f9dc4a172665524f163b10012/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs
71 if (!context.Request.Path.StartsWithSegments(StaticFileBasePath, out var subpath))
72 {
73 context.Response.StatusCode = 404;
74 return;
75 }
76
77 var fileInfo = this.GetFileInfo(subpath);
78 if (!fileInfo.Exists)
79 {
80 context.Response.StatusCode = 404;
81 return;
82 }
83
84 var responseHeaders = context.Response.GetTypedHeaders();
85 var fileLength = fileInfo.Length;
86 responseHeaders.ContentLength = fileLength;
87
88 this.OnPrepareStaticFileResponse(new StaticFileResponseContext(context, fileInfo));
89
90 if (HttpMethods.IsGet(context.Request.Method))
91 {
92 await context.Response.SendFileAsync(fileInfo, 0, fileLength);
93 }
94 }
95
54 private void OnPrepareStaticFileResponse(StaticFileResponseContext obj) 96 private void OnPrepareStaticFileResponse(StaticFileResponseContext obj)
55 { 97 {
56 if (this.DisableHeadResponses && obj.Context.Request.Method == "HEAD") 98 if (this.DisableHeadResponses && obj.Context.Request.Method == "HEAD")