From 0419f315e5689e043f311203ab8e61f69f1ee1d6 Mon Sep 17 00:00:00 2001 From: vnugent Date: Sun, 4 Aug 2024 17:14:10 -0400 Subject: Squashed commit of the following: commit bf6085a67a25c0242e3f170c3e617a08498d9ad0 Author: vnugent Date: Sun Aug 4 16:58:09 2024 -0400 fix compression source tree and source package commit 4eb2cf913495e8a7c8c9ad3fceb3bff2a1b2a072 Author: vnugent Date: Sun Aug 4 15:49:27 2024 -0400 push to codeberg commit 0b69bc760f87efab73ca6efb454b30a3393be269 Author: vnugent Date: Sun Aug 4 15:36:26 2024 -0400 consolidate log config commit b8841c2218133bb8692e30cee0cfc719bfa5e9a0 Author: vnugent Date: Thu Aug 1 21:30:59 2024 -0400 update compression copyright data and add package.json commit 08020ccace1474e27702ad6575259e799ca56b63 Merge: 904560a ff1765d Author: vnugent Date: Thu Aug 1 21:26:47 2024 -0400 Merge branch 'develop' into slurp-webserver commit ff1765d3aa4d98fd223c47d16fca8e3e13a4d894 Author: vnugent Date: Thu Aug 1 21:26:05 2024 -0400 set trasnport manager setters private for debug commit 904560a7b5eafd7580fb0a03e778d1751e72a503 Author: vnugent Date: Thu Aug 1 21:13:04 2024 -0400 build(app): swallow vnlib.webserver into core & build updates commit 6af95e61212611908d39235222474d4038e10fcd Author: vnugent Date: Wed Jul 31 15:14:07 2024 -0400 ci: clean up heapapi header & better formalize taskfiles commit 52965ce8bb0b06f59b07c7f6b5a9de6bfbc22b40 Author: vnugent Date: Mon Jul 29 12:26:32 2024 -0400 move dev-init to module level for vnbuild module init commit e66b75642f654f623a3115bd3586d567d1554726 Merge: a4dacd2 7be5d66 Author: vnugent Date: Sun Jul 28 19:16:13 2024 -0400 Merge branch 'master' into develop commit a4dacd2909426bf628c1eee1253cc5c8a01e2691 Author: vnugent Date: Sat Jul 27 22:41:04 2024 -0400 package updates commit f836e09981866f5c9f2ae46990d11b186a7d12bb Author: vnugent Date: Wed Jul 24 19:15:54 2024 -0400 chore: Remove argon2 docs & optional tcp resuse commit b9b892ab2143b0ab92e4dcf0a8b043c5c6c17271 Author: vnugent Date: Sun Jul 21 20:57:01 2024 -0400 fix spelling Enqueue and deprecate mispelled version commit 21ffa816f18be4b765ad740ed5d93346ec3b1fda Author: vnugent Date: Sat Jul 20 19:44:31 2024 -0400 static arugment list parsing functions commit 85cd6793818a3edd0a963bb4829a960ee6b0e022 Author: vnugent Date: Mon Jul 15 18:58:06 2024 -0400 chore: Just some minor checks and adjustments commit abfb5761ee381b7e1e5342a5525ceca8c8fd81dd Author: vnugent Date: Thu Jul 4 23:57:37 2024 -0400 analyzer pass commit 4a96dbb924f2b5bf80293e4054f221efe67151dd Author: vnugent Date: Thu Jul 4 22:45:28 2024 -0400 package updates commit 38ad7d923fa8d9e463d4aaa8e35f021086a03f2d Author: vnugent Date: Thu Jul 4 16:20:48 2024 -0400 mimalloc merge upstream upgrades commit 981ba286e4793de95bf65e6588313411344c4d53 Author: vnugent Date: Thu Jul 4 16:04:03 2024 -0400 refactor: Refactor extensions with perf updates commit 6b8c67888731f7dd210acdb2b1160cdbdbe30d47 Author: vnugent Date: Fri Jun 28 15:48:22 2024 -0400 refactor: Update service stack to reflect new loading patterns commit 12391e9a207b60b41a074600fc2373ad3eb1c3ab Author: vnugent Date: Wed Jun 26 21:01:15 2024 -0400 feat(server): Server arch update, Memory struct access commit 92e182ceaf843f8d859d38faa8b2c0ff53207ff6 Author: vnugent Date: Fri Jun 21 16:02:34 2024 -0400 feat: Multi transport listeners commit ee3620b8168a42c8e571e853c751ad5999a9b907 Author: vnugent Date: Tue Jun 18 21:17:28 2024 -0400 feat: Add file path caching support commit ff0926be56fc6eafdce36411847d73bf4ce9f183 Author: vnugent Date: Sun Jun 16 13:08:31 2024 -0400 feat: Allow multiple plugin loading directories commit 07ddf6738d32127926d07b1366e56d2a2308b53b Author: vnugent Date: Sun Jun 16 01:12:07 2024 -0400 perf: Absolutely yuge perf boosts commit ff15c05a9c3e632c39f3889820fb7d889342b452 Author: vnugent Date: Fri Jun 14 14:16:24 2024 -0400 fix: Improper request buffer property assignment commit 7d2987f1d4048c30808a85798e32c99747f6cfe3 Author: vnugent Date: Thu Jun 13 21:57:34 2024 -0400 perf: Async pre-buffer to avoid sync buffer commit 75c1d0cbf9a5a7856c544671a45f1b4312ffe7ce Author: vnugent Date: Tue Jun 11 22:11:45 2024 -0400 feat: Add a default site adapater and interceptors commit a7c739b7db9a17622cee751fe0e8a10e4b84b48b Author: vnugent Date: Sun Jun 9 13:05:12 2024 -0400 chore: Package updated commit b4b506a4b6c7c1e90b5b0980e4cfe0460e7546a2 Author: vnugent Date: Sat Jun 8 21:54:52 2024 -0400 some minor touchups commit 2160510fcc22a8574b0090fd91ca29072f45ab59 Author: vnugent Date: Fri May 31 15:12:20 2024 -0400 refactor: Immutable tcp listeners commit 51cb4eb93e4f1b4c47d35b105e72af1fe771abcc Author: vnugent Date: Thu May 30 17:31:16 2024 -0400 refactor: minor non-breaking changes to VNEncoding commit 768ddc1eb949266d693f96c67d734e881bd59374 Merge: 9a835fe 1b590c2 Author: vnugent Date: Wed May 22 17:50:57 2024 -0400 Merge branch 'main' into develop commit 9a835fe12c9586ab8dd44d7c96fef4a2d6017e4b Author: vnugent Date: Fri May 17 18:27:03 2024 -0400 chore: Update mimmaloc v2.1.6, update fPIC & cleanup commit 3b7004b88acfc7f7baa3a8857a5a2f7cf3dd560e Author: vnugent Date: Fri May 17 16:03:28 2024 -0400 feat: Added ReadFileDataAsync function commit 9a964795757bf0da4dd7fcab15ad304f4ea3fdf1 Author: vnugent Date: Wed May 15 21:57:39 2024 -0400 refactor: Harden some argon2 password hashing commit 4035c838c1508af0aa7e767a97431a692958ce1c Author: vnugent Date: Sun May 12 16:55:32 2024 -0400 perf: Utils + http perf mods commit f4f0d4f74250257991c57bfae74c4852c7e1ae46 Author: vnugent Date: Thu May 2 15:22:53 2024 -0400 feat: Buff middleware handlers | | Added implicit support for middleware post processing of files before the filehandler closes the connection. Also cleaned up some project file stuff commit f0b7dca107659df1d7d4631fdbd2aae9d716d053 Merge: 8c4a45e 107b058 Author: vnugent Date: Sat Apr 20 12:24:05 2024 -0400 Merge branch 'main' into develop commit 8c4a45e384accf92b1b6d748530e8d46f7de40d6 Author: vnugent Date: Sat Apr 20 11:10:30 2024 -0400 refactor: Overhaul C libraries and fix builds commit 42ff77080d10b0fc9fecbbc46141e8e23a1d066a Author: vnugent Date: Sat Apr 20 00:45:57 2024 -0400 fix!: Middlware array, multiple cookie set, and cookie check commit 97e82b9d66f387f9e6d21d88ddc7a8ab8693149c Merge: 4ca5791 e07537a Author: vnugent Date: Tue Apr 2 13:34:22 2024 -0400 Merge branch 'main' into develop commit 4ca5791ed67b9834bdbd010206b30373e4705e9b Author: vnugent Date: Tue Apr 2 13:32:12 2024 -0400 fix: Missed ! on null pointer check commit 9b4036377c52200c6488c98180d69a0e63321f97 Author: vnugent Date: Tue Apr 2 13:22:29 2024 -0400 fix: Fix _In_ macro for compression public api commit 53a7b4b5c5b67b4a4e06e1d9098cac4bcd6afd7c Merge: 448a93b 21130c8 Author: vnugent Date: Sun Mar 31 17:01:15 2024 -0400 Merge branch 'main' into develop commit 448a93bb1d18d032087afe2476ffccb98634a54c Author: vnugent Date: Sun Mar 31 16:56:51 2024 -0400 ci: fix third-party dir cleanup commit 9afed1427472da1ea13079f98dbe27339e55ee7d Author: vnugent Date: Sun Mar 31 16:43:15 2024 -0400 perf: Deprecate unsafememoryhandle span extensions commit 3ff90da4f02af47ea6d233fdd4445337ebe36452 Author: vnugent Date: Sat Mar 30 21:36:18 2024 -0400 refactor: Updates, advanced tracing, http optimizations commit 8d6b79b5ae309b36f265ba81529bcef8bfcd7414 Merge: 6c1667b 5585915 Author: vnugent Date: Sun Mar 24 21:01:31 2024 -0400 Merge branch 'main' into develop commit 6c1667be23597513537f8190e2f55d65eb9b7c7a Author: vnugent Date: Fri Mar 22 12:01:53 2024 -0400 refactor: Overhauled native library loading and lazy init commit ebf688f2f974295beabf7b5def7e6f6f150551d0 Author: vnugent Date: Wed Mar 20 22:16:17 2024 -0400 refactor: Update compression header files and macros + Ci build commit 9c7b564911080ccd5cbbb9851a0757b05e1e9047 Author: vnugent Date: Tue Mar 19 21:54:49 2024 -0400 refactor: JWK overhaul & add length getter to FileUpload commit 6d8c3444e09561e5957491b3cc1ae858e0abdd14 Author: vnugent Date: Mon Mar 18 16:13:20 2024 -0400 feat: Add FNV1a software checksum and basic correction tests commit 00d182088cecefc08ca80b1faee9bed3f215f40b Author: vnugent Date: Fri Mar 15 01:05:27 2024 -0400 chore: #6 Use utils filewatcher instead of built-in commit d513c10d9895c6693519ef1d459c6a5a76929436 Author: vnugent Date: Sun Mar 10 21:58:14 2024 -0400 source tree project location updated --- apps/VNLib.WebServer/src/Config/IServerConfig.cs | 34 +++++ .../src/Config/JsonConfigOptions.cs | 42 ++++++ .../VNLib.WebServer/src/Config/JsonServerConfig.cs | 156 +++++++++++++++++++++ .../src/Config/Model/BenchmarkConfig.cs | 43 ++++++ .../src/Config/Model/CorsSecurityConfig.cs | 41 ++++++ .../src/Config/Model/ErrorFileConfig.cs | 37 +++++ .../src/Config/Model/HttpCompressorConfig.cs | 47 +++++++ .../src/Config/Model/HttpGlobalConfig.cs | 137 ++++++++++++++++++ apps/VNLib.WebServer/src/Config/Model/LogConfig.cs | 52 +++++++ .../src/Config/Model/ServerPluginConfig.cs | 46 ++++++ .../src/Config/Model/TcpConfigJson.cs | 73 ++++++++++ .../src/Config/Model/TransportInterface.cs | 137 ++++++++++++++++++ .../src/Config/Model/VirtualHostServerConfig.cs | 98 +++++++++++++ .../src/Config/ServerConfigurationException.cs | 40 ++++++ apps/VNLib.WebServer/src/Config/Validate.cs | 113 +++++++++++++++ 15 files changed, 1096 insertions(+) create mode 100644 apps/VNLib.WebServer/src/Config/IServerConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/JsonConfigOptions.cs create mode 100644 apps/VNLib.WebServer/src/Config/JsonServerConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/BenchmarkConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/CorsSecurityConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/ErrorFileConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/HttpCompressorConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/HttpGlobalConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/LogConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/ServerPluginConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/TcpConfigJson.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/TransportInterface.cs create mode 100644 apps/VNLib.WebServer/src/Config/Model/VirtualHostServerConfig.cs create mode 100644 apps/VNLib.WebServer/src/Config/ServerConfigurationException.cs create mode 100644 apps/VNLib.WebServer/src/Config/Validate.cs (limited to 'apps/VNLib.WebServer/src/Config') diff --git a/apps/VNLib.WebServer/src/Config/IServerConfig.cs b/apps/VNLib.WebServer/src/Config/IServerConfig.cs new file mode 100644 index 0000000..b0b06ba --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/IServerConfig.cs @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: IServerConfig.cs +* +* IServerConfig.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json; + + +namespace VNLib.WebServer.Config +{ + internal interface IServerConfig + { + JsonElement GetDocumentRoot(); + } +} diff --git a/apps/VNLib.WebServer/src/Config/JsonConfigOptions.cs b/apps/VNLib.WebServer/src/Config/JsonConfigOptions.cs new file mode 100644 index 0000000..a48b4e9 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/JsonConfigOptions.cs @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: JsonConfigOptions.cs +* +* JsonConfigOptions.cs is part of VNLib.WebServer which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json; + +namespace VNLib.WebServer.Config +{ + internal static class JsonConfigOptions + { + private static readonly JsonSerializerOptions _ops = new() + { + AllowTrailingCommas = true, + ReadCommentHandling = JsonCommentHandling.Skip, + }; + + public static T? DeserializeElement(this JsonElement el) + { + return el.Deserialize(_ops); + } + } +} diff --git a/apps/VNLib.WebServer/src/Config/JsonServerConfig.cs b/apps/VNLib.WebServer/src/Config/JsonServerConfig.cs new file mode 100644 index 0000000..ada9902 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/JsonServerConfig.cs @@ -0,0 +1,156 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: JsonServerConfig.cs +* +* JsonServerConfig.cs is part of VNLib.WebServer which is part +* of the larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.IO; +using System.Text.Json; + +using YamlDotNet.Core.Events; +using YamlDotNet.Serialization; + +using VNLib.Utils.IO; + +namespace VNLib.WebServer.Config +{ + internal sealed class JsonServerConfig(JsonDocument doc) : IServerConfig + { + public JsonElement GetDocumentRoot() => doc.RootElement; + + public static JsonServerConfig? FromFile(string filename) + { + string nameOnly = Path.GetFileName(filename); + Console.WriteLine("Loading configuration file from {0}", nameOnly); + + if (filename.EndsWith(".json")) + { + return FromJson(filename); + } + else if (filename.EndsWith(".yaml") || filename.EndsWith(".yml")) + { + return FromYaml(filename); + } + else + { + return null; + } + } + + /// + /// Reads a server configuration from the specified JSON document + /// + /// The file path of the json cofiguration file + /// A new wrapping the server config + public static JsonServerConfig? FromJson(string fileName) + { + if (!FileOperations.FileExists(fileName)) + { + return null; + } + + //Open the config file + using FileStream fs = File.OpenRead(fileName); + + //Allow comments + JsonDocumentOptions jdo = new() + { + CommentHandling = JsonCommentHandling.Skip, + AllowTrailingCommas = true, + }; + + return new JsonServerConfig(JsonDocument.Parse(fs, jdo)); + } + + public static JsonServerConfig? FromYaml(string fileName) + { + if (!FileOperations.FileExists(fileName)) + { + return null; + } + + /* + * The following code reads the configuration as a yaml + * object and then serializes it over to json. + */ + + using StreamReader reader = OpenFileRead(fileName); + + object? yamlObject = new DeserializerBuilder() + .WithNodeTypeResolver(new NumberTypeResolver()) + .Build() + .Deserialize(reader); + + ISerializer serializer = new SerializerBuilder() + .JsonCompatible() + .Build(); + + using VnMemoryStream ms = new(); + using (StreamWriter sw = new(ms, leaveOpen: true)) + { + serializer.Serialize(sw, yamlObject); + } + + ms.Seek(0, SeekOrigin.Begin); + + return new JsonServerConfig(JsonDocument.Parse(ms)); + } + + private static StreamReader OpenFileRead(string fileName) + { + return new StreamReader( + stream: File.OpenRead(fileName), + encoding: System.Text.Encoding.UTF8, + detectEncodingFromByteOrderMarks: false, + leaveOpen: false + ); + } + + public class NumberTypeResolver : INodeTypeResolver + { + public bool Resolve(NodeEvent? nodeEvent, ref Type currentType) + { + if (nodeEvent is Scalar scalar) + { + if(long.TryParse(scalar.Value, out _)) + { + currentType = typeof(int); + return true; + } + + if (double.TryParse(scalar.Value, out _)) + { + currentType = typeof(double); + return true; + } + + if (bool.TryParse(scalar.Value, out _)) + { + currentType = typeof(bool); + return true; + } + } + return false; + } + } + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/BenchmarkConfig.cs b/apps/VNLib.WebServer/src/Config/Model/BenchmarkConfig.cs new file mode 100644 index 0000000..1b0f157 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/BenchmarkConfig.cs @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: BenchmarkConfig.cs +* +* BenchmarkConfig.cs is part of VNLib.WebServer which is part of the +* larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + + internal sealed class BenchmarkConfig + { + + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } + + [JsonPropertyName("size")] + public int Size { get; set; } + + [JsonPropertyName("random")] + public bool Random { get; set; } + + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/CorsSecurityConfig.cs b/apps/VNLib.WebServer/src/Config/Model/CorsSecurityConfig.cs new file mode 100644 index 0000000..2f95697 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/CorsSecurityConfig.cs @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: CorsSecurityConfig.cs +* +* CorsSecurityConfig.cs is part of VNLib.WebServer which is part of the +* larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal class CorsSecurityConfig + { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = false; + + [JsonPropertyName("deny_cors_connections")] + public bool DenyCorsCons { get; set; } = false; + + [JsonPropertyName("allowed_authority")] + public string[] AllowedCorsAuthority { get; set; } = Array.Empty(); + } +} \ No newline at end of file diff --git a/apps/VNLib.WebServer/src/Config/Model/ErrorFileConfig.cs b/apps/VNLib.WebServer/src/Config/Model/ErrorFileConfig.cs new file mode 100644 index 0000000..c4355d6 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/ErrorFileConfig.cs @@ -0,0 +1,37 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: ErrorFileConfig.cs +* +* ErrorFileConfig.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal sealed class ErrorFileConfig + { + [JsonPropertyName("code")] + public int Code { get; set; } + + [JsonPropertyName("path")] + public string? Path { get; set; } + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/HttpCompressorConfig.cs b/apps/VNLib.WebServer/src/Config/Model/HttpCompressorConfig.cs new file mode 100644 index 0000000..eb8e68c --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/HttpCompressorConfig.cs @@ -0,0 +1,47 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: HttpCompressorConfig.cs +* +* HttpCompressorConfig.cs is part of VNLib.WebServer which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal sealed class HttpCompressorConfig + { + [JsonPropertyName("assembly")] + public string? AssemblyPath { get; set; } + + /// + /// If this compressor is enabled. The default is true, to use built-in + /// compressors. + /// + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = true; + + [JsonPropertyName("max_size")] + public long CompressionMax { get; set; } = 104857600; //100MB + + [JsonPropertyName("min_size")] + public int CompressionMin { get; set; } = 256; + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/HttpGlobalConfig.cs b/apps/VNLib.WebServer/src/Config/Model/HttpGlobalConfig.cs new file mode 100644 index 0000000..22bfe75 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/HttpGlobalConfig.cs @@ -0,0 +1,137 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: HttpGlobalConfig.cs +* +* HttpGlobalConfig.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal class HttpGlobalConfig + { + + [JsonPropertyName("default_version")] + public string DefaultHttpVersion { get; set; } = "HTTP/1.1"; + + /// + /// The maximum size of a request entity that can be sent to the server. + /// + [JsonPropertyName("max_entity_size")] + public long MaxEntitySize { get; set; } = long.MaxValue; + + /// + /// The maximum size of a multipart form data upload. + /// + [JsonPropertyName("multipart_max_size")] + public int MultipartMaxSize { get; set; } = 1048576; //1MB + + /// + /// The time in milliseconds for an HTTP/1.1 connection to remain open + /// before the server closes it. + /// + [JsonPropertyName("keepalive_ms")] + public int KeepAliveMs { get; set; } = 60000; //60 seconds + + /// + /// The time in milliseconds to wait for data on an active connection. + /// IE: A connection that has been established and has signaled that + /// it is ready to transfer data. + /// + [JsonPropertyName("recv_timeout_ms")] + public int RecvTimeoutMs { get; set; } = 5000; //5 seconds + + /// + /// The time in milliseconds to wait for data to be sent on a connection. + /// + [JsonPropertyName("send_timeout_ms")] + public int SendTimeoutMs { get; set; } = 60000; //60 seconds + + /// + /// The maximum number of headers that can be sent in a request. + /// + [JsonPropertyName("max_request_header_count")] + public int MaxRequestHeaderCount { get; set; } = 32; + + /// + /// The maximum number of open connections that can be made to the server, before + /// the server starts rejecting new connections. + /// + [JsonPropertyName("max_connections")] + public int MaxConnections { get; set; } = int.MaxValue; + + /// + /// The maximum number of uploads that can be made in a single request. If + /// this value is exceeded, the request will be rejected. + /// + [JsonPropertyName("max_uploads_per_request")] + public ushort MaxUploadsPerRequest { get; set; } = 10; + + /// + /// The size of the buffer used to store request headers. + /// + [JsonPropertyName("header_buf_size")] + public int HeaderBufSize { get; set; } + + /// + /// The size of the buffer used to store response headers. + /// + [JsonPropertyName("response_header_buf_size")] + public int ResponseHeaderBufSize { get; set; } + + /// + /// The size of the buffer used to store form data. + /// + [JsonPropertyName("multipart_max_buf_size")] + public int MultipartMaxBufSize { get; set; } + + /// + /// The configuration for the HTTP compression settings. + /// + [JsonPropertyName("compression")] + public HttpCompressorConfig? Compression { get; set; } = new(); + + public void ValidateConfig() + { + Validate.EnsureNotNull(DefaultHttpVersion, "Default HTTP version is required"); + + Validate.EnsureRange(MaxEntitySize, 0, long.MaxValue); + Validate.EnsureRange(MultipartMaxSize, -1, int.MaxValue); + Validate.EnsureRange(KeepAliveMs, -1, int.MaxValue); + + //Timeouts may be disabled by setting 0 or -1. Both are allowed for readability + Validate.EnsureRange(RecvTimeoutMs, -2, int.MaxValue); + Validate.EnsureRange(SendTimeoutMs, -2, int.MaxValue); + + Validate.EnsureRange(MaxRequestHeaderCount, 0, 1024); + Validate.EnsureRange(MaxConnections, 0, int.MaxValue); + Validate.EnsureRange(MaxUploadsPerRequest, 0, 1024); + Validate.EnsureRange(HeaderBufSize, 0, int.MaxValue); + Validate.EnsureRange(ResponseHeaderBufSize, 0, int.MaxValue); + Validate.EnsureRange(MultipartMaxBufSize, 0, int.MaxValue); + + //Validate compression config + Validate.EnsureNotNull(Compression, "Compression configuration should not be set to null. Comment to enable defaults"); + Validate.EnsureRange(Compression.CompressionMax, -1, long.MaxValue); + Validate.EnsureRange(Compression.CompressionMin, -1, int.MaxValue); + } + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/LogConfig.cs b/apps/VNLib.WebServer/src/Config/Model/LogConfig.cs new file mode 100644 index 0000000..fafd630 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/LogConfig.cs @@ -0,0 +1,52 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: LogConfig.cs +* +* LogConfig.cs is part of VNLib.WebServer which is part of the +* larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal sealed class LogConfig + { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = false; + + [JsonPropertyName("path")] + public string? Path { get; set; } + + [JsonPropertyName("template")] + public string? Template { get; set; } + + [JsonPropertyName("flush_sec")] + public int FlushIntervalSeconds { get; set; } = 10; + + [JsonPropertyName("retained_files")] + public int RetainedFiles { get; set; } = 31; + + [JsonPropertyName("file_size_limit")] + public int FileSizeLimit { get; set; } = 500 * 1000 * 1024; + + [JsonPropertyName("interval")] + public string Interval { get; set; } = "infinite"; + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/ServerPluginConfig.cs b/apps/VNLib.WebServer/src/Config/Model/ServerPluginConfig.cs new file mode 100644 index 0000000..42b91b1 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/ServerPluginConfig.cs @@ -0,0 +1,46 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: ServerPluginConfig.cs +* +* ServerPluginConfig.cs is part of VNLib.WebServer which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal class ServerPluginConfig + { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } = true; //default to true if config is defined, then it can be assumed we want to load plugins unless explicitly disabled + + [JsonPropertyName("path")] + public string? Path { get; set; } + + [JsonPropertyName("config_dir")] + public string? ConfigDir { get; set; } + + [JsonPropertyName("hot_reload")] + public bool HotReload { get; set; } + + [JsonPropertyName("reload_delay_sec")] + public int ReloadDelaySec { get; set; } = 2; + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/TcpConfigJson.cs b/apps/VNLib.WebServer/src/Config/Model/TcpConfigJson.cs new file mode 100644 index 0000000..5bd2b94 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/TcpConfigJson.cs @@ -0,0 +1,73 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: TcpServerLoader.cs +* +* TcpServerLoader.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal sealed class TcpConfigJson + { + [JsonPropertyName("keepalive_sec")] + public int TcpKeepAliveTime { get; set; } = 4; + + [JsonPropertyName("keepalive_interval_sec")] + public int KeepaliveInterval { get; set; } = 4; + + [JsonPropertyName("max_recv_buffer")] + public int MaxRecvBufferData { get; set; } = 10 * 64 * 1024; + + [JsonPropertyName("backlog")] + public int BackLog { get; set; } = 1000; + + [JsonPropertyName("max_connections")] + public long MaxConnections { get; set; } = long.MaxValue; + + [JsonPropertyName("no_delay")] + public bool NoDelay { get; set; } = false; + + /* + * Buffer sizes are a pain, this is a good default size for medium bandwith connections (100mbps) + * using the BDP calculations + * + * BDP = Bandwidth * RTT + */ + + [JsonPropertyName("tx_buffer")] + public int TcpSendBufferSize { get; set; } = 625 * 1024; + + [JsonPropertyName("rx_buffer")] + public int TcpRecvBufferSize { get; set; } = 625 * 1024; + + + public void ValidateConfig() + { + Validate.EnsureRange(TcpKeepAliveTime, 0, 60); + Validate.EnsureRange(KeepaliveInterval, 0, 60); + Validate.EnsureRange(BackLog, 0, 10000); + Validate.EnsureRange(MaxConnections, 0, long.MaxValue); + Validate.EnsureRange(MaxRecvBufferData, 0, 10 * 1024 * 1024); //10MB + Validate.EnsureRange(TcpSendBufferSize, 0, 10 * 1024 * 1024); //10MB + } + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/TransportInterface.cs b/apps/VNLib.WebServer/src/Config/Model/TransportInterface.cs new file mode 100644 index 0000000..e0e2551 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/TransportInterface.cs @@ -0,0 +1,137 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: TransportInterface.cs +* +* TransportInterface.cs is part of VNLib.WebServer which is part of +* the larger VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.IO; +using System.Net; +using System.Security.Cryptography.X509Certificates; +using System.Text.Json.Serialization; + +using VNLib.Utils.Memory; +using VNLib.Utils.Resources; + +namespace VNLib.WebServer.Config.Model +{ + /// + /// Represents a transport interface configuration element for a virtual host + /// + internal class TransportInterface + { + [JsonPropertyName("port")] + public int Port { get; set; } + + [JsonPropertyName("address")] + public string? Address { get; set; } + + [JsonPropertyName("certificate")] + public string? Cert { get; set; } + + [JsonPropertyName("private_key")] + public string? PrivKey { get; set; } + + [JsonPropertyName("ssl")] + public bool Ssl { get; set; } + + [JsonPropertyName("client_cert_required")] + public bool ClientCertRequired { get; set; } + + [JsonPropertyName("password")] + public string? PrivKeyPassword { get; set; } + + [JsonPropertyName("use_os_ciphers")] + public bool UseOsCiphers { get; set; } + + public IPEndPoint GetEndpoint() + { + IPAddress addr = string.IsNullOrEmpty(Address) ? IPAddress.Any : IPAddress.Parse(Address); + return new IPEndPoint(addr, Port); + } + + public X509Certificate? LoadCertificate() + { + if (!Ssl) + { + return null; + } + + Validate.EnsureNotNull(Cert, "TLS Certificate is required when ssl is enabled"); + Validate.FileExists(Cert); + + X509Certificate? cert = null; + + /* + * Default to use a PEM encoded certificate and private key file. Unless the file + * is a pfx file, then we will use the private key from the pfx file. + */ + + if (Path.GetExtension(Cert).EndsWith("pfx", StringComparison.OrdinalIgnoreCase)) + { + //Create from pfx file including private key + cert = X509Certificate.CreateFromCertFile(Cert); + } + else + { + Validate.EnsureNotNull(PrivKey, "TLS Private Key is required ssl is enabled"); + Validate.FileExists(PrivKey); + + /* + * Attempt to capture the private key password. This will wrap the + * string in a private string instance, and setting the value to true + * will ensure the password memory is wiped when this function returns + */ + using PrivateString? password = PrivateString.ToPrivateString(PrivKeyPassword, true); + + //Load the cert and decrypt with password if set + using X509Certificate2 cert2 = password == null ? X509Certificate2.CreateFromPemFile(Cert, PrivKey) + : X509Certificate2.CreateFromEncryptedPemFile(Cert, password.ToReadOnlySpan(), PrivKey); + + /* + * Workaround for a silly Windows SecureChannel module bug for parsing + * X509Certificate2 from pem cert and private key files. + * + * Must export into pkcs12 format then create a new X509Certificate2 from the + * exported bytes. + */ + + //Copy the cert in pkcs12 format + byte[] pkcs = cert2.Export(X509ContentType.Pkcs12); + cert = new X509Certificate2(pkcs); + MemoryUtil.InitializeBlock(pkcs); + } + + return cert; + } + + /// + /// Builds a deterministic hash-code base on the configuration state. + /// + /// The hash-code that represents the current instance + public override int GetHashCode() => HashCode.Combine(Address, Port); + + public override bool Equals(object? obj) => obj is TransportInterface iface && GetHashCode() == iface.GetHashCode(); + + public override string ToString() => $"[{Address}:{Port}]"; + + } +} diff --git a/apps/VNLib.WebServer/src/Config/Model/VirtualHostServerConfig.cs b/apps/VNLib.WebServer/src/Config/Model/VirtualHostServerConfig.cs new file mode 100644 index 0000000..5734bd3 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Model/VirtualHostServerConfig.cs @@ -0,0 +1,98 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: VirtualHostServerConfig.cs +* +* VirtualHostServerConfig.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text.Json.Serialization; + +namespace VNLib.WebServer.Config.Model +{ + internal sealed class VirtualHostServerConfig + { + [JsonPropertyName("trace")] + public bool RequestTrace { get; set; } = false; + + [JsonPropertyName("force_port_check")] + public bool ForcePortCheck { get; set; } = false; + + [JsonPropertyName("benchmark")] + public BenchmarkConfig? Benchmark { get; set; } + + [JsonPropertyName("interfaces")] + public TransportInterface[] Interfaces { get; set; } = Array.Empty(); + + [JsonPropertyName("hostnames")] + public string[]? Hostnames { get; set; } = Array.Empty(); + + [JsonPropertyName("hostname")] + public string? Hostname + { + get => Hostnames?.FirstOrDefault(); + set + { + if (value != null) + { + Hostnames = [value]; + } + } + } + + [JsonPropertyName("path")] + public string? DirPath { get; set; } = string.Empty; + + [JsonPropertyName("downstream_servers")] + public string[] DownstreamServers { get; set; } = Array.Empty(); + + [JsonPropertyName("whitelist")] + public string[]? Whitelist { get; set; } + + [JsonPropertyName("blacklist")] + public string[]? Blacklist { get; set; } + + [JsonPropertyName("deny_extensions")] + public string[]? DenyExtensions { get; set; } + + [JsonPropertyName("default_files")] + public string[]? DefaultFiles { get; set; } + + [JsonPropertyName("headers")] + public Dictionary Headers { get; set; } = []; + + [JsonPropertyName("cors")] + public CorsSecurityConfig Cors { get; set; } = new(); + + [JsonPropertyName("error_files")] + public ErrorFileConfig[] ErrorFiles { get; set; } = Array.Empty(); + + [JsonPropertyName("cache_default_sec")] + public int CacheDefaultTimeSeconds { get; set; } = 0; + + [JsonPropertyName("path_filter")] + public string? PathFilter { get; set; } + + [JsonPropertyName("max_execution_time_ms")] + public int MaxExecutionTimeMs { get; set; } = 20000; + } +} diff --git a/apps/VNLib.WebServer/src/Config/ServerConfigurationException.cs b/apps/VNLib.WebServer/src/Config/ServerConfigurationException.cs new file mode 100644 index 0000000..39c18e5 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/ServerConfigurationException.cs @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: ServerConfigurationException.cs +* +* ServerConfigurationException.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System; + +namespace VNLib.WebServer.Config +{ + public class ServerConfigurationException : Exception + { + public ServerConfigurationException() + { } + + public ServerConfigurationException(string? message) : base(message) + { } + + public ServerConfigurationException(string? message, Exception? innerException) : base(message, innerException) + { } + } +} diff --git a/apps/VNLib.WebServer/src/Config/Validate.cs b/apps/VNLib.WebServer/src/Config/Validate.cs new file mode 100644 index 0000000..773d787 --- /dev/null +++ b/apps/VNLib.WebServer/src/Config/Validate.cs @@ -0,0 +1,113 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.WebServer +* File: Validate.cs +* +* Validate.cs is part of VNLib.WebServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.WebServer is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published +* by the Free Software Foundation, either version 2 of the License, +* or (at your option) any later version. +* +* VNLib.WebServer is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with VNLib.WebServer. If not, see http://www.gnu.org/licenses/. +*/ + +using System; +using System.Net; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +using VNLib.Utils.IO; + +namespace VNLib.WebServer.Config +{ + internal static class Validate + { + [DoesNotReturn] + public static void EnsureNotNull(T? obj, string message) where T : class + { + if (obj is null) + { + throw new ServerConfigurationException(message); + } + + if (obj is string s && string.IsNullOrWhiteSpace(s)) + { + throw new ServerConfigurationException(message); + } + } + + public static void Assert([DoesNotReturnIf(false)] bool condition, string message) + { + if (!condition) + { + throw new ServerConfigurationException(message); + } + } + + public static void EnsureValidIp(string? address, string message) + { + if (!IPAddress.TryParse(address, out _)) + { + throw new ServerConfigurationException(message); + } + } + + public static void EnsureNotEqual(T a, T b, string message) + { + if (a is null || b is null) + { + throw new ServerConfigurationException(message); + } + + if (a.Equals(b)) + { + throw new ServerConfigurationException(message); + } + } + + public static void EnsureRangeEx(ulong value, ulong min, ulong max, string message) + { + if (value < min || value > max) + { + throw new ServerConfigurationException(message); + } + } + + public static void EnsureRangeEx(long value, long min, long max, string message) + { + if (value < min || value > max) + { + throw new ServerConfigurationException(message); + } + } + + public static void EnsureRange(ulong value, ulong min, ulong max, [CallerArgumentExpression(nameof(value))] string? paramName = null) + { + EnsureRangeEx(value, min, max, $"Value for {paramName} must be between {min} and {max}. Value: {value}"); + } + + public static void EnsureRange(long value, long min, long max, [CallerArgumentExpression(nameof(value))] string? paramName = null) + { + EnsureRangeEx(value, min, max, $"Value for {paramName} must be between {min} and {max}. Value: {value}"); + } + + public static void FileExists(string path) + { + if (!FileOperations.FileExists(path)) + { + throw new ServerConfigurationException($"Required file: {path} not found"); + } + } + } +} -- cgit