Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Reduce UriHelper to static methods. #300

Merged
merged 1 commit into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Microsoft.AspNet.Http.Extensions/ResponseHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,20 @@ public DateTimeOffset? LastModified
}
}

public UriHelper Location
public Uri Location
{
get
{
Uri uri;
if (Uri.TryCreate(Headers[HeaderNames.Location], UriKind.RelativeOrAbsolute, out uri))
{
return new UriHelper(uri);
return uri;
}
return null;
}
set
{
Headers.Set(HeaderNames.Location, value);
Headers.Set(HeaderNames.Location, value == null ? null : UriHelper.Encode(value));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using null coalescing (?? operator)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That only works for the reverse case value == null ? DoSomethingElse() : null; We only want to call Encode if it is not null.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right.

}
}

Expand Down
135 changes: 48 additions & 87 deletions src/Microsoft.AspNet.Http.Extensions/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,107 +8,68 @@ namespace Microsoft.AspNet.Http.Extensions
/// <summary>
/// A helper class for constructing encoded Uris for use in headers and other Uris.
/// </summary>
public class UriHelper
public static class UriHelper
{
public UriHelper()
{
}

public UriHelper(HttpRequest request)
{
Scheme = request.Scheme;
Host = request.Host;
PathBase = request.PathBase;
Path = request.Path;
Query = request.QueryString;
// Fragment is not a valid request field.
}

public UriHelper(Uri uri)
{
Scheme = uri.Scheme;
Host = HostString.FromUriComponent(uri);
// Assume nothing is being put in PathBase
Path = PathString.FromUriComponent(uri);
Query = QueryString.FromUriComponent(uri);
Fragment = FragmentString.FromUriComponent(uri);
}

public string Scheme { get; set; }

public HostString Host { get; set; }

public PathString PathBase { get; set; }

public PathString Path { get; set; }

public QueryString Query { get; set; }

public FragmentString Fragment { get; set; }

public bool IsFullUri
{
get { return !string.IsNullOrEmpty(Scheme) && Host.HasValue; }
}

// Always returns at least '/'
public string GetPartialUri()
{
string path = (PathBase.HasValue || Path.HasValue) ? (PathBase + Path).ToString() : "/";
return path + Query + Fragment;
}

// Always returns at least 'scheme://host/'
public string GetFullUri()
{
if (string.IsNullOrEmpty(Scheme))
{
throw new InvalidOperationException("Missing Scheme");
}
if (!Host.HasValue)
{
throw new InvalidOperationException("Missing Host");
}

string path = (PathBase.HasValue || Path.HasValue) ? (PathBase + Path).ToString() : "/";
return Scheme + "://" + Host + path + Query + Fragment;
}

public override string ToString()
{
return IsFullUri ? GetFullUri() : GetPartialUri();
}

public static string Create(PathString pathBase,
/// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// </summary>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc nit: lowercase "uri" should probably be URI (or Uri)

/// <param name="pathBase"></param>
/// <param name="path"></param>
/// <param name="query"></param>
/// <param name="fragment"></param>
/// <returns></returns>
public static string Encode(PathString pathBase = new PathString(),
PathString path = new PathString(),
QueryString query = new QueryString(),
FragmentString fragment = new FragmentString())
{
return new UriHelper()
{
PathBase = pathBase,
Path = path,
Query = query,
Fragment = fragment
}.GetPartialUri();
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
return combinePath + query + fragment;
}

public static string Create(string scheme,
/// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// Note that unicode in the HostString will be encoded as punycode.
/// </summary>
/// <param name="scheme"></param>
/// <param name="host"></param>
/// <param name="pathBase"></param>
/// <param name="path"></param>
/// <param name="query"></param>
/// <param name="fragment"></param>
/// <returns></returns>
public static string Encode(string scheme,
HostString host,
PathString pathBase = new PathString(),
PathString path = new PathString(),
QueryString query = new QueryString(),
FragmentString fragment = new FragmentString())
{
return new UriHelper()
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
return scheme + "://" + host + combinePath + query + fragment;
}

/// <summary>
/// Generates a string from the given absolute or relative Uri that is appropriately encoded for use in
/// HTTP headers. Note that a unicode host name will be encoded as punycode.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static string Encode(Uri uri)
{
if (uri.IsAbsoluteUri)
{
return Encode(
scheme: uri.Scheme,
host: HostString.FromUriComponent(uri),
pathBase: PathString.FromUriComponent(uri),
query: QueryString.FromUriComponent(uri),
fragment: FragmentString.FromUriComponent(uri));
}
else
{
Scheme = scheme,
Host = host,
PathBase = pathBase,
Path = path,
Query = query,
Fragment = fragment
}.GetFullUri();
return uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
}
}
}
}