-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
BrowserHost.cs
276 lines (231 loc) · 11.2 KB
/
BrowserHost.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 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;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.WebAssembly.AppHost.DevServer;
using Microsoft.WebAssembly.Diagnostics;
#nullable enable
namespace Microsoft.WebAssembly.AppHost;
internal sealed class BrowserHost
{
private readonly ILogger _logger;
private readonly BrowserArguments _args;
public BrowserHost(BrowserArguments args, ILogger logger)
{
_logger = logger;
_args = args;
}
public static async Task<int> InvokeAsync(CommonConfiguration commonArgs,
ILoggerFactory loggerFactory,
ILogger logger,
CancellationToken token)
{
var args = new BrowserArguments(commonArgs);
args.Validate();
var host = new BrowserHost(args, logger);
await host.RunAsync(loggerFactory, token);
return 0;
}
private async Task RunAsync(ILoggerFactory loggerFactory, CancellationToken token)
{
if (_args.CommonConfig.Debugging && !_args.CommonConfig.UseStaticWebAssets)
{
ProxyOptions options = _args.CommonConfig.ToProxyOptions();
_ = Task.Run(() => DebugProxyHost.RunDebugProxyAsync(options, Array.Empty<string>(), loggerFactory, token), token)
.ConfigureAwait(false);
}
Dictionary<string, string> envVars = new();
if (_args.CommonConfig.HostProperties.EnvironmentVariables is not null)
{
foreach (KeyValuePair<string, string> kvp in _args.CommonConfig.HostProperties.EnvironmentVariables)
envVars[kvp.Key] = kvp.Value;
}
var runArgsJson = new RunArgumentsJson(applicationArguments: _args.AppArgs,
runtimeArguments: _args.CommonConfig.RuntimeArguments,
environmentVariables: envVars,
forwardConsoleToWS: _args.ForwardConsoleOutput ?? false,
debugging: _args.CommonConfig.Debugging);
runArgsJson.Save(Path.Combine(_args.CommonConfig.AppPath, "runArgs.json"));
// Read system environment variables after runArgs.json is saved, so they won't be passed to browser.
// But we need them to correctly read ASPNETCORE_URLS
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
if (de.Key is not null && de.Value is not null)
envVars[(string)de.Key] = (string)de.Value;
}
string[] urls = (envVars.TryGetValue("ASPNETCORE_URLS", out string? aspnetUrls) && aspnetUrls.Length > 0)
? aspnetUrls.Split(';', StringSplitOptions.RemoveEmptyEntries)
: new string[] { $"http://127.0.0.1:{_args.CommonConfig.HostProperties.WebServerPort}", "https://127.0.0.1:0" };
(ServerURLs serverURLs, IWebHost host) = await StartWebServerAsync(_args,
urls,
token);
string[] fullUrls = BuildUrls(serverURLs, _args.AppArgs);
Console.WriteLine();
foreach (string url in fullUrls)
Console.WriteLine($"App url: {url}");
if (serverURLs.DebugPath != null)
{
Console.WriteLine($"Debug at url: {BuildUrl(serverURLs.Http, serverURLs.DebugPath, string.Empty)}");
if (serverURLs.Https != null)
Console.WriteLine($"Debug at url: {BuildUrl(serverURLs.Https, serverURLs.DebugPath, string.Empty)}");
}
await host.WaitForShutdownAsync(token);
}
private async Task<(ServerURLs, IWebHost)> StartWebServerAsync(BrowserArguments args, string[] urls, CancellationToken token)
{
Func<WebSocket, Task>? onConsoleConnected = null;
if (args.ForwardConsoleOutput ?? false)
{
WasmTestMessagesProcessor logProcessor = new(_logger);
onConsoleConnected = socket => RunConsoleMessagesPump(socket, logProcessor!, token);
}
// If we are using new browser template, use dev server
if (args.CommonConfig.UseStaticWebAssets)
{
DevServerOptions devServerOptions = CreateDevServerOptions(args, urls, onConsoleConnected);
return await DevServer.DevServer.StartAsync(devServerOptions, _logger, token);
}
// Otherwise for old template, use web server
WebServerOptions webServerOptions = CreateWebServerOptions(urls, args.CommonConfig.AppPath, onConsoleConnected);
return await WebServer.StartAsync(webServerOptions, _logger, token);
}
private static WebServerOptions CreateWebServerOptions(string[] urls, string appPath, Func<WebSocket, Task>? onConsoleConnected) => new
(
OnConsoleConnected: onConsoleConnected,
ContentRootPath: Path.GetFullPath(appPath),
WebServerUseCors: true,
WebServerUseCrossOriginPolicy: true,
Urls: urls
);
private static DevServerOptions CreateDevServerOptions(BrowserArguments args, string[] urls, Func<WebSocket, Task>? onConsoleConnected)
{
const string staticWebAssetsV1Extension = ".StaticWebAssets.xml";
const string staticWebAssetsV2Extension = ".staticwebassets.runtime.json";
DevServerOptions? devServerOptions = null;
string appPath = args.CommonConfig.AppPath;
if (args.CommonConfig.HostProperties.MainAssembly != null)
{
// If we have main assembly name, try to find static web assets manifest by precise name.
var mainAssemblyPath = Path.Combine(appPath, args.CommonConfig.HostProperties.MainAssembly);
var endpointsManifest = Path.ChangeExtension(mainAssemblyPath, ".staticwebassets.endpoints.json");
var staticWebAssetsPath = Path.ChangeExtension(mainAssemblyPath, staticWebAssetsV2Extension);
if (File.Exists(staticWebAssetsPath))
{
devServerOptions = CreateDevServerOptions(urls, staticWebAssetsPath, endpointsManifest, onConsoleConnected);
}
else
{
staticWebAssetsPath = Path.ChangeExtension(mainAssemblyPath, staticWebAssetsV1Extension);
if (File.Exists(staticWebAssetsPath))
devServerOptions = CreateDevServerOptions(urls, staticWebAssetsPath, endpointsManifest, onConsoleConnected);
}
if (devServerOptions == null)
devServerOptions = CreateDevServerOptions(urls, mainAssemblyPath, endpointsManifest, onConsoleConnected);
}
else
{
// If we don't have main assembly name, try to find static web assets manifest by search in the directory.
var staticWebAssetsPath = FindFirstFileWithExtension(appPath, staticWebAssetsV2Extension)
?? FindFirstFileWithExtension(appPath, staticWebAssetsV1Extension);
var endpointsManifest = FindFirstFileWithExtension(appPath, ".staticwebassets.endpoints.json");
if (staticWebAssetsPath != null)
devServerOptions = CreateDevServerOptions(urls, staticWebAssetsPath, endpointsManifest, onConsoleConnected);
if (devServerOptions == null)
throw new CommandLineException($"Please, provide mainAssembly in hostProperties of runtimeconfig. Alternatively leave the static web assets manifest ('*{staticWebAssetsV2Extension}') in the build output directory '{appPath}' .");
}
return devServerOptions;
}
private static DevServerOptions CreateDevServerOptions(string[] urls, string staticWebAssetsPath, string? endpointsManifest, Func<WebSocket, Task>? onConsoleConnected) => new
(
OnConsoleConnected: onConsoleConnected,
StaticWebAssetsPath: staticWebAssetsPath,
StaticWebAssetsEndpointsPath: endpointsManifest,
WebServerUseCors: true,
WebServerUseCrossOriginPolicy: true,
Urls: urls
);
private static string? FindFirstFileWithExtension(string directory, string extension)
=> Directory.EnumerateFiles(directory, "*" + extension).FirstOrDefault();
private async Task RunConsoleMessagesPump(WebSocket socket, WasmTestMessagesProcessor messagesProcessor, CancellationToken token)
{
byte[] buff = new byte[4000];
var mem = new MemoryStream();
try
{
while (!token.IsCancellationRequested)
{
if (socket.State != WebSocketState.Open)
{
_logger.LogError($"Console websocket is no longer open");
break;
}
WebSocketReceiveResult result = await socket.ReceiveAsync(new ArraySegment<byte>(buff), token).ConfigureAwait(false);
if (result.MessageType == WebSocketMessageType.Close)
{
// tcs.SetResult(false);
return;
}
mem.Write(buff, 0, result.Count);
if (result.EndOfMessage)
{
string? line = Encoding.UTF8.GetString(mem.GetBuffer(), 0, (int)mem.Length);
line += Environment.NewLine;
messagesProcessor.Invoke(line);
mem.SetLength(0);
mem.Seek(0, SeekOrigin.Begin);
}
}
}
catch (OperationCanceledException oce)
{
if (!token.IsCancellationRequested)
_logger.LogDebug($"RunConsoleMessagesPump cancelled: {oce}");
}
catch (Exception ex)
{
_logger.LogError($"Console pump failed: {ex}");
throw;
}
}
private string[] BuildUrls(ServerURLs serverURLs, IEnumerable<string> passThroughArguments)
{
var sb = new StringBuilder();
foreach (string arg in passThroughArguments)
{
if (sb.Length > 0)
sb.Append('&');
sb.Append($"arg={HttpUtility.UrlEncode(arg)}");
}
string query = sb.ToString();
string? filename = _args.HTMLPath != null ? Path.GetFileName(_args.HTMLPath) : null;
string httpUrl = BuildUrl(serverURLs.Http, filename, query);
return string.IsNullOrEmpty(serverURLs.Https)
? (new[] { httpUrl })
: (new[]
{
httpUrl,
BuildUrl(serverURLs.Https!, filename, query)
});
}
private static string BuildUrl(string baseUrl, string? htmlFileName, string query)
{
var uriBuilder = new UriBuilder(baseUrl)
{
Query = query
};
if (htmlFileName != null)
uriBuilder.Path = htmlFileName;
return uriBuilder.ToString();
}
}