-//\r
-// System.UriBuilder\r
-//\r
-// Author:\r
-// Lawrence Pit (loz@cable.a2000.nl)\r
-//\r
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)\r
-//\r
-// Permission is hereby granted, free of charge, to any person obtaining\r
-// a copy of this software and associated documentation files (the\r
-// "Software"), to deal in the Software without restriction, including\r
-// without limitation the rights to use, copy, modify, merge, publish,\r
-// distribute, sublicense, and/or sell copies of the Software, and to\r
-// permit persons to whom the Software is furnished to do so, subject to\r
-// the following conditions:\r
-// \r
-// The above copyright notice and this permission notice shall be\r
-// included in all copies or substantial portions of the Software.\r
-// \r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
-//\r
-\r
-using System;\r
-using System.Collections;\r
-using System.Runtime.Serialization;\r
-using System.Text;\r
-\r
-// See RFC 2396 for more info on URI's.\r
-\r
-namespace System \r
-{\r
- public class UriBuilder\r
- {\r
- private string scheme;\r
- private string host;\r
- private int port;\r
- private string path;\r
- private string query;\r
- private string fragment;\r
- private string username;\r
- private string password;\r
- \r
- private Uri uri;\r
- private bool modified;\r
- \r
- \r
- // Constructors\r
- \r
- public UriBuilder ()\r
-#if NET_2_0\r
- : this (Uri.UriSchemeHttp, "localhost")\r
-#else\r
- : this (Uri.UriSchemeHttp, "loopback")\r
-#endif\r
- {\r
- }\r
-\r
- public UriBuilder (string uri) : this (new Uri (uri))\r
- {\r
- }\r
- \r
- public UriBuilder (Uri uri)\r
- {\r
- scheme = uri.Scheme;\r
- host = uri.Host;\r
- port = uri.Port;\r
- path = uri.AbsolutePath;\r
- query = uri.Query;\r
- fragment = uri.Fragment;\r
- username = uri.UserInfo;\r
- int pos = username.IndexOf (':');\r
- if (pos != -1) {\r
- password = username.Substring (pos + 1);\r
- username = username.Substring (0, pos);\r
- } else {\r
- password = String.Empty;\r
- }\r
- modified = true;\r
- }\r
- \r
- public UriBuilder (string schemeName, string hostName) \r
- {\r
- Scheme = schemeName;\r
- Host = hostName;\r
- port = -1;\r
- Path = String.Empty; // dependent on scheme it may set path to "/"\r
- query = String.Empty;\r
- fragment = String.Empty;\r
- username = String.Empty;\r
- password = String.Empty;\r
- modified = true;\r
- }\r
- \r
- public UriBuilder (string scheme, string host, int portNumber) \r
- : this (scheme, host)\r
- {\r
- Port = portNumber;\r
- }\r
- \r
- public UriBuilder (string scheme, string host, int port, string pathValue)\r
- : this (scheme, host, port)\r
- {\r
- Path = pathValue;\r
- }\r
-\r
- public UriBuilder (string scheme, string host, int port, string pathValue, string extraValue)\r
- : this (scheme, host, port, pathValue)\r
- {\r
- if (extraValue == null || extraValue.Length == 0)\r
- return;\r
- \r
- if (extraValue [0] == '#') \r
- Fragment = extraValue.Remove (0, 1);\r
- else if (extraValue [0] == '?') \r
- Query = extraValue.Remove (0, 1);\r
- else \r
- throw new ArgumentException ("extraValue");\r
- }\r
-\r
- \r
- // Properties\r
- \r
- public string Fragment {\r
- get { return fragment; }\r
- set {\r
- fragment = value;\r
- if (fragment == null)\r
- fragment = String.Empty;\r
- else if (fragment.Length > 0)\r
-// fragment = "#" + EncodeUtf8 (value.Replace ("%23", "#"));\r
- fragment = "#" + value.Replace ("%23", "#");\r
-#if !NET_2_0\r
- query = String.Empty;\r
-#endif\r
- modified = true;\r
- }\r
- }\r
-\r
- public string Host {\r
- get { return host; }\r
- set {\r
- host = (value == null) ? String.Empty : value;;\r
- modified = true;\r
- }\r
- }\r
-\r
- public string Password {\r
- get { return password; }\r
- set {\r
- password = (value == null) ? String.Empty : value;;\r
- modified = true;\r
- }\r
- }\r
- \r
- public string Path {\r
- get { return path; }\r
- set {\r
- if (value == null || value.Length == 0) {\r
- path = "/";\r
- } else {\r
- path = Uri.EscapeString (value.Replace ('\\', '/'), Uri.EscapeCommonHexBrackets);\r
- }\r
- modified = true;\r
- }\r
- }\r
- \r
- public int Port {\r
- get { return port; }\r
- set {\r
-#if NET_2_0\r
- if (value < -1)\r
- throw new ArgumentOutOfRangeException ("value");\r
-#else\r
- if (value < 0)\r
- throw new ArgumentOutOfRangeException ("value");\r
-#endif\r
- // apparently it is\r
- port = value;\r
- modified = true;\r
- }\r
- }\r
- \r
- public string Query {\r
- get { return query; }\r
- set {\r
- // LAMESPEC: it doesn't say to always prepend a \r
- // question mark to the value.. it does say this \r
- // for fragment.\r
- if (value == null || value.Length == 0)\r
- query = String.Empty;\r
- else\r
-// query = "?" + EncodeUtf8 (value);\r
- query = "?" + value;\r
-#if !NET_2_0\r
- fragment = String.Empty;\r
-#endif\r
- modified = true;\r
- }\r
- }\r
- \r
- public string Scheme {\r
- get { return scheme; }\r
- set {\r
- if (value == null)\r
- value = String.Empty;\r
- int colonPos = value.IndexOf (':');\r
- if (colonPos != -1)\r
- value = value.Substring (0, colonPos);\r
- scheme = value.ToLower ();\r
- modified = true;\r
- }\r
- }\r
- \r
- public Uri Uri {\r
- get {\r
- if (!modified) \r
- return uri;\r
- uri = new Uri (ToString (), true);\r
- modified = false;\r
- return uri;\r
- }\r
- }\r
- \r
- public string UserName {\r
- get { return username; }\r
- set {\r
- username = (value == null) ? String.Empty : value;;\r
- modified = true;\r
- }\r
- }\r
-\r
- // Methods\r
- \r
- public override bool Equals (object rparam) \r
- {\r
- return (rparam == null) ? false : this.Uri.Equals (rparam.ToString ());\r
- }\r
- \r
- public override int GetHashCode ()\r
- {\r
- return this.Uri.GetHashCode ();\r
- }\r
- \r
- public override string ToString ()\r
- {\r
- StringBuilder builder = new StringBuilder ();\r
-\r
- builder.Append (scheme);\r
- builder.Append ("://");\r
-\r
- if (username != String.Empty) {\r
- builder.Append (username);\r
- if (password != String.Empty)\r
- builder.Append (":" + password);\r
- builder.Append ('@');\r
- }\r
-\r
- builder.Append (host);\r
- if (port > 0)\r
- builder.Append (":" + port);\r
-\r
- if (path != String.Empty &&\r
- builder [builder.Length - 1] != '/' &&\r
- path.Length > 0 && path [0] != '/')\r
- builder.Append ('/');\r
- builder.Append (path);\r
- builder.Append (query);\r
- builder.Append (fragment);\r
-\r
- return builder.ToString ();\r
- }\r
- }\r
-}\r
+//
+// System.UriBuilder
+//
+// Author:
+// Lawrence Pit (loz@cable.a2000.nl)
+//
+// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+using System.Runtime.Serialization;
+using System.Text;
+
+// See RFC 2396 for more info on URI's.
+
+namespace System
+{
+ public class UriBuilder
+ {
+ private string scheme;
+ private string host;
+ private int port;
+ private string path;
+ private string query;
+ private string fragment;
+ private string username;
+ private string password;
+
+ private Uri uri;
+ private bool modified;
+
+
+ // Constructors
+
+ public UriBuilder ()
+#if NET_2_0
+ : this (Uri.UriSchemeHttp, "localhost")
+#else
+ : this (Uri.UriSchemeHttp, "loopback")
+#endif
+ {
+ }
+
+ public UriBuilder (string uri) : this (new Uri (uri))
+ {
+ }
+
+ public UriBuilder (Uri uri)
+ {
+ scheme = uri.Scheme;
+ host = uri.Host;
+ port = uri.Port;
+ path = uri.AbsolutePath;
+ query = uri.Query;
+ fragment = uri.Fragment;
+ username = uri.UserInfo;
+ int pos = username.IndexOf (':');
+ if (pos != -1) {
+ password = username.Substring (pos + 1);
+ username = username.Substring (0, pos);
+ } else {
+ password = String.Empty;
+ }
+ modified = true;
+ }
+
+ public UriBuilder (string schemeName, string hostName)
+ {
+ Scheme = schemeName;
+ Host = hostName;
+ port = -1;
+ Path = String.Empty; // dependent on scheme it may set path to "/"
+ query = String.Empty;
+ fragment = String.Empty;
+ username = String.Empty;
+ password = String.Empty;
+ modified = true;
+ }
+
+ public UriBuilder (string scheme, string host, int portNumber)
+ : this (scheme, host)
+ {
+ Port = portNumber;
+ }
+
+ public UriBuilder (string scheme, string host, int port, string pathValue)
+ : this (scheme, host, port)
+ {
+ Path = pathValue;
+ }
+
+ public UriBuilder (string scheme, string host, int port, string pathValue, string extraValue)
+ : this (scheme, host, port, pathValue)
+ {
+ if (extraValue == null || extraValue.Length == 0)
+ return;
+
+ if (extraValue [0] == '#')
+ Fragment = extraValue.Remove (0, 1);
+ else if (extraValue [0] == '?')
+ Query = extraValue.Remove (0, 1);
+ else
+ throw new ArgumentException ("extraValue");
+ }
+
+
+ // Properties
+
+ public string Fragment {
+ get { return fragment; }
+ set {
+ fragment = value;
+ if (fragment == null)
+ fragment = String.Empty;
+ else if (fragment.Length > 0)
+// fragment = "#" + EncodeUtf8 (value.Replace ("%23", "#"));
+ fragment = "#" + value.Replace ("%23", "#");
+#if !NET_2_0
+ query = String.Empty;
+#endif
+ modified = true;
+ }
+ }
+
+ public string Host {
+ get { return host; }
+ set {
+ host = (value == null) ? String.Empty : value;;
+ modified = true;
+ }
+ }
+
+ public string Password {
+ get { return password; }
+ set {
+ password = (value == null) ? String.Empty : value;;
+ modified = true;
+ }
+ }
+
+ public string Path {
+ get { return path; }
+ set {
+ if (value == null || value.Length == 0) {
+ path = "/";
+ } else {
+ path = Uri.EscapeString (value.Replace ('\\', '/'), Uri.EscapeCommonHexBrackets);
+ }
+ modified = true;
+ }
+ }
+
+ public int Port {
+ get { return port; }
+ set {
+#if NET_2_0
+ if (value < -1)
+ throw new ArgumentOutOfRangeException ("value");
+#else
+ if (value < 0)
+ throw new ArgumentOutOfRangeException ("value");
+#endif
+ // apparently it is
+ port = value;
+ modified = true;
+ }
+ }
+
+ public string Query {
+ get { return query; }
+ set {
+ // LAMESPEC: it doesn't say to always prepend a
+ // question mark to the value.. it does say this
+ // for fragment.
+ if (value == null || value.Length == 0)
+ query = String.Empty;
+ else
+// query = "?" + EncodeUtf8 (value);
+ query = "?" + value;
+#if !NET_2_0
+ fragment = String.Empty;
+#endif
+ modified = true;
+ }
+ }
+
+ public string Scheme {
+ get { return scheme; }
+ set {
+ if (value == null)
+ value = String.Empty;
+ int colonPos = value.IndexOf (':');
+ if (colonPos != -1)
+ value = value.Substring (0, colonPos);
+ scheme = value.ToLower ();
+ modified = true;
+ }
+ }
+
+ public Uri Uri {
+ get {
+ if (!modified)
+ return uri;
+ uri = new Uri (ToString (), true);
+ modified = false;
+ return uri;
+ }
+ }
+
+ public string UserName {
+ get { return username; }
+ set {
+ username = (value == null) ? String.Empty : value;;
+ modified = true;
+ }
+ }
+
+ // Methods
+
+ public override bool Equals (object rparam)
+ {
+ return (rparam == null) ? false : this.Uri.Equals (rparam.ToString ());
+ }
+
+ public override int GetHashCode ()
+ {
+ return this.Uri.GetHashCode ();
+ }
+
+ public override string ToString ()
+ {
+ StringBuilder builder = new StringBuilder ();
+
+ builder.Append (scheme);
+ builder.Append ("://");
+
+ if (username != String.Empty) {
+ builder.Append (username);
+ if (password != String.Empty)
+ builder.Append (":" + password);
+ builder.Append ('@');
+ }
+
+ builder.Append (host);
+ if (port > 0)
+ builder.Append (":" + port);
+
+ if (path != String.Empty &&
+ builder [builder.Length - 1] != '/' &&
+ path.Length > 0 && path [0] != '/')
+ builder.Append ('/');
+ builder.Append (path);
+ builder.Append (query);
+ builder.Append (fragment);
+
+ return builder.ToString ();
+ }
+ }
+}