protected override async Task CleanupImplAsync(Exception?ex) { if (HostTask != null) { await HostTask; } GlobalObjectExchange.TryWithdraw(ParamToken, out _); GlobalObjectExchange.TryWithdraw(CancelToken, out _); }
public HttpServerImplementation(IConfiguration configuration) { this.Configuration = configuration; this.Param = GlobalObjectExchange.Withdraw(this.Configuration["coreutil_param_token"]); this.CancelToken = (CancellationToken)GlobalObjectExchange.Withdraw(this.Configuration["coreutil_cancel_token"]); this.builder_config = this.Configuration["coreutil_ServerBuilderConfig"].JsonToObject <HttpServerBuilderConfig>(); this.startup_config = new HttpServerStartupConfig(); }
public HttpServerStartupHelper(IConfiguration configuration) { this.Configuration = configuration; this.Param = GlobalObjectExchange.Withdraw(this.Configuration["coreutil_param_token"]); this.CancelToken = (CancellationToken)GlobalObjectExchange.Withdraw(this.Configuration["coreutil_cancel_token"]) !; this.ServerOptions = this.Configuration["coreutil_ServerBuilderConfig"]._JsonToObject <HttpServerOptions>() !; this.StartupConfig = new HttpServerStartupConfig(); if (this.ServerOptions.DisableHiveBasedSetting == false) { Hive.LocalAppSettingsEx[this.ServerOptions.HiveName].AccessData(true, k => { this.IsDevelopmentMode = k.GetBool("IsDevelopmentMode", false); if (this.ServerOptions.UseSimpleBasicAuthentication || this.ServerOptions.HoldSimpleBasicAuthenticationDatabase) { k.Get("SimpleBasicAuthDatabase", new HttpServerSimpleBasicAuthDatabase(EnsureSpecial.Yes)); SimpleBasicAuthenticationPasswordValidator = async(username, password) => { bool ok = false; Hive.LocalAppSettingsEx[this.ServerOptions.HiveName].AccessData(false, k2 => { HttpServerSimpleBasicAuthDatabase?db = k2.Get <HttpServerSimpleBasicAuthDatabase>("SimpleBasicAuthDatabase"); ok = db?.Authenticate(username, password) ?? false; }); await Task.CompletedTask; return(ok); }; } }); } }
public HttpServer(HttpServerOptions options, object?param = null, CancellationToken cancel = default) : base(cancel) { try { this.Options = options; bool isDevelopmentMode = false; if (this.Options.DisableHiveBasedSetting == false) { Hive.LocalAppSettingsEx[this.Options.HiveName].AccessData(true, k => { isDevelopmentMode = k.GetBool("IsDevelopmentMode", false); // Update options with the config file string httpPortsList = k.GetStr("HttpPorts", Str.PortsListToStr(Options.HttpPortsList)); Options.HttpPortsList = Str.ParsePortsList(httpPortsList).ToList(); string httpsPortsList = k.GetStr("HttpsPorts", Str.PortsListToStr(Options.HttpsPortsList)); Options.HttpsPortsList = Str.ParsePortsList(httpsPortsList).ToList(); Options.LocalHostOnly = k.GetBool("LocalHostOnly", Options.LocalHostOnly); Options.IPv4Only = k.GetBool("IPv4Only", Options.IPv4Only); string mustIncludeHostnameStr = k.GetStr("MustIncludeHostnameList", ""); string[] tokens = mustIncludeHostnameStr.Split(new char[] { ' ', ' ', ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries); tokens._DoForEach(x => Options.MustIncludeHostnameStrList.Add(x)); }); } Lfs.CreateDirectory(Options.WwwRoot); Lfs.CreateDirectory(Options.ContentsRoot); ParamToken = GlobalObjectExchange.Deposit(param); CancelToken = GlobalObjectExchange.Deposit(this.GrandCancel); try { var dict = new Dictionary <string, string> { { "coreutil_ServerBuilderConfig", this.Options._ObjectToJson() }, { "coreutil_param_token", ParamToken }, { "coreutil_cancel_token", CancelToken }, }; IConfiguration iconf = new ConfigurationBuilder() .AddInMemoryCollection(dict) .Build(); var host = Options.GetWebHostBuilder <THttpServerBuilder>(param) .UseConfiguration(iconf) .CaptureStartupErrors(true) .UseSetting("detailedErrors", isDevelopmentMode.ToString()) .Build(); HostTask = host.RunAsync(this.CancelWatcher.CancelToken); } catch { GlobalObjectExchange.TryWithdraw(ParamToken, out _); ParamToken = null; GlobalObjectExchange.TryWithdraw(CancelToken, out _); CancelToken = null; throw; } } catch { this._DisposeSafe(); throw; } }
public HttpServer(HttpServerBuilderConfig cfg, object param) { this.config = cfg; IO.MakeDirIfNotExists(config.ContentsRoot); string param_token = GlobalObjectExchange.Deposit(param); string cancel_token = GlobalObjectExchange.Deposit(cancel.Token); try { var dict = new Dictionary <string, string> { { "coreutil_ServerBuilderConfig", this.config.ObjectToJson() }, { "coreutil_param_token", param_token }, { "coreutil_cancel_token", cancel_token }, }; IConfiguration iconf = new ConfigurationBuilder() .AddInMemoryCollection(dict) .Build(); var h = new WebHostBuilder() .UseKestrel(opt => { if (config.LocalHostOnly) { foreach (int port in config.HttpPortsList) { opt.ListenLocalhost(port); } foreach (int port in config.HttpsPortsList) { opt.ListenLocalhost(port, lo => { lo.UseHttps(so => { so.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12; }); }); } } else if (config.IPv4Only) { foreach (int port in config.HttpPortsList) { opt.Listen(IPAddress.Any, port); } foreach (int port in config.HttpsPortsList) { opt.Listen(IPAddress.Any, port, lo => { lo.UseHttps(so => { so.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12; }); }); } } else { foreach (int port in config.HttpPortsList) { opt.ListenAnyIP(port); } foreach (int port in config.HttpsPortsList) { opt.ListenAnyIP(port, lo => { lo.UseHttps(so => { so.SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12; }); }); } } }) .UseWebRoot(config.ContentsRoot) .UseContentRoot(config.ContentsRoot) .ConfigureAppConfiguration((hostingContext, config) => { }) .ConfigureLogging((hostingContext, logging) => { if (config.DebugToConsole) { logging.AddConsole(); logging.AddDebug(); } }) .UseConfiguration(iconf) .UseStartup <THttpServerStartup>() .Build(); hosttask = h.RunAsync(cancel.Token); } catch { GlobalObjectExchange.TryWithdraw(param_token); GlobalObjectExchange.TryWithdraw(cancel_token); throw; } }