2020-02-04 00:49:27 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
using System;
|
2018-12-27 23:27:57 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Services
|
|
|
|
{
|
2019-02-21 09:07:21 +00:00
|
|
|
// Remove this garbage class, it's just a bastard copy of NameValueCollection
|
2018-12-27 23:27:57 +00:00
|
|
|
public class QueryParamCollection : List<NameValuePair>
|
|
|
|
{
|
|
|
|
public QueryParamCollection()
|
|
|
|
{
|
2019-02-25 22:34:32 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static StringComparison GetStringComparison()
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return StringComparison.OrdinalIgnoreCase;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static StringComparer GetStringComparer()
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return StringComparer.OrdinalIgnoreCase;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds a new query parameter.
|
|
|
|
/// </summary>
|
2019-03-05 19:48:04 +00:00
|
|
|
public void Add(string key, string value)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
Add(new NameValuePair(key, value));
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:48:04 +00:00
|
|
|
private void Set(string key, string value)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
{
|
|
|
|
var parameters = GetItems(key);
|
|
|
|
|
|
|
|
foreach (var p in parameters)
|
|
|
|
{
|
|
|
|
Remove(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var pair in this)
|
|
|
|
{
|
|
|
|
var stringComparison = GetStringComparison();
|
|
|
|
|
|
|
|
if (string.Equals(key, pair.Name, stringComparison))
|
|
|
|
{
|
|
|
|
pair.Value = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Add(key, value);
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:48:04 +00:00
|
|
|
private string Get(string name)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
var stringComparison = GetStringComparison();
|
|
|
|
|
|
|
|
foreach (var pair in this)
|
|
|
|
{
|
|
|
|
if (string.Equals(pair.Name, name, stringComparison))
|
|
|
|
{
|
|
|
|
return pair.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:48:04 +00:00
|
|
|
private List<NameValuePair> GetItems(string name)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
var stringComparison = GetStringComparison();
|
|
|
|
|
|
|
|
var list = new List<NameValuePair>();
|
|
|
|
|
|
|
|
foreach (var pair in this)
|
|
|
|
{
|
|
|
|
if (string.Equals(pair.Name, name, stringComparison))
|
|
|
|
{
|
|
|
|
list.Add(pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual List<string> GetValues(string name)
|
|
|
|
{
|
|
|
|
var stringComparison = GetStringComparison();
|
|
|
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
foreach (var pair in this)
|
|
|
|
{
|
|
|
|
if (string.Equals(pair.Name, name, stringComparison))
|
|
|
|
{
|
|
|
|
list.Add(pair.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<string> Keys
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var keys = new string[this.Count];
|
|
|
|
|
|
|
|
for (var i = 0; i < keys.Length; i++)
|
|
|
|
{
|
|
|
|
keys[i] = this[i].Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a query parameter value by name. A query may contain multiple values of the same name
|
|
|
|
/// (i.e. "x=1&x=2"), in which case the value is an array, which works for both getting and setting.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The query parameter name</param>
|
|
|
|
/// <returns>The query parameter value or array of values</returns>
|
|
|
|
public string this[string name]
|
|
|
|
{
|
2019-01-13 20:31:14 +00:00
|
|
|
get => Get(name);
|
|
|
|
set => Set(name, value);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetQueryStringValue(NameValuePair pair)
|
|
|
|
{
|
|
|
|
return pair.Name + "=" + pair.Value;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public override string ToString()
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2018-12-28 15:48:26 +00:00
|
|
|
var vals = this.Select(GetQueryStringValue).ToArray();
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
return string.Join("&", vals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|