|
| 1 | +// <copyright file="AppServer.cs" company="Selenium Committers"> |
| 2 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The SFC licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// </copyright> |
| 19 | + |
| 20 | +using System; |
| 21 | +using System.Collections.Concurrent; |
| 22 | +using System.IO; |
| 23 | +using System.Linq; |
| 24 | +using System.Net; |
| 25 | +using System.Security.Cryptography; |
| 26 | +using System.Security.Cryptography.X509Certificates; |
| 27 | +using System.Threading.Tasks; |
| 28 | +using Microsoft.AspNetCore.Builder; |
| 29 | +using Microsoft.AspNetCore.Hosting; |
| 30 | +using Microsoft.AspNetCore.Http; |
| 31 | +using Microsoft.AspNetCore.Routing; |
| 32 | +using Microsoft.Extensions.DependencyInjection; |
| 33 | +using Microsoft.Extensions.FileProviders; |
| 34 | +using OpenQA.Selenium.Testing.WebServer.Handlers; |
| 35 | + |
| 36 | +namespace OpenQA.Selenium.Testing.WebServer; |
| 37 | + |
| 38 | +public class AppServer : IAsyncDisposable |
| 39 | +{ |
| 40 | + private WebApplication? _app; |
| 41 | + private readonly string _webContentRoot = FindWebContentRoot(); |
| 42 | + private readonly ConcurrentDictionary<string, string> _pages = new(); |
| 43 | + |
| 44 | + public async Task<(string HttpUrl, string HttpsUrl)> StartAsync() |
| 45 | + { |
| 46 | + var builder = WebApplication.CreateSlimBuilder(); |
| 47 | + |
| 48 | + var certificate = GenerateSelfSignedCertificate(); |
| 49 | + |
| 50 | + builder.WebHost.ConfigureKestrel(options => |
| 51 | + { |
| 52 | + options.Listen(IPAddress.Loopback, 0); |
| 53 | + options.Listen(IPAddress.Loopback, 0, listenOptions => |
| 54 | + { |
| 55 | + listenOptions.UseHttps(certificate); |
| 56 | + }); |
| 57 | + }); |
| 58 | + builder.Services.AddDirectoryBrowser(); |
| 59 | + |
| 60 | + _app = builder.Build(); |
| 61 | + |
| 62 | + MapEndpoints(_app); |
| 63 | + MapEndpoints(_app.MapGroup("/common")); |
| 64 | + |
| 65 | + if (Directory.Exists(_webContentRoot)) |
| 66 | + { |
| 67 | + var fileProvider = new PhysicalFileProvider(_webContentRoot); |
| 68 | + |
| 69 | + _app.UseStaticFiles(new StaticFileOptions |
| 70 | + { |
| 71 | + FileProvider = fileProvider, |
| 72 | + ServeUnknownFileTypes = true |
| 73 | + }); |
| 74 | + |
| 75 | + _app.UseStaticFiles(new StaticFileOptions |
| 76 | + { |
| 77 | + FileProvider = fileProvider, |
| 78 | + RequestPath = "/common", |
| 79 | + ServeUnknownFileTypes = true |
| 80 | + }); |
| 81 | + |
| 82 | + _app.UseDirectoryBrowser(new DirectoryBrowserOptions |
| 83 | + { |
| 84 | + FileProvider = fileProvider |
| 85 | + }); |
| 86 | + |
| 87 | + _app.UseDirectoryBrowser(new DirectoryBrowserOptions |
| 88 | + { |
| 89 | + FileProvider = fileProvider, |
| 90 | + RequestPath = "/common" |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + await _app.StartAsync(); |
| 95 | + |
| 96 | + int httpPort = new Uri(_app.Urls.First(u => u.StartsWith("http://"))).Port; |
| 97 | + int httpsPort = new Uri(_app.Urls.First(u => u.StartsWith("https://"))).Port; |
| 98 | + |
| 99 | + return ($"http://localhost:{httpPort}", $"https://localhost:{httpsPort}"); |
| 100 | + } |
| 101 | + |
| 102 | + public async Task StopAsync() |
| 103 | + { |
| 104 | + if (_app is not null) |
| 105 | + { |
| 106 | + await _app.StopAsync(); |
| 107 | + await _app.DisposeAsync(); |
| 108 | + _app = null; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public async ValueTask DisposeAsync() |
| 113 | + { |
| 114 | + await StopAsync(); |
| 115 | + } |
| 116 | + |
| 117 | + private void MapEndpoints(IEndpointRouteBuilder endpoints) |
| 118 | + { |
| 119 | + endpoints.MapGet("/basicAuth", BasicAuthHandler.Handle); |
| 120 | + endpoints.MapGet("/echo", (Delegate)EchoHandler.Handle); |
| 121 | + endpoints.MapGet("/cookie", CookieHandler.Handle); |
| 122 | + endpoints.MapGet("/encoding", EncodingHandler.Handle); |
| 123 | + endpoints.MapGet("/sleep", (Delegate)SleepHandler.Handle); |
| 124 | + endpoints.MapGet("/redirect", RedirectHandler.Handle); |
| 125 | + endpoints.MapGet("/page/{pageNumber}", PageHandler.Handle); |
| 126 | + endpoints.MapGet("/utf8/{*path}", (HttpContext context, string path) => Utf8Handler.Handle(context, path, _webContentRoot)); |
| 127 | + endpoints.MapPost("/createPage", (Delegate)((HttpContext context) => CreatePageHandler.Handle(context, _pages))); |
| 128 | + endpoints.MapPost("/upload", (Delegate)UploadHandler.Handle); |
| 129 | + |
| 130 | + endpoints.MapGet("/.well-known/web-identity", (HttpContext context) => FedCmHandler.HandleWebIdentity(context)); |
| 131 | + endpoints.MapGet("/fedcm/config.json", (HttpContext context) => FedCmHandler.HandleConfig(context)); |
| 132 | + endpoints.MapPost("/fedcm/id_assertion.json", (HttpContext context) => FedCmHandler.HandleIdAssertion(context)); |
| 133 | + |
| 134 | + endpoints.MapGet("/temp/{fileName}", (string fileName) => CreatePageHandler.ServePage(fileName, _pages)); |
| 135 | + } |
| 136 | + |
| 137 | + private static X509Certificate2 GenerateSelfSignedCertificate() |
| 138 | + { |
| 139 | + using var ecdsa = ECDsa.Create(); |
| 140 | + var request = new CertificateRequest("CN=localhost", ecdsa, HashAlgorithmName.SHA256); |
| 141 | + return request.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(1)); |
| 142 | + } |
| 143 | + |
| 144 | + private static string FindWebContentRoot() |
| 145 | + { |
| 146 | + var info = new DirectoryInfo(AppContext.BaseDirectory); |
| 147 | + while (info is not null && info != info.Root) |
| 148 | + { |
| 149 | + string webPath = Path.Combine(info.FullName, "common", "src", "web"); |
| 150 | + if (Directory.Exists(webPath)) |
| 151 | + { |
| 152 | + return webPath; |
| 153 | + } |
| 154 | + info = info.Parent; |
| 155 | + } |
| 156 | + |
| 157 | + return string.Empty; |
| 158 | + } |
| 159 | +} |
0 commit comments