This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathQueryString.cs
More file actions
146 lines (130 loc) · 5.59 KB
/
QueryString.cs
File metadata and controls
146 lines (130 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Framework.Internal;
using Microsoft.Framework.WebEncoders;
namespace Microsoft.AspNet.Http
{
/// <summary>
/// Provides correct handling for QueryString value when needed to reconstruct a request or redirect URI string
/// </summary>
public struct QueryString : IEquatable<QueryString>
{
/// <summary>
/// Represents the empty query string. This field is read-only.
/// </summary>
public static readonly QueryString Empty = new QueryString(String.Empty);
private readonly string _value;
/// <summary>
/// Initialize the query string with a given value. This value must be in escaped and delimited format with
/// a leading '?' character.
/// </summary>
/// <param name="value">The query string to be assigned to the Value property.</param>
public QueryString(string value)
{
if (!string.IsNullOrEmpty(value) && value[0] != '?')
{
throw new ArgumentException("The leading '?' must be included for a non-empty query.", "value");
}
_value = value;
}
/// <summary>
/// Initialize a query string with a single given parameter name and value.
/// </summary>
/// <param name="name">The un-encoded parameter name</param>
/// <param name="value">The un-encoded parameter value</param>
public QueryString(string name, string value)
{
_value = "?" + UrlEncoder.Default.UrlEncode(name) + '=' + UrlEncoder.Default.UrlEncode(value);
}
/// <summary>
/// The escaped query string with the leading '?' character
/// </summary>
public string Value
{
get { return _value; }
}
/// <summary>
/// True if the query string is not empty
/// </summary>
public bool HasValue
{
get { return !string.IsNullOrEmpty(_value); }
}
/// <summary>
/// Provides the query string escaped in a way which is correct for combining into the URI representation.
/// A leading '?' character will be included unless the Value is null or empty. Characters which are potentially
/// dangerous are escaped.
/// </summary>
/// <returns>The query string value</returns>
public override string ToString()
{
return ToUriComponent();
}
/// <summary>
/// Provides the query string escaped in a way which is correct for combining into the URI representation.
/// A leading '?' character will be included unless the Value is null or empty. Characters which are potentially
/// dangerous are escaped.
/// </summary>
/// <returns>The query string value</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification = "Purpose of the method is to return a string")]
public string ToUriComponent()
{
// Escape things properly so System.Uri doesn't mis-interpret the data.
return HasValue ? _value.Replace("#", "%23") : string.Empty;
}
/// <summary>
/// Returns an QueryString given the query as it is escaped in the URI format. The string MUST NOT contain any
/// value that is not a query.
/// </summary>
/// <param name="uriComponent">The escaped query as it appears in the URI format.</param>
/// <returns>The resulting QueryString</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads", Justification = "Delimiter characters ? and # must be escaped by this method instead of truncating the value")]
public static QueryString FromUriComponent(string uriComponent)
{
if (String.IsNullOrEmpty(uriComponent))
{
return new QueryString(string.Empty);
}
return new QueryString(uriComponent);
}
/// <summary>
/// Returns an QueryString given the query as from a Uri object. Relative Uri objects are not supported.
/// </summary>
/// <param name="uri">The Uri object</param>
/// <returns>The resulting QueryString</returns>
public static QueryString FromUriComponent([NotNull] Uri uri)
{
string queryValue = uri.GetComponents(UriComponents.Query, UriFormat.UriEscaped);
if (!string.IsNullOrEmpty(queryValue))
{
queryValue = "?" + queryValue;
}
return new QueryString(queryValue);
}
public bool Equals(QueryString other)
{
return string.Equals(_value, other._value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is QueryString && Equals((QueryString)obj);
}
public override int GetHashCode()
{
return (_value != null ? _value.GetHashCode() : 0);
}
public static bool operator ==(QueryString left, QueryString right)
{
return left.Equals(right);
}
public static bool operator !=(QueryString left, QueryString right)
{
return !left.Equals(right);
}
}
}