|
| 1 | +// ****************************************************************************************************************************** |
| 2 | +// |
| 3 | +// Copyright (c) 2018-2022 InterlockLedger Network |
| 4 | +// All rights reserved. |
| 5 | +// |
| 6 | +// Redistribution and use in source and binary forms, with or without |
| 7 | +// modification, are permitted provided that the following conditions are met |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above copyright notice, this |
| 10 | +// list of conditions and the following disclaimer. |
| 11 | +// |
| 12 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +// this list of conditions and the following disclaimer in the documentation |
| 14 | +// and/or other materials provided with the distribution. |
| 15 | +// |
| 16 | +// * Neither the name of the copyright holder nor the names of its |
| 17 | +// contributors may be used to endorse or promote products derived from |
| 18 | +// this software without specific prior written permission. |
| 19 | +// |
| 20 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 24 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | +// SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION) HOWEVER |
| 27 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | +// |
| 31 | +// ****************************************************************************************************************************** |
| 32 | + |
| 33 | +using System.Diagnostics.CodeAnalysis; |
| 34 | + |
| 35 | +namespace InterlockLedger.Rest.Client; |
| 36 | + |
| 37 | +public record ConfigurationOptions(string Host, ushort Port, string? ClientCertificateFilePath, string? ClientCertificatePassword) : IParsable<ConfigurationOptions> |
| 38 | +{ |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Parses a connection string to InterlockLedger REST Client ConfigurationOptions |
| 42 | + /// </summary> |
| 43 | + /// <param name="connectionString">Connection string in the form "ilkl-minerva://minerva-data.il2.io[:32067],/absolute/path/to/clientcertificate.pfx,clientCertificatePassword"</param> |
| 44 | + /// <returns>Parsed ConfigurationOptions</returns> |
| 45 | + /// <exception cref="ArgumentException"></exception> |
| 46 | + public static ConfigurationOptions Parse(string connectionString) => Parse(connectionString, null); |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Parses a connection string to InterlockLedger REST Client ConfigurationOptions |
| 50 | + /// </summary> |
| 51 | + /// <param name="connectionString">Connection string in the form "ilkl-minerva://minerva-data.il2.io[:32067],/absolute/path/to/clientcertificate.pfx,clientCertificatePassword"</param> |
| 52 | + /// <param name="provider">[Ignored] Format provider</param> |
| 53 | + /// <returns>Parsed ConfigurationOptions</returns> |
| 54 | + /// <exception cref="ArgumentException"></exception> |
| 55 | + public static ConfigurationOptions Parse(string connectionString, IFormatProvider? provider) => |
| 56 | + TryParse(connectionString, provider, out var result) |
| 57 | + ? result |
| 58 | + : throw new ArgumentException("Malformed connection string", nameof(connectionString)); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Try to parse a connection string to InterlockLedger REST Client ConfigurationOptions |
| 62 | + /// </summary> |
| 63 | + /// <param name="connectionString">Connection string in the form "ilkl-minerva://minerva-data.il2.io[:32067],/absolute/path/to/clientcertificate.pfx,clientCertificatePassword"</param> |
| 64 | + /// <param name="provider"></param> |
| 65 | + /// <param name="result"></param> |
| 66 | + /// <returns>True if correctly parsed</returns> |
| 67 | + public static bool TryParse([NotNullWhen(true)] string? connectionString, IFormatProvider? provider, [MaybeNullWhen(false)] out ConfigurationOptions result) { |
| 68 | + if (!string.IsNullOrWhiteSpace(connectionString)) { |
| 69 | + var match = ConfigurationOptionsParsingHelper.ConnectionStringRegex().Match(connectionString); |
| 70 | + var groups = match.Groups; |
| 71 | + if (match.Success && groups.Count >= 8 && ParsePort(groups[4], groups[1].Value, out ushort port)) { |
| 72 | + result = new ConfigurationOptions(groups[2].Value, port, GetValueOrDefault(groups[6]), GetValueOrDefault(groups[7])); |
| 73 | + return true; |
| 74 | + } |
| 75 | + } |
| 76 | + result = null; |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + private static string? GetValueOrDefault(Group group) => group.Success ? group.Value : null; |
| 81 | + |
| 82 | + private static bool ParsePort(Group group, string network, out ushort port) { |
| 83 | + if (group.Success) |
| 84 | + return ushort.TryParse(group.Value, null, out port); |
| 85 | + port = ConfigurationOptionsParsingHelper.GetDefaultPortFor(network); |
| 86 | + return true; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +internal static partial class ConfigurationOptionsParsingHelper |
| 91 | +{ |
| 92 | + [GeneratedRegex(@"^ilkl-(\w+)://([^:,]+)(:(\d+))?(,([^,]+),([^,]+))?$")] |
| 93 | + public static partial Regex ConnectionStringRegex(); |
| 94 | + public static ushort GetDefaultPortFor(string network) => 0; |
| 95 | +} |
0 commit comments