-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebBrowserUtilities.cs
337 lines (203 loc) · 11.8 KB
/
WebBrowserUtilities.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using Gsemac.IO;
using Gsemac.Net.Extensions;
using Gsemac.Net.Http.Extensions;
using Gsemac.Net.Sockets;
using Gsemac.Net.WebBrowsers.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace Gsemac.Net.WebBrowsers {
public static class WebBrowserUtilities {
// Public members
public static WebHeaderCollection GetBrowserRequestHeaders(IWebBrowserInfo webBrowserInfo) {
return GetBrowserRequestHeaders(webBrowserInfo, TimeSpan.FromSeconds(10));
}
public static WebHeaderCollection GetBrowserRequestHeaders(IWebBrowserInfo webBrowserInfo, TimeSpan timeout) {
return GetBrowserRequestHeaders(webBrowserInfo, timeout, null);
}
public static WebHeaderCollection GetBrowserRequestHeaders(IWebBrowserInfo webBrowserInfo, TimeSpan timeout, string responseBody) {
WebHeaderCollection requestHeaders = new WebHeaderCollection();
if (webBrowserInfo is null)
throw new ArgumentNullException(nameof(webBrowserInfo));
if (!File.Exists(webBrowserInfo?.ExecutablePath))
throw new FileNotFoundException();
// Start an HTTP server on a random port.
Uri requestUri = new Uri($"http://localhost:{SocketUtilities.GetAvailablePort()}/");
using (HttpListener listener = new HttpListener()) {
listener.Prefixes.Add(requestUri.AbsoluteUri);
listener.Start();
// Open the HTTP server in the user's web browser.
Process.Start(webBrowserInfo.ExecutablePath, requestUri.AbsoluteUri);
// Wait for an incoming request.
HttpListenerContext context = listener.GetContext(timeout);
if (context is object) {
HttpListenerRequest request = context.Request;
if (request.HttpMethod.Equals("get", StringComparison.OrdinalIgnoreCase)) {
// The web browser requested our webpage, so copy the request headers.
context.Request.Headers.CopyTo(requestHeaders);
// Respond to the request.
using (HttpListenerResponse response = context.Response) {
StringBuilder responseBuilder = new StringBuilder();
responseBuilder.AppendLine("<!DOCTYPE html>");
responseBuilder.Append("<html>");
responseBuilder.Append("<body>");
responseBuilder.Append(responseBody ?? "This page may now be closed.");
responseBuilder.Append("</body>");
responseBuilder.Append("</html>");
byte[] responseBytes = Encoding.UTF8.GetBytes(responseBuilder.ToString());
// We need to be careful with how we handle our resources to ensure that HttpListener finishes responding before stopping.
// While calling "Close()" on the output stream may look redundant, it seems to help.
//m https://stackoverflow.com/a/72939272/5383169
response.ContentLength64 = responseBytes.Length;
response.KeepAlive = true;
using (MemoryStream contentStream = new MemoryStream(responseBytes))
using (Stream outputStream = response.OutputStream) {
contentStream.CopyTo(response.OutputStream);
outputStream.Close();
}
response.Close();
}
listener.Stop();
}
}
}
return requestHeaders;
}
public static string EscapeUriString(string stringToEscape) {
return EscapeUriString(stringToEscape, WebBrowserId.GoogleChrome);
}
public static string EscapeUriString(string stringToEscape, WebBrowserId webBrowserId) {
if (string.IsNullOrEmpty(stringToEscape))
return stringToEscape;
// Web browsers escape URLs differently compared to Uri.EscapeUriString.
// Certain reserved characters are never escaped, while others are escaped only contextually.
// Each browser has its own specific behavior.
// Non-ASCII characters are always escaped.
char[] globallyRestrictedChars = new[] { ' ', '<', '>', '"' };
char[] pathRestrictedChars = null;
char[] queryRestrictedChars = new[] { '\'' };
if (webBrowserId != WebBrowserId.Firefox) {
// The following applies to all Chromium-based browsers.
pathRestrictedChars = new[] { '|' };
}
stringToEscape = PercentEncodeChars(stringToEscape, globallyRestrictedChars, encodeNonAsciiChars: true);
stringToEscape = Regex.Replace(stringToEscape, @"^(?<scheme>[^:]+:\/\/)(?<authority>[^\/#]+)(?<path>\/[^#?]*)?(?<query>\?[^#]*)?(?<fragment>#.+)?$", m => {
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.Append(m.Groups["scheme"].Value);
resultBuilder.Append(m.Groups["authority"].Value);
resultBuilder.Append(PercentEncodeChars(m.Groups["path"].Value, pathRestrictedChars, false));
resultBuilder.Append(PercentEncodeChars(m.Groups["query"].Value, queryRestrictedChars, false));
resultBuilder.Append(m.Groups["fragment"].Value);
return resultBuilder.ToString();
});
return stringToEscape;
}
public static bool OpenUrl(Uri uri) {
if (uri is null)
throw new ArgumentNullException(nameof(uri));
return OpenUrl(uri, OpenUrlOptions.Default);
}
public static bool OpenUrl(Uri uri, IOpenUrlOptions options) {
if (uri is null)
throw new ArgumentNullException(nameof(uri));
if (options is null)
throw new ArgumentNullException(nameof(options));
return OpenUrl(uri.AbsoluteUri, options);
}
public static bool OpenUrl(string url) {
if (url is null)
throw new ArgumentNullException(nameof(url));
return OpenUrl(url, OpenUrlOptions.Default);
}
public static bool OpenUrl(string url, IOpenUrlOptions options) {
if (url is null)
throw new ArgumentNullException(nameof(url));
if (options is null)
throw new ArgumentNullException(nameof(options));
if (!PathUtilities.IsUrl(url))
throw new ArgumentException(ExceptionMessages.StringIsNotAValidUrl, nameof(url));
IWebBrowserInfo browserInfo = options.WebBrowser;
IWebBrowserProfile browserProfile = options.Profile;
if (browserInfo is null && options.WebBrowserId != WebBrowserId.Unknown)
browserInfo = WebBrowserInfoFactory.Default.GetWebBrowserInfo(options.WebBrowserId);
string browserExecutablePath = browserInfo?.ExecutablePath ?? "explorer.exe";
List<string> arguments = new List<string>();
if (browserInfo is object) {
switch (browserInfo.Id) {
case WebBrowserId.Brave:
case WebBrowserId.GoogleChrome:
case WebBrowserId.MicrosoftEdge:
// Open the URL in a Chromium-based web browser.
arguments.Add($"\"{url}\"");
if (!string.IsNullOrWhiteSpace(options.UserDataDirectoryPath))
arguments.Add($"--user-data-dir=\"{options.UserDataDirectoryPath}\"");
if (browserProfile is object)
arguments.Add($"--profile-directory=\"{browserProfile.Identifier}\"");
if (options.PrivateWindow)
arguments.Add(browserInfo.Id == WebBrowserId.MicrosoftEdge ? $"-inprivate" : "--incognito");
break;
case WebBrowserId.Firefox:
// Open the URL in Firefox.
if (options.PrivateWindow)
arguments.Add($"--private-window");
arguments.Add($"\"{url}\"");
if (browserProfile is object) {
arguments.Add($"-profile \"{browserProfile.DirectoryPath}\"");
}
else if (!string.IsNullOrWhiteSpace(options.UserDataDirectoryPath)) {
// Firefox takes a profile path directly instead of a user data path (i.e. "Profiles" path).
// If the given directory doesn't have any profile information, we'll generate it.
FirefoxUtilities.CreateFirefoxUserDataDirectory(options.UserDataDirectoryPath);
IWebBrowserProfilesReader profilesReader = new FirefoxProfilesReader(options.UserDataDirectoryPath);
IWebBrowserProfile profile = profilesReader.GetDefaultProfile() ??
profilesReader.GetProfiles().FirstOrDefault();
if (profile is object)
arguments.Add($"-profile \"{profile.DirectoryPath}\"");
}
break;
}
}
// Open the URL in the shell/without any special options.
if (!arguments.Any())
arguments.Add($"\"{url}\"");
using (Process process = new Process()) {
process.StartInfo = new ProcessStartInfo() {
FileName = browserExecutablePath,
Arguments = string.Join(" ", arguments),
};
bool processedStarted = process.Start();
try {
// We can't rely on the return value of Start() alone, because it only returns true if we started a new process.
// It's possible that we're opening a new tab in an existing process as well, so it's important to also check the Responding property.
return processedStarted ||
process.Responding;
}
catch (InvalidOperationException) {
// Accessing the Responding property will sometimes, nondeterministically, throw an exception (I'm not totally sure why).
// When this happens, we'll just return false to indicate that an error has occurred.
return false;
}
}
}
// Private members
private static string PercentEncodeChars(string stringToEscape, char[] chars, bool encodeNonAsciiChars) {
if (string.IsNullOrEmpty(stringToEscape))
return stringToEscape;
List<string> charPatterns = new List<string>(2);
if (chars is object)
charPatterns.Add($"[{string.Join(string.Empty, chars.Select(c => Regex.Escape(c.ToString())))}]");
if (encodeNonAsciiChars)
charPatterns.Add(@"[^\x00-\x7F]");
string pattern = string.Join("|", charPatterns);
return Regex.Replace(stringToEscape, pattern, m => {
byte[] bytes = Encoding.UTF8.GetBytes(m.Value);
return string.Join(string.Empty, bytes.Select(b => $"%{b:X2}"));
});
}
}
}