-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUrlInfo.cs
245 lines (220 loc) · 6.05 KB
/
UrlInfo.cs
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Text.RegularExpressions;
using Inversion.Collections;
namespace Inversion.Web {
/// <summary>
/// Represent the structure of a url.
/// </summary>
public class UrlInfo : ICloneable {
private const string DefaultRegexSpec = @"
(?<protocol>http|ftp|https|ftps)://
(?<domain>[^/\r\n\?]+)
(?<path>
(?<app_path>/[^\r\n\?]*)*
/
(?<file>
(?<resource>[^\#\r\n\?]+)
\.
(?<extension>[^\#\r\n\?\./]+)
)
(?<tail>/[^\#\r\n\?]*)?
)?
(?<anchor>\#[^\#\r\n\?]*)?
(?:\?)?
(?<query>[^\#\r\n]*)?
";
/// <summary>
/// The default regex that is used to deconstruct urls.
/// </summary>
public static readonly Regex DefaultRegex = new Regex(
DefaultRegexSpec,
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled
);
private readonly string _url;
private readonly Regex _regex;
private IDictionary<string,string> _query;
private Match _match;
/// <summary>
/// The regular expression being used to break URLs
/// down into their parts.
/// </summary>
/// <remarks>
/// If a regular expression isn't provided via the contructor
/// then the default <see cref="UrlInfo.DefaultRegex"/> is used.
/// </remarks>
public Regex Regex {
get { return _regex ?? UrlInfo.DefaultRegex; }
}
/// <summary>
/// The matches produced by matching the
/// <see cref="Regex"/> against the <see cref="Url"/>.
/// </summary>
public Match Match {
get {
return _match;
}
}
/// <summary>
/// The url being processed.
/// </summary>
public string Url {
get { return _url; }
}
/// <summary>
/// The protocol specified by the <see cref="Url"/>
/// </summary>
public string Protocol {
get {
return this.Match.Groups["protocol"].Value;
}
}
/// <summary>
/// The domain specified by the <see cref="Url"/>
/// </summary>
public string Domain {
get {
return this.Match.Groups["domain"].Value;
}
}
/// <summary>
/// The full path specified by the <see cref="Url"/>
/// </summary>
public string FullPath {
get {
return this.Match.Groups["path"].Value;
}
}
/// <summary>
/// The application path specified by the <see cref="Url"/>
/// </summary>
public string AppPath {
get {
return this.Match.Groups["app_path"].Value;
}
}
/// <summary>
/// The URL of the current application.
/// </summary>
public string AppUrl {
get {
return String.Concat(this.Protocol, "://", this.Domain, this.AppPath);
}
}
/// <summary>
/// The file specified by the <see cref="Url"/>
/// </summary>
public string File {
get {
return this.Match.Groups["file"].Value;
}
}
/// <summary>
/// The extension specified by the <see cref="Url"/>
/// </summary>
public string Extension {
get {
return this.Match.Groups["extension"].Value;
}
}
/// <summary>
/// The tail specified by the <see cref="Url"/>
/// </summary>
public string Tail {
get {
return this.Match.Groups["tail"].Value;
}
}
/// <summary>
/// The query string specified by the <see cref="Url"/>
/// </summary>
public string QueryString {
get {
return this.Match.Groups["query"].Value;
}
}
/// <summary>
/// A name / value dictionary as the propduct of
/// parsing the <see cref="QueryString"/>
/// </summary>
public IDictionary<string, string> Query {
get {
return _query;
}
}
/// <summary>
/// Instantiates a new url-info object from the uri object provided.
/// </summary>
/// <param name="uri">The uri object to contrsut the url-info from.</param>
public UrlInfo(Uri uri) : this(uri, null) { }
/// <summary>
/// Instantiates a new url-info object from the url string representation provided.
/// </summary>
/// <param name="url">The url to construct the url-info from.</param>
public UrlInfo(string url) : this(url, null) { }
/// <summary>
/// Instantiates a new url-info object from the uri provided
/// using the regex provided to deconstruct it.
/// </summary>
/// <param name="uri">The uri object to contrsut the url-info from.</param>
/// <param name="regex">The regex to use in deconstructing the uri.</param>
public UrlInfo(Uri uri, Regex regex) : this(uri.ToString(), regex) { }
/// <summary>
/// Instantiates a new url-info object from the url string representation provided,
/// using the regex provided to deconstruct it.
/// </summary>
/// <param name="url">The url to construct the url-info from.</param>
/// <param name="regex">The regex to use in deconstructing the uri.</param>
public UrlInfo(string url, Regex regex) {
_url = url;
_regex = regex ?? DefaultRegex;
this.ProcessUrl();
}
/// <summary>
/// Instantiates a url-info object as a copy
/// of the url-info object provided.
/// </summary>
/// <param name="info">The url-info obect to create a copy from.</param>
public UrlInfo(UrlInfo info) {
_url = info.Url;
_regex = info.Regex;
_query = new DataDictionary<string>(info.Query);
}
/// <summary>
/// Processes the url with a deconstruction regex.
/// </summary>
public void ProcessUrl() {
_match = this.Regex.Match(_url);
if (!String.IsNullOrWhiteSpace(this.QueryString)) {
Dictionary<string,string> qs = new Dictionary<string, string>();
foreach (string term in this.QueryString.Split(new string[] {"&"}, StringSplitOptions.RemoveEmptyEntries)) {
string[] pair = term.Split(new string[] {"="}, StringSplitOptions.RemoveEmptyEntries);
switch (pair.Length) {
case 2:
qs[pair[0]] = pair[1];
break;
case 1:
qs[pair[0]] = pair[0];
break;
}
}
_query = qs.ToImmutableDictionary();
} else {
_query = ImmutableDictionary<string, string>.Empty;
}
}
object ICloneable.Clone() {
return new UrlInfo(this);
}
/// <summary>
/// Instantiates a new url-info object that is a copy
/// of the current instance.
/// </summary>
/// <returns></returns>
public UrlInfo Clone() {
return new UrlInfo(this);
}
}
}