2021-05-12 15:19:08 +00:00
|
|
|
using System;
|
2021-05-05 21:52:39 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-06-19 16:02:33 +00:00
|
|
|
using Jellyfin.Extensions;
|
2021-05-05 21:52:39 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
|
|
using Microsoft.Extensions.Primitives;
|
|
|
|
|
2023-01-14 02:37:37 +00:00
|
|
|
namespace Jellyfin.Api.Middleware
|
2021-05-05 21:52:39 +00:00
|
|
|
{
|
|
|
|
/// <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
|
|
|
|
{
|
2021-05-05 22:14:05 +00:00
|
|
|
// Only interested in where the querystring is encoded which shows up as one key with nothing in the value.
|
2021-05-05 21:52:39 +00:00
|
|
|
if (value.Count != 1)
|
|
|
|
{
|
|
|
|
_store = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-05 22:14:05 +00:00
|
|
|
// Encoded querystrings have no value, so don't process anything if a value is present.
|
2021-06-07 15:22:11 +00:00
|
|
|
var (key, stringValues) = value.First();
|
2021-06-07 15:22:37 +00:00
|
|
|
if (!string.IsNullOrEmpty(stringValues))
|
2021-05-05 21:52:39 +00:00
|
|
|
{
|
|
|
|
_store = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 12:14:31 +00:00
|
|
|
if (!key.Contains('=', StringComparison.Ordinal))
|
2021-09-25 16:15:46 +00:00
|
|
|
{
|
|
|
|
_store = value;
|
|
|
|
return;
|
|
|
|
}
|
2021-05-05 21:52:39 +00:00
|
|
|
|
2021-09-25 16:15:46 +00:00
|
|
|
var pairs = new Dictionary<string, StringValues>();
|
|
|
|
foreach (var pair in key.SpanSplit('&'))
|
2021-05-05 21:52:39 +00:00
|
|
|
{
|
2021-05-12 15:19:08 +00:00
|
|
|
var i = pair.IndexOf('=');
|
2021-05-08 15:00:41 +00:00
|
|
|
if (i == -1)
|
2021-05-07 13:02:42 +00:00
|
|
|
{
|
2021-05-08 11:52:25 +00:00
|
|
|
// encoded is an equals.
|
2021-06-08 20:26:59 +00:00
|
|
|
// We use TryAdd so duplicate keys get ignored
|
|
|
|
pairs.TryAdd(pair.ToString(), StringValues.Empty);
|
2021-05-08 11:52:25 +00:00
|
|
|
continue;
|
2021-05-07 13:02:42 +00:00
|
|
|
}
|
2021-05-08 11:52:25 +00:00
|
|
|
|
2021-06-08 20:26:59 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-05-05 21:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_store = new QueryCollection(pairs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|