aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
diff options
context:
space:
mode:
authorPatrick Barron <barronpm@gmail.com>2021-06-18 18:47:44 -0400
committerPatrick Barron <barronpm@gmail.com>2021-06-18 18:56:10 -0400
commitbe88efce3cbbd357142a75f109258d6c7be398b4 (patch)
treedf8a0bf9d5e108d45a473edd8121a9af91ca6a0d /Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
parent336ba2879f325a4efd52bc7737ce94f40369bfeb (diff)
parentc791c3a215b33bd4ca534c9f383c9fd7d23b59af (diff)
Merge branch 'master' into authenticationdb-efcore
# Conflicts: # Emby.Server.Implementations/Devices/DeviceManager.cs # Emby.Server.Implementations/HttpServer/Security/SessionContext.cs # Emby.Server.Implementations/Security/AuthenticationRepository.cs # Emby.Server.Implementations/Session/SessionManager.cs # Jellyfin.Server.Implementations/Security/AuthorizationContext.cs # MediaBrowser.Controller/Library/IUserManager.cs # MediaBrowser.Controller/Net/ISessionContext.cs
Diffstat (limited to 'Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs')
-rw-r--r--Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs91
1 files changed, 91 insertions, 0 deletions
diff --git a/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs b/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
new file mode 100644
index 000000000..310a3d31a
--- /dev/null
+++ b/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs
@@ -0,0 +1,91 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using MediaBrowser.Common.Extensions;
+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 nothing in the value.
+ if (value.Count != 1)
+ {
+ _store = value;
+ return;
+ }
+
+ // Encoded querystrings have no value, so don't process anything if a value is present.
+ var (key, stringValues) = value.First();
+ if (!string.IsNullOrEmpty(stringValues))
+ {
+ _store = value;
+ return;
+ }
+
+ // Unencode and re-parse querystring.
+ var unencodedKey = HttpUtility.UrlDecode(key);
+
+ if (string.Equals(unencodedKey, key, StringComparison.Ordinal))
+ {
+ // Don't do anything if it's not encoded.
+ _store = value;
+ return;
+ }
+
+ var pairs = new Dictionary<string, StringValues>();
+ var queryString = unencodedKey.SpanSplit('&');
+
+ foreach (var pair in queryString)
+ {
+ var i = pair.IndexOf('=');
+ if (i == -1)
+ {
+ // encoded is an equals.
+ // We use TryAdd so duplicate keys get ignored
+ pairs.TryAdd(pair.ToString(), StringValues.Empty);
+ continue;
+ }
+
+ var k = pair[..i].ToString();
+ var v = pair[(i + 1)..].ToString();
+ if (!pairs.TryAdd(k, new StringValues(v)))
+ {
+ pairs[k] = StringValues.Concat(pairs[k], v);
+ }
+ }
+
+ _store = new QueryCollection(pairs);
+ }
+ }
+ }
+}