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

Commit 568d0d9

Browse files
committed
#275 Reduce UriHelper to static methods.
1 parent 8703e2d commit 568d0d9

File tree

2 files changed

+51
-90
lines changed

2 files changed

+51
-90
lines changed

src/Microsoft.AspNet.Http.Extensions/ResponseHeaders.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,20 @@ public DateTimeOffset? LastModified
126126
}
127127
}
128128

129-
public UriHelper Location
129+
public Uri Location
130130
{
131131
get
132132
{
133133
Uri uri;
134134
if (Uri.TryCreate(Headers[HeaderNames.Location], UriKind.RelativeOrAbsolute, out uri))
135135
{
136-
return new UriHelper(uri);
136+
return uri;
137137
}
138138
return null;
139139
}
140140
set
141141
{
142-
Headers.Set(HeaderNames.Location, value);
142+
Headers.Set(HeaderNames.Location, value == null ? null : UriHelper.Encode(value));
143143
}
144144
}
145145

src/Microsoft.AspNet.Http.Extensions/UriHelper.cs

Lines changed: 48 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,107 +8,68 @@ namespace Microsoft.AspNet.Http.Extensions
88
/// <summary>
99
/// A helper class for constructing encoded Uris for use in headers and other Uris.
1010
/// </summary>
11-
public class UriHelper
11+
public static class UriHelper
1212
{
13-
public UriHelper()
14-
{
15-
}
16-
17-
public UriHelper(HttpRequest request)
18-
{
19-
Scheme = request.Scheme;
20-
Host = request.Host;
21-
PathBase = request.PathBase;
22-
Path = request.Path;
23-
Query = request.QueryString;
24-
// Fragment is not a valid request field.
25-
}
26-
27-
public UriHelper(Uri uri)
28-
{
29-
Scheme = uri.Scheme;
30-
Host = HostString.FromUriComponent(uri);
31-
// Assume nothing is being put in PathBase
32-
Path = PathString.FromUriComponent(uri);
33-
Query = QueryString.FromUriComponent(uri);
34-
Fragment = FragmentString.FromUriComponent(uri);
35-
}
36-
37-
public string Scheme { get; set; }
38-
39-
public HostString Host { get; set; }
40-
41-
public PathString PathBase { get; set; }
42-
43-
public PathString Path { get; set; }
44-
45-
public QueryString Query { get; set; }
46-
47-
public FragmentString Fragment { get; set; }
48-
49-
public bool IsFullUri
50-
{
51-
get { return !string.IsNullOrEmpty(Scheme) && Host.HasValue; }
52-
}
53-
54-
// Always returns at least '/'
55-
public string GetPartialUri()
56-
{
57-
string path = (PathBase.HasValue || Path.HasValue) ? (PathBase + Path).ToString() : "/";
58-
return path + Query + Fragment;
59-
}
60-
61-
// Always returns at least 'scheme://host/'
62-
public string GetFullUri()
63-
{
64-
if (string.IsNullOrEmpty(Scheme))
65-
{
66-
throw new InvalidOperationException("Missing Scheme");
67-
}
68-
if (!Host.HasValue)
69-
{
70-
throw new InvalidOperationException("Missing Host");
71-
}
72-
73-
string path = (PathBase.HasValue || Path.HasValue) ? (PathBase + Path).ToString() : "/";
74-
return Scheme + "://" + Host + path + Query + Fragment;
75-
}
76-
77-
public override string ToString()
78-
{
79-
return IsFullUri ? GetFullUri() : GetPartialUri();
80-
}
81-
82-
public static string Create(PathString pathBase,
13+
/// <summary>
14+
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
15+
/// </summary>
16+
/// <param name="pathBase"></param>
17+
/// <param name="path"></param>
18+
/// <param name="query"></param>
19+
/// <param name="fragment"></param>
20+
/// <returns></returns>
21+
public static string Encode(PathString pathBase = new PathString(),
8322
PathString path = new PathString(),
8423
QueryString query = new QueryString(),
8524
FragmentString fragment = new FragmentString())
8625
{
87-
return new UriHelper()
88-
{
89-
PathBase = pathBase,
90-
Path = path,
91-
Query = query,
92-
Fragment = fragment
93-
}.GetPartialUri();
26+
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
27+
return combinePath + query + fragment;
9428
}
9529

96-
public static string Create(string scheme,
30+
/// <summary>
31+
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
32+
/// Note that unicode in the HostString will be encoded as punycode.
33+
/// </summary>
34+
/// <param name="scheme"></param>
35+
/// <param name="host"></param>
36+
/// <param name="pathBase"></param>
37+
/// <param name="path"></param>
38+
/// <param name="query"></param>
39+
/// <param name="fragment"></param>
40+
/// <returns></returns>
41+
public static string Encode(string scheme,
9742
HostString host,
9843
PathString pathBase = new PathString(),
9944
PathString path = new PathString(),
10045
QueryString query = new QueryString(),
10146
FragmentString fragment = new FragmentString())
10247
{
103-
return new UriHelper()
48+
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
49+
return scheme + "://" + host + combinePath + query + fragment;
50+
}
51+
52+
/// <summary>
53+
/// Generates a string from the given absolute or relative Uri that is appropriately encoded for use in
54+
/// HTTP headers. Note that a unicode host name will be encoded as punycode.
55+
/// </summary>
56+
/// <param name="uri"></param>
57+
/// <returns></returns>
58+
public static string Encode(Uri uri)
59+
{
60+
if (uri.IsAbsoluteUri)
61+
{
62+
return Encode(
63+
scheme: uri.Scheme,
64+
host: HostString.FromUriComponent(uri),
65+
pathBase: PathString.FromUriComponent(uri),
66+
query: QueryString.FromUriComponent(uri),
67+
fragment: FragmentString.FromUriComponent(uri));
68+
}
69+
else
10470
{
105-
Scheme = scheme,
106-
Host = host,
107-
PathBase = pathBase,
108-
Path = path,
109-
Query = query,
110-
Fragment = fragment
111-
}.GetFullUri();
71+
return uri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
72+
}
11273
}
11374
}
11475
}

0 commit comments

Comments
 (0)