|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Buffers; |
| 6 | +using System.Runtime.CompilerServices; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | + |
| 9 | +namespace SixLabors.ImageSharp.Web.Caching |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Optimized helper methods for generating cache keys from URI components. Much of this code has been adapted from the MIT licensed .NET runtime. |
| 13 | + /// </summary> |
| 14 | + internal static class CacheKeyHelper |
| 15 | + { |
| 16 | + private static readonly SpanAction<char, (bool LowerInvariant, string Host, string PathBase, string Path, string Query)> InitializeAbsoluteUriStringSpanAction = new(InitializeAbsoluteUriString); |
| 17 | + |
| 18 | + public enum CaseHandling |
| 19 | + { |
| 20 | + None, |
| 21 | + LowerInvariant |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Combines the given URI components into a string that is properly encoded for use in HTTP headers. |
| 26 | + /// </summary> |
| 27 | + /// <param name="handling">Determines case handling for the result. <paramref name="query"/> is always converted to invariant lowercase.</param> |
| 28 | + /// <param name="pathBase">The first portion of the request path associated with application root.</param> |
| 29 | + /// <param name="path">The portion of the request path that identifies the requested resource.</param> |
| 30 | + /// <param name="query">The query, if any.</param> |
| 31 | + /// <returns>The combined URI components, properly encoded for use in HTTP headers.</returns> |
| 32 | + public static string BuildRelativeKey( |
| 33 | + CaseHandling handling, |
| 34 | + PathString pathBase = default, |
| 35 | + PathString path = default, |
| 36 | + QueryString query = default) |
| 37 | + |
| 38 | + // Take any potential performance hit vs concatination for code reading sanity. |
| 39 | + => BuildAbsoluteKey(handling, default, pathBase, path, query); |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Combines the given URI components into a string that is properly encoded for use in HTTP headers. |
| 43 | + /// Note that unicode in the HostString will be encoded as punycode. |
| 44 | + /// </summary> |
| 45 | + /// <param name="handling">Determines case handling for the result. <paramref name="query"/> is always converted to invariant lowercase.</param> |
| 46 | + /// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param> |
| 47 | + /// <param name="pathBase">The first portion of the request path associated with application root.</param> |
| 48 | + /// <param name="path">The portion of the request path that identifies the requested resource.</param> |
| 49 | + /// <param name="query">The query, if any.</param> |
| 50 | + /// <returns>The combined URI components, properly encoded for use in HTTP headers.</returns> |
| 51 | + public static string BuildAbsoluteKey( |
| 52 | + CaseHandling handling, |
| 53 | + HostString host, |
| 54 | + PathString pathBase = default, |
| 55 | + PathString path = default, |
| 56 | + QueryString query = default) |
| 57 | + { |
| 58 | + string hostText = host.ToString(); |
| 59 | + string pathBaseText = pathBase.ToString(); |
| 60 | + string pathText = path.ToString(); |
| 61 | + string queryText = query.ToString(); |
| 62 | + |
| 63 | + // PERF: Calculate string length to allocate correct buffer size for string.Create. |
| 64 | + int length = |
| 65 | + hostText.Length + |
| 66 | + pathBaseText.Length + |
| 67 | + pathText.Length + |
| 68 | + queryText.Length; |
| 69 | + |
| 70 | + if (string.IsNullOrEmpty(pathText)) |
| 71 | + { |
| 72 | + if (string.IsNullOrEmpty(pathBaseText)) |
| 73 | + { |
| 74 | + pathText = "/"; |
| 75 | + length++; |
| 76 | + } |
| 77 | + } |
| 78 | + else if (pathBaseText.EndsWith('/')) |
| 79 | + { |
| 80 | + // If the path string has a trailing slash and the other string has a leading slash, we need |
| 81 | + // to trim one of them. |
| 82 | + // Just decrement the total length, for now. |
| 83 | + length--; |
| 84 | + } |
| 85 | + |
| 86 | + return string.Create(length, (handling == CaseHandling.LowerInvariant, hostText, pathBaseText, pathText, queryText), InitializeAbsoluteUriStringSpanAction); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Copies the specified <paramref name="text"/> to the specified <paramref name="buffer"/> starting at the specified <paramref name="index"/>. |
| 91 | + /// </summary> |
| 92 | + /// <param name="buffer">The buffer to copy text to.</param> |
| 93 | + /// <param name="index">The buffer start index.</param> |
| 94 | + /// <param name="text">The text to copy.</param> |
| 95 | + /// <returns>The <see cref="int"/> representing the combined text length.</returns> |
| 96 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 97 | + private static int CopyTextToBuffer(Span<char> buffer, int index, ReadOnlySpan<char> text) |
| 98 | + { |
| 99 | + text.CopyTo(buffer.Slice(index, text.Length)); |
| 100 | + return index + text.Length; |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Copies the specified <paramref name="text"/> to the specified <paramref name="buffer"/> starting at the specified <paramref name="index"/> |
| 105 | + /// converting each character to lowercase, using the casing rules of the invariant culture. |
| 106 | + /// </summary> |
| 107 | + /// <param name="buffer">The buffer to copy text to.</param> |
| 108 | + /// <param name="index">The buffer start index.</param> |
| 109 | + /// <param name="text">The text to copy.</param> |
| 110 | + /// <returns>The <see cref="int"/> representing the combined text length.</returns> |
| 111 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 112 | + private static int CopyTextToBufferLowerInvariant(Span<char> buffer, int index, ReadOnlySpan<char> text) |
| 113 | + => index + text.ToLowerInvariant(buffer.Slice(index, text.Length)); |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Initializes the URI <see cref="string"/> for <see cref="BuildAbsoluteKey(CaseHandling, HostString, PathString, PathString, QueryString)"/>. |
| 117 | + /// </summary> |
| 118 | + /// <param name="buffer">The URI <see cref="string"/>'s <see cref="char"/> buffer.</param> |
| 119 | + /// <param name="uriParts">The URI parts.</param> |
| 120 | + private static void InitializeAbsoluteUriString(Span<char> buffer, (bool Lower, string Host, string PathBase, string Path, string Query) uriParts) |
| 121 | + { |
| 122 | + int index = 0; |
| 123 | + ReadOnlySpan<char> pathBaseSpan = uriParts.PathBase.AsSpan(); |
| 124 | + |
| 125 | + if (uriParts.Path.Length > 0 && pathBaseSpan.Length > 0 && pathBaseSpan[pathBaseSpan.Length - 1] == '/') |
| 126 | + { |
| 127 | + // If the path string has a trailing slash and the other string has a leading slash, we need |
| 128 | + // to trim one of them. |
| 129 | + // Trim the last slash from pathBase. The total length was decremented before the call to string.Create. |
| 130 | + pathBaseSpan = pathBaseSpan.Slice(0, pathBaseSpan.Length - 1); |
| 131 | + } |
| 132 | + |
| 133 | + if (uriParts.Lower) |
| 134 | + { |
| 135 | + index = CopyTextToBufferLowerInvariant(buffer, index, uriParts.Host.AsSpan()); |
| 136 | + index = CopyTextToBufferLowerInvariant(buffer, index, pathBaseSpan); |
| 137 | + index = CopyTextToBufferLowerInvariant(buffer, index, uriParts.Path.AsSpan()); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + index = CopyTextToBuffer(buffer, index, uriParts.Host.AsSpan()); |
| 142 | + index = CopyTextToBuffer(buffer, index, pathBaseSpan); |
| 143 | + index = CopyTextToBuffer(buffer, index, uriParts.Path.AsSpan()); |
| 144 | + } |
| 145 | + |
| 146 | + // Querystring is always copied as lower invariant. |
| 147 | + _ = CopyTextToBufferLowerInvariant(buffer, index, uriParts.Query.AsSpan()); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments