aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
diff options
context:
space:
mode:
authorBaronGreenback <jimcartlidge@yahoo.co.uk>2021-05-05 22:52:39 +0100
committerBaronGreenback <jimcartlidge@yahoo.co.uk>2021-05-05 22:52:39 +0100
commit81d675990f87586061bdce5d585dad28f7e181fa (patch)
tree7b71f32353b3ac6f6a8c4cb8046ca244fbff21ec /Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
parent557a2ad7158b14ae97fa503a551ed17251b97ca0 (diff)
Enable automatic url decoding
Diffstat (limited to 'Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs')
-rw-r--r--Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs75
1 files changed, 75 insertions, 0 deletions
diff --git a/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs b/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
new file mode 100644
index 000000000..0232b89ce
--- /dev/null
+++ b/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
@@ -0,0 +1,75 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Http.Features;
+using Microsoft.Extensions.Primitives;
+
+namespace Jellyfin.Server.Middleware
+{
+ /// <summary>
+ /// Defines the <see cref="UrlDecodeQueryFeature"/>.
+ /// </summary>
+ public class UrlDecodeQueryFeature : IQueryFeature
+ {
+ private IQueryCollection? _store;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="UrlDecodeQueryFeature"/> class.
+ /// </summary>
+ /// <param name="feature">The <see cref="IQueryFeature"/> instance.</param>
+ public UrlDecodeQueryFeature(IQueryFeature feature)
+ {
+ Query = feature.Query;
+ }
+
+ /// <summary>
+ /// Gets or sets a value indicating the url decoded <see cref="IQueryCollection"/>.
+ /// </summary>
+ public IQueryCollection Query
+ {
+ get
+ {
+ return _store ?? QueryCollection.Empty;
+ }
+
+ set
+ {
+ // Only interested in where the querystring is encoded which shows up as one key with everything else in the value.
+ if (value.Count != 1)
+ {
+ _store = value;
+ return;
+ }
+
+ // Encoded querystrings have no value, so don't process anything if a values is present.
+ var kvp = value.First();
+ if (!string.IsNullOrEmpty(kvp.Value))
+ {
+ _store = value;
+ return;
+ }
+
+ // Unencode and re-parse querystring.
+ var unencodedKey = HttpUtility.UrlDecode(kvp.Key);
+
+ if (string.Equals(unencodedKey, kvp.Key, System.StringComparison.Ordinal))
+ {
+ _store = value;
+ return;
+ }
+
+ var pairs = new Dictionary<string, StringValues>();
+ var queryString = unencodedKey.Split('&', System.StringSplitOptions.RemoveEmptyEntries);
+
+ foreach (var pair in queryString)
+ {
+ var item = pair.Split('=', System.StringSplitOptions.RemoveEmptyEntries);
+ pairs.Add(item[0], new StringValues(item.Length == 2 ? item[1] : string.Empty));
+ }
+
+ _store = new QueryCollection(pairs);
+ }
+ }
+ }
+}