| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="StringExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Text.Sanitization; |
| | | 12 | | |
| | | 13 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 14 | | namespace MyNet.Text; |
| | | 15 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 16 | | |
| | | 17 | | public static class StringExtensions |
| | | 18 | | { |
| | | 19 | | private const string RelativeUriSeparator = "/"; |
| | | 20 | | private const string WebUriSeparator = "&"; |
| | | 21 | | private const string QueryStringEquals = "="; |
| | | 22 | | |
| | | 23 | | extension(string value) |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Builds a relative URI by appending sanitized non-empty path segments to the base path. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="parameters">Optional path segments appended to the base path.</param> |
| | | 29 | | /// <returns>A relative <see cref="Uri"/>.</returns> |
| | | 30 | | /// <exception cref="ArgumentException">Thrown when a segment is "." or "..".</exception> |
| | | 31 | | public Uri ToRelativeUri(params string?[] parameters) |
| | | 32 | | { |
| | 9 | 33 | | ArgumentNullException.ThrowIfNull(parameters); |
| | | 34 | | |
| | 9 | 35 | | var allSegments = string.SplitAndSanitizeSegments(value) |
| | 9 | 36 | | .Concat(parameters.SelectMany(string.SplitAndSanitizeSegments)); |
| | | 37 | | |
| | 9 | 38 | | var relativePath = string.Join(RelativeUriSeparator, allSegments); |
| | 6 | 39 | | return new(relativePath, UriKind.Relative); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Builds an absolute web URI and appends query-string parameters safely. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="parameters">Query-string key/value pairs.</param> |
| | | 46 | | /// <returns>An absolute <see cref="Uri"/>.</returns> |
| | | 47 | | /// <exception cref="ArgumentException">Thrown when the base URI is not absolute.</exception> |
| | | 48 | | public Uri ToWebUri(params (string Key, string Value)[] parameters) |
| | | 49 | | { |
| | 9 | 50 | | ArgumentNullException.ThrowIfNull(parameters); |
| | 9 | 51 | | if (!Uri.TryCreate(value, UriKind.Absolute, out var baseUri)) |
| | | 52 | | { |
| | 3 | 53 | | throw new ArgumentException("The base URI must be absolute."); |
| | | 54 | | } |
| | | 55 | | |
| | 6 | 56 | | var builder = new UriBuilder(baseUri); |
| | 6 | 57 | | var currentQuery = builder.Query.TrimStart('?'); |
| | 6 | 58 | | var appendedQuery = string.Join( |
| | 6 | 59 | | WebUriSeparator, |
| | 6 | 60 | | parameters |
| | 6 | 61 | | .Where(x => !string.IsNullOrWhiteSpace(x.Key)) |
| | 6 | 62 | | .Select(x => $"{Sanitizer.UrlSegment.Apply(x.Key, CultureInfo.InvariantCulture)}{QueryStringEquals}{ |
| | | 63 | | |
| | 6 | 64 | | builder.Query = string.IsNullOrEmpty(currentQuery) |
| | 6 | 65 | | ? appendedQuery |
| | 6 | 66 | | : string.IsNullOrEmpty(appendedQuery) |
| | 6 | 67 | | ? currentQuery |
| | 6 | 68 | | : currentQuery + WebUriSeparator + appendedQuery; |
| | | 69 | | |
| | 6 | 70 | | return builder.Uri; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Splits the input string into segments based on the defined separator, trims whitespace, removes empty segmen |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="path">The input string to split and sanitize.</param> |
| | | 77 | | /// <returns>An enumerable of sanitized segments.</returns> |
| | | 78 | | private static IEnumerable<string> SplitAndSanitizeSegments(string? path) => |
| | 30 | 79 | | string.IsNullOrWhiteSpace(path) |
| | 30 | 80 | | ? [] |
| | 30 | 81 | | : path |
| | 30 | 82 | | .Split([RelativeUriSeparator], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntrie |
| | 30 | 83 | | .Select(SanitizePathSegment) |
| | 30 | 84 | | .Where(x => !string.IsNullOrEmpty(x)) |
| | 30 | 85 | | .Select(x => x!); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Sanitizes a path segment by trimming whitespace and applying URL-safe sanitization. If the segment is null, |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="segment">The path segment to sanitize.</param> |
| | | 91 | | /// <returns>The sanitized path segment, or null if the segment is null, empty, or consists only of whitespace.< |
| | | 92 | | /// <exception cref="ArgumentException">Thrown when the segment is "." or "..".</exception> |
| | | 93 | | private static string? SanitizePathSegment(string? segment) |
| | | 94 | | { |
| | 24 | 95 | | if (string.IsNullOrWhiteSpace(segment)) |
| | 0 | 96 | | return null; |
| | | 97 | | |
| | 24 | 98 | | var normalized = segment.Trim(); |
| | 24 | 99 | | return normalized is "." or ".." ? throw new ArgumentException("Path segments '.' and '..' are not allowed." |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |