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

Commit 3fc1fef

Browse files
committed
#573 Rename UriHelper.Encode
1 parent 8b3c308 commit 3fc1fef

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

samples/SampleApp/Program.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
11
using System;
2-
using System.Diagnostics;
3-
using Microsoft.Extensions.Primitives;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Http.Extensions;
44

55
namespace SampleApp
66
{
77
public class Program
88
{
9-
public void Main(string[] args)
9+
public static void Main(string[] args)
1010
{
11-
for (int i = 0; i < 10; i++)
11+
var query = new QueryBuilder()
1212
{
13-
Stopwatch timer = new Stopwatch();
14-
timer.Start();
15-
string myString;
16-
string[] myArray;
17-
StringValues myValues;
18-
for (int j = 0; j < 100000000; j++)
19-
{
20-
myString = new string('a', 40);
21-
myArray = new[] { myString };
22-
// myValues = new StringValues(myString);
23-
myValues = new StringValues(myArray);
24-
}
25-
timer.Stop();
26-
Console.WriteLine(timer.Elapsed + ", " + Environment.WorkingSet);
27-
}
13+
{ "hello", "world" }
14+
}.ToQueryString();
15+
16+
var uri = UriHelper.BuildAbsolute("http", new HostString("contoso.com"), query: query);
17+
18+
Console.WriteLine(uri);
2819
}
2920
}
3021
}

samples/SampleApp/project.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
{
22
"version": "1.0.0-*",
33
"dependencies": {
4-
"Microsoft.AspNetCore.Http": "1.0.0-*"
5-
},
6-
"commands": {
7-
"SampleApp": "SampleApp"
4+
"Microsoft.AspNetCore.Http": "1.0.0-*",
5+
"Microsoft.AspNetCore.Http.Extensions": "1.0.0-*"
86
},
97
"frameworks": {
10-
"net451": {}
8+
"net451": { },
9+
"netcoreapp1.0": {
10+
"imports": [
11+
"dnxcore50"
12+
],
13+
"dependencies": {
14+
"Microsoft.NETCore.App": {
15+
"version": "1.0.0-*",
16+
"type": "platform"
17+
}
18+
}
19+
}
20+
},
21+
"buildOptions": {
22+
"emitEntryPoint": true
1123
}
1224
}

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public static class UriHelper
1616
/// <summary>
1717
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
1818
/// </summary>
19-
/// <param name="pathBase"></param>
20-
/// <param name="path"></param>
21-
/// <param name="query"></param>
22-
/// <param name="fragment"></param>
19+
/// <param name="pathBase">The first portion of the request path associated with application root.</param>
20+
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
21+
/// <param name="query">The query, if any.</param>
22+
/// <param name="fragment">The fragment, if any.</param>
2323
/// <returns></returns>
24-
public static string Encode(
24+
public static string BuildRelative(
2525
PathString pathBase = new PathString(),
2626
PathString path = new PathString(),
2727
QueryString query = new QueryString(),
@@ -35,14 +35,14 @@ public static string Encode(
3535
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
3636
/// Note that unicode in the HostString will be encoded as punycode.
3737
/// </summary>
38-
/// <param name="scheme"></param>
39-
/// <param name="host"></param>
40-
/// <param name="pathBase"></param>
41-
/// <param name="path"></param>
42-
/// <param name="query"></param>
43-
/// <param name="fragment"></param>
38+
/// <param name="scheme">http, https, etc.</param>
39+
/// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
40+
/// <param name="pathBase">The first portion of the request path associated with application root.</param>
41+
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
42+
/// <param name="query">The query, if any.</param>
43+
/// <param name="fragment">The fragment, if any.</param>
4444
/// <returns></returns>
45-
public static string Encode(
45+
public static string BuildAbsolute(
4646
string scheme,
4747
HostString host,
4848
PathString pathBase = new PathString(),
@@ -74,13 +74,13 @@ public static string Encode(
7474
/// Generates a string from the given absolute or relative Uri that is appropriately encoded for use in
7575
/// HTTP headers. Note that a unicode host name will be encoded as punycode.
7676
/// </summary>
77-
/// <param name="uri"></param>
77+
/// <param name="uri">The Uri to encode.</param>
7878
/// <returns></returns>
7979
public static string Encode(Uri uri)
8080
{
8181
if (uri.IsAbsoluteUri)
8282
{
83-
return Encode(
83+
return BuildAbsolute(
8484
scheme: uri.Scheme,
8585
host: HostString.FromUriComponent(uri),
8686
pathBase: PathString.FromUriComponent(uri),
@@ -97,18 +97,18 @@ public static string Encode(Uri uri)
9797
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers
9898
/// and other HTTP operations.
9999
/// </summary>
100-
/// <param name="request"></param>
100+
/// <param name="request">The request to assemble the uri pieces from.</param>
101101
/// <returns></returns>
102102
public static string GetEncodedUrl(this HttpRequest request)
103103
{
104-
return Encode(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
104+
return BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
105105
}
106106

107107
/// <summary>
108108
/// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString)
109109
/// suitable only for display. This format should not be used in HTTP headers or other HTTP operations.
110110
/// </summary>
111-
/// <param name="request"></param>
111+
/// <param name="request">The request to assemble the uri pieces from.</param>
112112
/// <returns></returns>
113113
public static string GetDisplayUrl(this HttpRequest request)
114114
{

test/Microsoft.AspNetCore.Http.Extensions.Tests/UriHelperTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public class UriHelperTests
1010
[Fact]
1111
public void EncodeEmptyPartialUrl()
1212
{
13-
var result = UriHelper.Encode();
13+
var result = UriHelper.BuildRelative();
1414

1515
Assert.Equal("/", result);
1616
}
1717

1818
[Fact]
1919
public void EncodePartialUrl()
2020
{
21-
var result = UriHelper.Encode(new PathString("/un?escaped/base"), new PathString("/un?escaped"),
21+
var result = UriHelper.BuildRelative(new PathString("/un?escaped/base"), new PathString("/un?escaped"),
2222
new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));
2323

2424
Assert.Equal("/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);
@@ -27,15 +27,15 @@ public void EncodePartialUrl()
2727
[Fact]
2828
public void EncodeEmptyFullUrl()
2929
{
30-
var result = UriHelper.Encode("http", new HostString(string.Empty));
30+
var result = UriHelper.BuildAbsolute("http", new HostString(string.Empty));
3131

3232
Assert.Equal("http:///", result);
3333
}
3434

3535
[Fact]
3636
public void EncodeFullUrl()
3737
{
38-
var result = UriHelper.Encode("http", new HostString("my.HoΨst:80"), new PathString("/un?escaped/base"), new PathString("/un?escaped"),
38+
var result = UriHelper.BuildAbsolute("http", new HostString("my.HoΨst:80"), new PathString("/un?escaped/base"), new PathString("/un?escaped"),
3939
new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));
4040

4141
Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);

0 commit comments

Comments
 (0)