-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
WebServerStartup.cs
173 lines (158 loc) · 6.54 KB
/
WebServerStartup.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
#nullable enable
namespace Microsoft.WebAssembly.AppHost;
internal sealed class WebServerStartup
{
private readonly IWebHostEnvironment _hostingEnvironment;
private static readonly object LaunchLock = new object();
private static string LaunchedDebugProxyUrl = "";
private ILogger? _logger;
public WebServerStartup(IWebHostEnvironment hostingEnvironment) => _hostingEnvironment = hostingEnvironment;
public static int StartDebugProxy(string devToolsHost)
{
//we need to start another process, otherwise it will be running the BrowserDebugProxy in the same process that will be debugged, so pausing in a breakpoint
//on managed code will freeze because it will not be able to continue executing the BrowserDebugProxy to get the locals value
var executablePath = Path.Combine(System.AppContext.BaseDirectory, "BrowserDebugHost.dll");
var ownerPid = Environment.ProcessId;
// generate a random port in a given range, skipping the ports blocked by browsers: https://chromestatus.com/feature/5064283639513088
var generateRandomPort = GetNextRandomExcept(5000..5300,
5060, // SIP
5061 // SIPS
);
var processStartInfo = new ProcessStartInfo
{
FileName = "dotnet" + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : ""),
Arguments = $"exec \"{executablePath}\" --OwnerPid {ownerPid} --DevToolsUrl {devToolsHost} --DevToolsProxyPort {generateRandomPort}",
UseShellExecute = false,
RedirectStandardOutput = true,
};
var debugProxyProcess = Process.Start(processStartInfo);
if (debugProxyProcess is null)
{
throw new InvalidOperationException("Unable to start debug proxy process.");
}
return generateRandomPort;
static int GetNextRandomExcept(Range range, params int[] except)
{
int current;
do
{
current = Random.Shared.Next(range.Start.Value, range.End.Value);
} while (Array.IndexOf(except, current) > -1);
return current;
}
}
public void Configure(IApplicationBuilder app,
IOptions<WebServerOptions> optionsContainer,
TaskCompletionSource<ServerURLs> realUrlsAvailableTcs,
ILogger logger,
IHostApplicationLifetime applicationLifetime)
{
_logger = logger;
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".wasm"] = "application/wasm";
provider.Mappings[".cjs"] = "text/javascript";
provider.Mappings[".mjs"] = "text/javascript";
foreach (string extn in new string[] { ".dll", ".pdb", ".dat", ".webcil" })
{
provider.Mappings[extn] = "application/octet-stream";
}
WebServerOptions options = optionsContainer.Value;
if (options.WebServerUseCrossOriginPolicy)
{
app.Use((context, next) =>
{
context.Response.Headers.Append("Cross-Origin-Embedder-Policy", "require-corp");
context.Response.Headers.Append("Cross-Origin-Opener-Policy", "same-origin");
return next();
});
}
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(_hostingEnvironment.ContentRootPath),
ContentTypeProvider = provider,
ServeUnknownFileTypes = true
});
if (options.WebServerUseCors)
{
app.UseCors("AnyCors");
}
app.UseRouting();
app.UseWebSockets();
if (options.OnConsoleConnected is not null)
{
app.UseRouter(router =>
{
router.MapGet("/console", async context =>
{
if (!context.WebSockets.IsWebSocketRequest)
{
context.Response.StatusCode = 400;
return;
}
using WebSocket socket = await context.WebSockets.AcceptWebSocketAsync();
await options.OnConsoleConnected(socket);
});
});
}
app.Map("/debug", app =>
{
app.Run(async (context) =>
{
//debug from VS
var queryParams = HttpUtility.ParseQueryString(context.Request.QueryString.Value!);
var browserParam = queryParams.Get("browser");
Uri? browserUrl = null;
var devToolsHost = "http://localhost:9222";
if (browserParam != null)
{
browserUrl = new Uri(browserParam);
devToolsHost = $"http://{browserUrl.Host}:{browserUrl.Port}";
}
lock (LaunchLock)
{
if (LaunchedDebugProxyUrl == "")
{
LaunchedDebugProxyUrl = $"http://localhost:{StartDebugProxy(devToolsHost)}";
}
}
var requestPath = context.Request.Path.ToString();
if (requestPath == string.Empty)
{
requestPath = "/";
}
context.Response.Redirect($"{LaunchedDebugProxyUrl}{browserUrl!.PathAndQuery}");
await Task.FromResult(0);
});
});
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", context =>
{
context.Response.Redirect("index.html", permanent: false);
return Task.CompletedTask;
});
});
ServerURLsProvider.ResolveServerUrlsOnApplicationStarted(app, logger, applicationLifetime, realUrlsAvailableTcs);
}
}