// Constructors
public UriBuilder ()
- : this (Uri.UriSchemeHttp, "localhost")
{
+ Initialize (Uri.UriSchemeHttp, "localhost", -1, String.Empty, String.Empty);
}
- public UriBuilder (string uri) : this (new Uri (uri))
+ public UriBuilder (string uri)
{
+ if (uri == null)
+ throw new ArgumentNullException ("uriString");
+
+ Uri u = null;
+ if (Uri.TryCreate (uri, UriKind.Absolute, out u)) {
+ Initialize (u);
+ } else if (!uri.Contains (Uri.SchemeDelimiter)) {
+ // second chance, UriBuilder parsing is more forgiving than Uri
+ Initialize (new Uri (Uri.UriSchemeHttp + Uri.SchemeDelimiter + uri));
+ } else
+ throw new UriFormatException ();
}
public UriBuilder (Uri uri)
{
-#if NET_4_0
if (uri == null)
throw new ArgumentNullException ("uri");
-#endif
- scheme = uri.Scheme;
- host = uri.Host;
- port = uri.Port;
- path = uri.AbsolutePath;
- query = uri.Query;
+ Initialize (uri);
+ }
+
+ public UriBuilder (string schemeName, string hostName)
+ {
+ Initialize (schemeName, hostName, -1, String.Empty, String.Empty);
+ }
+
+ public UriBuilder (string scheme, string host, int portNumber)
+ {
+ Initialize (scheme, host, portNumber, String.Empty, String.Empty);
+ }
+
+ public UriBuilder (string scheme, string host, int port, string pathValue)
+ {
+ Initialize (scheme, host, port, pathValue, String.Empty);
+ }
+
+ public UriBuilder (string scheme, string host, int port, string path, string extraValue)
+ {
+ Initialize (scheme, host, port, path, extraValue);
+ }
+
+ private void Initialize (Uri uri)
+ {
+ Initialize (uri.Scheme, uri.Host, uri.Port, uri.AbsolutePath, String.Empty);
fragment = uri.Fragment;
+ query = uri.Query;
username = uri.UserInfo;
int pos = username.IndexOf (':');
if (pos != -1) {
} else {
password = String.Empty;
}
- modified = true;
}
-
- public UriBuilder (string schemeName, string hostName)
+
+ private void Initialize (string scheme, string host, int port, string pathValue, string extraValue)
{
- Scheme = schemeName;
- Host = hostName;
- port = -1;
- Path = String.Empty; // dependent on scheme it may set path to "/"
+ modified = true;
+
+ Scheme = scheme;
+ Host = host;
+ Port = port;
+ Path = pathValue;
query = String.Empty;
fragment = String.Empty;
+ Path = pathValue;
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)
+ if (String.IsNullOrEmpty (extraValue))
return;
-
- if (extraValue [0] == '#')
+
+ if (extraValue [0] == '#')
Fragment = extraValue.Remove (0, 1);
- else if (extraValue [0] == '?')
+ else if (extraValue [0] == '?')
Query = extraValue.Remove (0, 1);
- else
+ else
throw new ArgumentException ("extraValue");
}
-
// Properties
public string Host {
get { return host; }
set {
- host = (value == null) ? String.Empty : value;
+ if (String.IsNullOrEmpty (value))
+ host = String.Empty;
+ else if ((value.IndexOf (':') != -1) && (value [0] != '[')) {
+ host = "[" + value + "]";
+ } else {
+ host = value;
+ }
modified = true;
}
}
get {
if (!modified)
return uri;
- uri = new Uri (ToString (), true);
+ uri = new Uri (ToString ());
// some properties are updated once the Uri is created - see unit tests
host = uri.Host;
+ path = uri.AbsolutePath;
modified = false;
return uri;
}
builder.Append (scheme);
// note: mailto and news use ':', not "://", as their delimiter
- builder.Append (Uri.GetSchemeDelimiter (scheme));
+ if (UriParser.IsKnownScheme(scheme)) {
+ builder.Append (Uri.GetSchemeDelimiter (scheme));
+ }
+ else {
+ builder.Append (host.Length > 0 ? Uri.SchemeDelimiter : ":");
+ }
if (username != String.Empty) {
builder.Append (username);
if (path != String.Empty &&
builder [builder.Length - 1] != '/' &&
- path.Length > 0 && path [0] != '/')
+ path.Length > 0 && path [0] != '/' &&
+ host.Length > 0)
builder.Append ('/');
builder.Append (path);
builder.Append (query);