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

Commit 981f21e

Browse files
committed
#275 Reduce UriHelper to static methods.
1 parent 5bce140 commit 981f21e

File tree

2 files changed

+29
-90
lines changed

2 files changed

+29
-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: 26 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,107 +8,46 @@ 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+
public static string Encode(PathString pathBase,
8314
PathString path = new PathString(),
8415
QueryString query = new QueryString(),
8516
FragmentString fragment = new FragmentString())
8617
{
87-
return new UriHelper()
88-
{
89-
PathBase = pathBase,
90-
Path = path,
91-
Query = query,
92-
Fragment = fragment
93-
}.GetPartialUri();
18+
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
19+
return combinePath + query + fragment;
9420
}
9521

96-
public static string Create(string scheme,
22+
public static string Encode(string scheme,
9723
HostString host,
9824
PathString pathBase = new PathString(),
9925
PathString path = new PathString(),
10026
QueryString query = new QueryString(),
10127
FragmentString fragment = new FragmentString())
10228
{
103-
return new UriHelper()
29+
string combinePath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
30+
return scheme + "://" + host + combinePath + query + fragment;
31+
}
32+
33+
public static string Encode(Uri uri)
34+
{
35+
if (uri.IsAbsoluteUri)
36+
{
37+
return Encode(
38+
scheme: uri.Scheme,
39+
host: HostString.FromUriComponent(uri),
40+
pathBase: PathString.FromUriComponent(uri),
41+
query: QueryString.FromUriComponent(uri),
42+
fragment: FragmentString.FromUriComponent(uri));
43+
}
44+
else
10445
{
105-
Scheme = scheme,
106-
Host = host,
107-
PathBase = pathBase,
108-
Path = path,
109-
Query = query,
110-
Fragment = fragment
111-
}.GetFullUri();
46+
return Encode(
47+
pathBase: PathString.FromUriComponent(uri),
48+
query: QueryString.FromUriComponent(uri),
49+
fragment: FragmentString.FromUriComponent(uri));
50+
}
11251
}
11352
}
11453
}

0 commit comments

Comments
 (0)