/// <summary> /// Called when the <see cref="Uri"/> needs to be parsed. /// </summary> protected virtual void OnParseUri() { RequiresParsePath = true; _directPath = null; if (_uri == null) { _port = UnspecifiedPort; _scheme = Http; _host = string.Empty; _pathPrefix = string.Empty; _server = string.Empty; _fragment = string.Empty; PathInternal = string.Empty; _queryArgs = null; } else { _scheme = _uri.Scheme; _port = _uri.Port; _host = _uri.Host; var path = Uri.UnescapeDataString(_uri.AbsolutePath); if (path.StartsWith(PathSegmentPrefix)) { path = path.Substring(PathSegmentPrefix.Length); } var endServer = path.IndexOf(PathSegmentPrefix[0]); if (endServer < 0) { _server = path; PathInternal = string.Empty; } else { _server = path.Substring(0, endServer); PathInternal = path.Substring(endServer + 1); } _fragment = _uri.Fragment; if (_fragment.StartsWith(FragmentPrefix)) { _fragment = _fragment.Substring(FragmentPrefix.Length); } if (PathInternal.StartsWith(PathSegmentPrefix)) { PathInternal = PathInternal.Substring(PathSegmentPrefix.Length); } _queryArgs = new QueryArgsDictionary(this, _uri.Query); } }
/// <summary> /// Called when the <see cref="Uri"/> needs to be rebuilt. /// </summary> protected virtual void OnBuildUri() { // http var uri = new StringBuilder(string.IsNullOrEmpty(_scheme) ? Http : _scheme); // http: uri.Append(SchemeSuffix); // http:// uri.Append(PathSegmentPrefix); uri.Append(PathSegmentPrefix); // http://host uri.Append(string.IsNullOrEmpty(_host) ? LocalHost : _host); // http://<host><:port> if (_port != UnspecifiedPort) { uri.Append(PortPrefix); uri.Append(_port.ToString()); } // http://host<:port>/serverPrefix if (!string.IsNullOrEmpty(_pathPrefix)) { if (!_pathPrefix.StartsWith(PathSegmentPrefix)) { uri.Append(PathSegmentPrefix); } uri.Append(_pathPrefix); } // http://host<:port>/serverPrefix/server if (!string.IsNullOrEmpty(_server)) { if (!_server.StartsWith(PathSegmentPrefix)) { uri.Append(PathSegmentPrefix); } uri.Append(_server); } // http://<host><:port>/<path> CheckRebuildPath(); if (!string.IsNullOrEmpty(PathInternal)) { if (!PathInternal.StartsWith(PathSegmentPrefix)) { uri.Append(PathSegmentPrefix); } uri.Append(PathInternal); } // http://<host><:port>/<path><?query> var query = Query; if (!string.IsNullOrEmpty(query)) { uri.Append(QueryPrefix); uri.Append(query); } if (!string.IsNullOrEmpty(_fragment)) { if (!_fragment.StartsWith(FragmentPrefix)) { uri.Append(FragmentPrefix); } uri.Append(_fragment); } _uri = new Uri(uri.ToString()); }