@@ -8,107 +8,68 @@ namespace Microsoft.AspNet.Http.Extensions
8
8
/// <summary>
9
9
/// A helper class for constructing encoded Uris for use in headers and other Uris.
10
10
/// </summary>
11
- public class UriHelper
11
+ public static class UriHelper
12
12
{
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 ( ) ,
83
22
PathString path = new PathString ( ) ,
84
23
QueryString query = new QueryString ( ) ,
85
24
FragmentString fragment = new FragmentString ( ) )
86
25
{
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 ;
94
28
}
95
29
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 ,
97
42
HostString host ,
98
43
PathString pathBase = new PathString ( ) ,
99
44
PathString path = new PathString ( ) ,
100
45
QueryString query = new QueryString ( ) ,
101
46
FragmentString fragment = new FragmentString ( ) )
102
47
{
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
104
70
{
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
+ }
112
73
}
113
74
}
114
75
}
0 commit comments