1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolsetTest.BurnE2E
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
public class CoreOwinWebServer : IWebServer, IFileProvider
{
const string StaticFileBasePath = "/e2e";
private Dictionary<string, string> PhysicalPathsByRelativeUrl { get; } = new Dictionary<string, string>();
private IHost WebHost { get; set; }
public bool DisableHeadResponses { get; set; }
public bool DisableRangeRequests { get; set; }
public void AddFiles(Dictionary<string, string> physicalPathsByRelativeUrl)
{
foreach (var kvp in physicalPathsByRelativeUrl)
{
this.PhysicalPathsByRelativeUrl.Add(kvp.Key, kvp.Value);
}
}
public void Start()
{
this.WebHost = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
// Use localhost instead of * to avoid firewall issues.
webBuilder.UseUrls("http://localhost:9999");
webBuilder.Configure(appBuilder =>
{
appBuilder.Use(this.CustomStaticFileMiddleware);
appBuilder.UseStaticFiles(new StaticFileOptions
{
FileProvider = this,
RequestPath = StaticFileBasePath,
ServeUnknownFileTypes = true,
OnPrepareResponse = this.OnPrepareStaticFileResponse,
});
});
})
.Build();
this.WebHost.Start();
}
private async Task CustomStaticFileMiddleware(HttpContext context, Func<Task> next)
{
if (!this.DisableRangeRequests || (!HttpMethods.IsGet(context.Request.Method) && !HttpMethods.IsHead(context.Request.Method)))
{
await next();
return;
}
// Only send Content-Length header.
// Don't support range requests.
// https://github.com/dotnet/aspnetcore/blob/60abfafe32a4692f9dc4a172665524f163b10012/src/Middleware/StaticFiles/src/StaticFileMiddleware.cs
if (!context.Request.Path.StartsWithSegments(StaticFileBasePath, out var subpath))
{
context.Response.StatusCode = 404;
return;
}
var fileInfo = this.GetFileInfo(subpath);
if (!fileInfo.Exists)
{
context.Response.StatusCode = 404;
return;
}
var responseHeaders = context.Response.GetTypedHeaders();
var fileLength = fileInfo.Length;
responseHeaders.ContentLength = fileLength;
this.OnPrepareStaticFileResponse(new StaticFileResponseContext(context, fileInfo));
if (HttpMethods.IsGet(context.Request.Method))
{
await context.Response.SendFileAsync(fileInfo, 0, fileLength);
}
}
private void OnPrepareStaticFileResponse(StaticFileResponseContext obj)
{
if (this.DisableHeadResponses && obj.Context.Request.Method == "HEAD")
{
obj.Context.Response.StatusCode = 404;
}
}
public void Dispose()
{
var waitTime = TimeSpan.FromSeconds(5);
this.WebHost?.StopAsync(waitTime).Wait(waitTime);
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
throw new NotImplementedException();
}
public IFileInfo GetFileInfo(string subpath)
{
if (this.PhysicalPathsByRelativeUrl.TryGetValue(subpath, out var filepath))
{
return new PhysicalFileInfo(new FileInfo(filepath));
}
return new NotFoundFileInfo(subpath);
}
public IChangeToken Watch(string filter)
{
throw new NotImplementedException();
}
}
}
|