mscorlib(4.0.0.0) API with additions
UriBuilder.cs
2 using System.Text;
3 using System.Threading;
4 
5 namespace System
6 {
8  [global::__DynamicallyInvokable]
9  public class UriBuilder
10  {
11  private bool m_changed = true;
12 
13  private string m_fragment = string.Empty;
14 
15  private string m_host = "localhost";
16 
17  private string m_password = string.Empty;
18 
19  private string m_path = "/";
20 
21  private int m_port = -1;
22 
23  private string m_query = string.Empty;
24 
25  private string m_scheme = "http";
26 
27  private string m_schemeDelimiter = Uri.SchemeDelimiter;
28 
29  private Uri m_uri;
30 
31  private string m_username = string.Empty;
32 
33  private string Extra
34  {
35  set
36  {
37  if (value == null)
38  {
39  value = string.Empty;
40  }
41  if (value.Length > 0)
42  {
43  if (value[0] == '#')
44  {
45  Fragment = value.Substring(1);
46  return;
47  }
48  if (value[0] != '?')
49  {
50  throw new ArgumentException("value");
51  }
52  int num = value.IndexOf('#');
53  if (num == -1)
54  {
55  num = value.Length;
56  }
57  else
58  {
59  Fragment = value.Substring(num + 1);
60  }
61  Query = value.Substring(1, num - 1);
62  }
63  else
64  {
65  Fragment = string.Empty;
66  Query = string.Empty;
67  }
68  }
69  }
70 
73  [global::__DynamicallyInvokable]
74  public string Fragment
75  {
76  [global::__DynamicallyInvokable]
77  get
78  {
79  return m_fragment;
80  }
81  [global::__DynamicallyInvokable]
82  set
83  {
84  if (value == null)
85  {
86  value = string.Empty;
87  }
88  if (value.Length > 0)
89  {
90  value = "#" + value;
91  }
92  m_fragment = value;
93  m_changed = true;
94  }
95  }
96 
99  [global::__DynamicallyInvokable]
100  public string Host
101  {
102  [global::__DynamicallyInvokable]
103  get
104  {
105  return m_host;
106  }
107  [global::__DynamicallyInvokable]
108  set
109  {
110  if (value == null)
111  {
112  value = string.Empty;
113  }
114  m_host = value;
115  if (m_host.IndexOf(':') >= 0 && m_host[0] != '[')
116  {
117  m_host = "[" + m_host + "]";
118  }
119  m_changed = true;
120  }
121  }
122 
125  [global::__DynamicallyInvokable]
126  public string Password
127  {
128  [global::__DynamicallyInvokable]
129  get
130  {
131  return m_password;
132  }
133  [global::__DynamicallyInvokable]
134  set
135  {
136  if (value == null)
137  {
138  value = string.Empty;
139  }
140  m_password = value;
141  m_changed = true;
142  }
143  }
144 
147  [global::__DynamicallyInvokable]
148  public string Path
149  {
150  [global::__DynamicallyInvokable]
151  get
152  {
153  return m_path;
154  }
155  [global::__DynamicallyInvokable]
156  set
157  {
158  if (value == null || value.Length == 0)
159  {
160  value = "/";
161  }
162  m_path = Uri.InternalEscapeString(ConvertSlashes(value));
163  m_changed = true;
164  }
165  }
166 
170  [global::__DynamicallyInvokable]
171  public int Port
172  {
173  [global::__DynamicallyInvokable]
174  get
175  {
176  return m_port;
177  }
178  [global::__DynamicallyInvokable]
179  set
180  {
181  if (value < -1 || value > 65535)
182  {
183  throw new ArgumentOutOfRangeException("value");
184  }
185  m_port = value;
186  m_changed = true;
187  }
188  }
189 
192  [global::__DynamicallyInvokable]
193  public string Query
194  {
195  [global::__DynamicallyInvokable]
196  get
197  {
198  return m_query;
199  }
200  [global::__DynamicallyInvokable]
201  set
202  {
203  if (value == null)
204  {
205  value = string.Empty;
206  }
207  if (value.Length > 0)
208  {
209  value = "?" + value;
210  }
211  m_query = value;
212  m_changed = true;
213  }
214  }
215 
219  [global::__DynamicallyInvokable]
220  public string Scheme
221  {
222  [global::__DynamicallyInvokable]
223  get
224  {
225  return m_scheme;
226  }
227  [global::__DynamicallyInvokable]
228  set
229  {
230  if (value == null)
231  {
232  value = string.Empty;
233  }
234  int num = value.IndexOf(':');
235  if (num != -1)
236  {
237  value = value.Substring(0, num);
238  }
239  if (value.Length != 0)
240  {
241  if (!Uri.CheckSchemeName(value))
242  {
243  throw new ArgumentException("value");
244  }
245  value = value.ToLower(CultureInfo.InvariantCulture);
246  }
247  m_scheme = value;
248  m_changed = true;
249  }
250  }
251 
256  [global::__DynamicallyInvokable]
257  public Uri Uri
258  {
259  [global::__DynamicallyInvokable]
260  get
261  {
262  if (m_changed)
263  {
264  m_uri = new Uri(ToString());
265  SetFieldsFromUri(m_uri);
266  m_changed = false;
267  }
268  return m_uri;
269  }
270  }
271 
274  [global::__DynamicallyInvokable]
275  public string UserName
276  {
277  [global::__DynamicallyInvokable]
278  get
279  {
280  return m_username;
281  }
282  [global::__DynamicallyInvokable]
283  set
284  {
285  if (value == null)
286  {
287  value = string.Empty;
288  }
289  m_username = value;
290  m_changed = true;
291  }
292  }
293 
295  [global::__DynamicallyInvokable]
296  public UriBuilder()
297  {
298  }
299 
308  [global::__DynamicallyInvokable]
309  public UriBuilder(string uri)
310  {
311  Uri uri2 = new Uri(uri, UriKind.RelativeOrAbsolute);
312  if (uri2.IsAbsoluteUri)
313  {
314  Init(uri2);
315  return;
316  }
317  uri = Uri.UriSchemeHttp + Uri.SchemeDelimiter + uri;
318  Init(new Uri(uri));
319  }
320 
325  [global::__DynamicallyInvokable]
326  public UriBuilder(Uri uri)
327  {
328  if ((object)uri == null)
329  {
330  throw new ArgumentNullException("uri");
331  }
332  Init(uri);
333  }
334 
335  private void Init(Uri uri)
336  {
337  m_fragment = uri.Fragment;
338  m_query = uri.Query;
339  m_host = uri.Host;
340  m_path = uri.AbsolutePath;
341  m_port = uri.Port;
342  m_scheme = uri.Scheme;
343  m_schemeDelimiter = (uri.HasAuthority ? Uri.SchemeDelimiter : ":");
344  string userInfo = uri.UserInfo;
345  if (!string.IsNullOrEmpty(userInfo))
346  {
347  int num = userInfo.IndexOf(':');
348  if (num != -1)
349  {
350  m_password = userInfo.Substring(num + 1);
351  m_username = userInfo.Substring(0, num);
352  }
353  else
354  {
355  m_username = userInfo;
356  }
357  }
358  SetFieldsFromUri(uri);
359  }
360 
364  [global::__DynamicallyInvokable]
365  public UriBuilder(string schemeName, string hostName)
366  {
367  Scheme = schemeName;
368  Host = hostName;
369  }
370 
377  [global::__DynamicallyInvokable]
378  public UriBuilder(string scheme, string host, int portNumber)
379  : this(scheme, host)
380  {
381  Port = portNumber;
382  }
383 
391  [global::__DynamicallyInvokable]
392  public UriBuilder(string scheme, string host, int port, string pathValue)
393  : this(scheme, host, port)
394  {
395  Path = pathValue;
396  }
397 
408  [global::__DynamicallyInvokable]
409  public UriBuilder(string scheme, string host, int port, string path, string extraValue)
410  : this(scheme, host, port, path)
411  {
412  try
413  {
414  Extra = extraValue;
415  }
416  catch (Exception ex)
417  {
419  {
420  throw;
421  }
422  throw new ArgumentException("extraValue");
423  }
424  }
425 
426  private string ConvertSlashes(string path)
427  {
428  StringBuilder stringBuilder = new StringBuilder(path.Length);
429  for (int i = 0; i < path.Length; i++)
430  {
431  char c = path[i];
432  if (c == '\\')
433  {
434  c = '/';
435  }
436  stringBuilder.Append(c);
437  }
438  return stringBuilder.ToString();
439  }
440 
445  [global::__DynamicallyInvokable]
446  public override bool Equals(object rparam)
447  {
448  if (rparam == null)
449  {
450  return false;
451  }
452  return Uri.Equals(rparam.ToString());
453  }
454 
457  [global::__DynamicallyInvokable]
458  public override int GetHashCode()
459  {
460  return Uri.GetHashCode();
461  }
462 
463  private void SetFieldsFromUri(Uri uri)
464  {
465  m_fragment = uri.Fragment;
466  m_query = uri.Query;
467  m_host = uri.Host;
468  m_path = uri.AbsolutePath;
469  m_port = uri.Port;
470  m_scheme = uri.Scheme;
471  m_schemeDelimiter = (uri.HasAuthority ? Uri.SchemeDelimiter : ":");
472  string userInfo = uri.UserInfo;
473  if (userInfo.Length > 0)
474  {
475  int num = userInfo.IndexOf(':');
476  if (num != -1)
477  {
478  m_password = userInfo.Substring(num + 1);
479  m_username = userInfo.Substring(0, num);
480  }
481  else
482  {
483  m_username = userInfo;
484  }
485  }
486  }
487 
492  [global::__DynamicallyInvokable]
493  public override string ToString()
494  {
495  if (m_username.Length == 0 && m_password.Length > 0)
496  {
497  throw new UriFormatException(SR.GetString("net_uri_BadUserPassword"));
498  }
499  if (m_scheme.Length != 0)
500  {
501  UriParser syntax = UriParser.GetSyntax(m_scheme);
502  if (syntax != null)
503  {
504  m_schemeDelimiter = ((syntax.InFact(UriSyntaxFlags.MustHaveAuthority) || (m_host.Length != 0 && syntax.NotAny(UriSyntaxFlags.MailToLikeUri) && syntax.InFact(UriSyntaxFlags.OptionalAuthority))) ? Uri.SchemeDelimiter : ":");
505  }
506  else
507  {
508  m_schemeDelimiter = ((m_host.Length != 0) ? Uri.SchemeDelimiter : ":");
509  }
510  }
511  string text = (m_scheme.Length != 0) ? (m_scheme + m_schemeDelimiter) : string.Empty;
512  return text + m_username + ((m_password.Length > 0) ? (":" + m_password) : string.Empty) + ((m_username.Length > 0) ? "@" : string.Empty) + m_host + ((m_port != -1 && m_host.Length > 0) ? (":" + m_port) : string.Empty) + ((m_host.Length > 0 && m_path.Length != 0 && m_path[0] != '/') ? "/" : string.Empty) + m_path + m_query + m_fragment;
513  }
514  }
515 }
UriKind
Defines the kinds of T:System.Uris for the M:System.Uri.IsWellFormedUriString(System....
Definition: UriKind.cs:5
UriBuilder(string scheme, string host, int port, string path, string extraValue)
Initializes a new instance of the T:System.UriBuilder class with the specified scheme,...
Definition: UriBuilder.cs:409
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
int Port
Gets the port number of this URI.
Definition: Uri.cs:617
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static readonly string UriSchemeHttp
Specifies that the URI is accessed through the Hypertext Transfer Protocol (HTTP)....
Definition: Uri.cs:154
string Path
Gets or sets the path to the resource referenced by the URI.
Definition: UriBuilder.cs:149
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
The exception that is thrown when the execution stack overflows because it contains too many nested m...
override int GetHashCode()
Returns the hash code for the URI.
Definition: UriBuilder.cs:458
string Scheme
Gets or sets the scheme name of the URI.
Definition: UriBuilder.cs:221
Definition: __Canon.cs:3
string Scheme
Gets the scheme name for this URI.
Definition: Uri.cs:702
The exception that is thrown when the value of an argument is outside the allowable range of values a...
UriBuilder(string scheme, string host, int port, string pathValue)
Initializes a new instance of the T:System.UriBuilder class with the specified scheme,...
Definition: UriBuilder.cs:392
UriBuilder(string scheme, string host, int portNumber)
Initializes a new instance of the T:System.UriBuilder class with the specified scheme,...
Definition: UriBuilder.cs:378
string Host
Gets or sets the Domain Name System (DNS) host name or IP address of a server.
Definition: UriBuilder.cs:101
override int GetHashCode()
Gets the hash code for the URI.
Definition: Uri.cs:1630
bool IsAbsoluteUri
Gets whether the T:System.Uri instance is absolute.
Definition: Uri.cs:820
string AbsolutePath
Gets the absolute path of the URI.
Definition: Uri.cs:303
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
UriBuilder(string uri)
Initializes a new instance of the T:System.UriBuilder class with the specified URI.
Definition: UriBuilder.cs:309
Uri Uri
Gets the T:System.Uri instance constructed by the specified T:System.UriBuilder instance.
Definition: UriBuilder.cs:258
override bool Equals(object rparam)
Compares an existing T:System.Uri instance with the contents of the T:System.UriBuilder for equality.
Definition: UriBuilder.cs:446
string Query
Gets any query information included in the specified URI.
Definition: Uri.cs:646
unsafe override bool Equals(object comparand)
Compares two T:System.Uri instances for equality.
Definition: Uri.cs:1731
The exception that is thrown when there is not enough memory to continue the execution of a program.
int Port
Gets or sets the port number of the URI.
Definition: UriBuilder.cs:172
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
Provides a custom constructor for uniform resource identifiers (URIs) and modifies URIs for the T:Sys...
Definition: UriBuilder.cs:9
The exception that is thrown when one of the arguments provided to a method is not valid.
string Fragment
Gets or sets the fragment portion of the URI.
Definition: UriBuilder.cs:75
override string ToString()
Returns the display string for the specified T:System.UriBuilder instance.
Definition: UriBuilder.cs:493
string? Query
Gets or sets any query information included in the URI.
Definition: UriBuilder.cs:194
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
string UserInfo
Gets the user name, password, or other user-specific information associated with the specified URI.
Definition: Uri.cs:845
string Fragment
Gets the escaped URI fragment.
Definition: Uri.cs:674
UriBuilder(string schemeName, string hostName)
Initializes a new instance of the T:System.UriBuilder class with the specified scheme and host.
Definition: UriBuilder.cs:365
static readonly string SchemeDelimiter
Specifies the characters that separate the communication protocol scheme from the address portion of ...
Definition: Uri.cs:179
static bool CheckSchemeName(string schemeName)
Determines whether the specified scheme name is valid.
Definition: Uri.cs:1576
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when a call is made to the M:System.Threading.Thread....
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
Definition: Uri.cs:19
UriBuilder(Uri uri)
Initializes a new instance of the T:System.UriBuilder class with the specified T:System....
Definition: UriBuilder.cs:326
Parses a new URI scheme. This is an abstract class.
Definition: UriParser.cs:9
string Host
Gets the host component of this instance.
Definition: Uri.cs:587
string Password
Gets or sets the password associated with the user that accesses the URI.
Definition: UriBuilder.cs:127
string UserName
The user name associated with the user that accesses the URI.
Definition: UriBuilder.cs:276
UriBuilder()
Initializes a new instance of the T:System.UriBuilder class.
Definition: UriBuilder.cs:296
The exception that is thrown when an invalid Uniform Resource Identifier (URI) is detected.