#573 Rename UriHelper.Encode

This commit is contained in:
Chris R 2016-06-03 13:11:40 -07:00
parent 8b3c308c22
commit 3fc1fef2be
4 changed files with 48 additions and 45 deletions

View File

@ -1,30 +1,21 @@
using System; using System;
using System.Diagnostics; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives; using Microsoft.AspNetCore.Http.Extensions;
namespace SampleApp namespace SampleApp
{ {
public class Program public class Program
{ {
public void Main(string[] args) public static void Main(string[] args)
{ {
for (int i = 0; i < 10; i++) var query = new QueryBuilder()
{ {
Stopwatch timer = new Stopwatch(); { "hello", "world" }
timer.Start(); }.ToQueryString();
string myString;
string[] myArray; var uri = UriHelper.BuildAbsolute("http", new HostString("contoso.com"), query: query);
StringValues myValues;
for (int j = 0; j < 100000000; j++) Console.WriteLine(uri);
{
myString = new string('a', 40);
myArray = new[] { myString };
// myValues = new StringValues(myString);
myValues = new StringValues(myArray);
}
timer.Stop();
Console.WriteLine(timer.Elapsed + ", " + Environment.WorkingSet);
}
} }
} }
} }

View File

@ -1,12 +1,24 @@
{ {
"version": "1.0.0-*", "version": "1.0.0-*",
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Http": "1.0.0-*" "Microsoft.AspNetCore.Http": "1.0.0-*",
}, "Microsoft.AspNetCore.Http.Extensions": "1.0.0-*"
"commands": {
"SampleApp": "SampleApp"
}, },
"frameworks": { "frameworks": {
"net451": {} "net451": { },
"netcoreapp1.0": {
"imports": [
"dnxcore50"
],
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0-*",
"type": "platform"
}
}
}
},
"buildOptions": {
"emitEntryPoint": true
} }
} }

View File

@ -16,12 +16,12 @@ namespace Microsoft.AspNetCore.Http.Extensions
/// <summary> /// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers. /// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// </summary> /// </summary>
/// <param name="pathBase"></param> /// <param name="pathBase">The first portion of the request path associated with application root.</param>
/// <param name="path"></param> /// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query"></param> /// <param name="query">The query, if any.</param>
/// <param name="fragment"></param> /// <param name="fragment">The fragment, if any.</param>
/// <returns></returns> /// <returns></returns>
public static string Encode( public static string BuildRelative(
PathString pathBase = new PathString(), PathString pathBase = new PathString(),
PathString path = new PathString(), PathString path = new PathString(),
QueryString query = new QueryString(), QueryString query = new QueryString(),
@ -35,14 +35,14 @@ namespace Microsoft.AspNetCore.Http.Extensions
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers. /// 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. /// Note that unicode in the HostString will be encoded as punycode.
/// </summary> /// </summary>
/// <param name="scheme"></param> /// <param name="scheme">http, https, etc.</param>
/// <param name="host"></param> /// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
/// <param name="pathBase"></param> /// <param name="pathBase">The first portion of the request path associated with application root.</param>
/// <param name="path"></param> /// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query"></param> /// <param name="query">The query, if any.</param>
/// <param name="fragment"></param> /// <param name="fragment">The fragment, if any.</param>
/// <returns></returns> /// <returns></returns>
public static string Encode( public static string BuildAbsolute(
string scheme, string scheme,
HostString host, HostString host,
PathString pathBase = new PathString(), PathString pathBase = new PathString(),
@ -74,13 +74,13 @@ namespace Microsoft.AspNetCore.Http.Extensions
/// Generates a string from the given absolute or relative Uri that is appropriately encoded for use in /// 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. /// HTTP headers. Note that a unicode host name will be encoded as punycode.
/// </summary> /// </summary>
/// <param name="uri"></param> /// <param name="uri">The Uri to encode.</param>
/// <returns></returns> /// <returns></returns>
public static string Encode(Uri uri) public static string Encode(Uri uri)
{ {
if (uri.IsAbsoluteUri) if (uri.IsAbsoluteUri)
{ {
return Encode( return BuildAbsolute(
scheme: uri.Scheme, scheme: uri.Scheme,
host: HostString.FromUriComponent(uri), host: HostString.FromUriComponent(uri),
pathBase: PathString.FromUriComponent(uri), pathBase: PathString.FromUriComponent(uri),
@ -97,18 +97,18 @@ namespace Microsoft.AspNetCore.Http.Extensions
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers /// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers
/// and other HTTP operations. /// and other HTTP operations.
/// </summary> /// </summary>
/// <param name="request"></param> /// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns> /// <returns></returns>
public static string GetEncodedUrl(this HttpRequest request) public static string GetEncodedUrl(this HttpRequest request)
{ {
return Encode(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString); return BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
} }
/// <summary> /// <summary>
/// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString) /// Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString)
/// suitable only for display. This format should not be used in HTTP headers or other HTTP operations. /// suitable only for display. This format should not be used in HTTP headers or other HTTP operations.
/// </summary> /// </summary>
/// <param name="request"></param> /// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns> /// <returns></returns>
public static string GetDisplayUrl(this HttpRequest request) public static string GetDisplayUrl(this HttpRequest request)
{ {

View File

@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
[Fact] [Fact]
public void EncodeEmptyPartialUrl() public void EncodeEmptyPartialUrl()
{ {
var result = UriHelper.Encode(); var result = UriHelper.BuildRelative();
Assert.Equal("/", result); Assert.Equal("/", result);
} }
@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
[Fact] [Fact]
public void EncodePartialUrl() public void EncodePartialUrl()
{ {
var result = UriHelper.Encode(new PathString("/un?escaped/base"), new PathString("/un?escaped"), var result = UriHelper.BuildRelative(new PathString("/un?escaped/base"), new PathString("/un?escaped"),
new QueryString("?name=val%23ue"), new FragmentString("#my%20value")); new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));
Assert.Equal("/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result); Assert.Equal("/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);
@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
[Fact] [Fact]
public void EncodeEmptyFullUrl() public void EncodeEmptyFullUrl()
{ {
var result = UriHelper.Encode("http", new HostString(string.Empty)); var result = UriHelper.BuildAbsolute("http", new HostString(string.Empty));
Assert.Equal("http:///", result); Assert.Equal("http:///", result);
} }
@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Http.Extensions
[Fact] [Fact]
public void EncodeFullUrl() public void EncodeFullUrl()
{ {
var result = UriHelper.Encode("http", new HostString("my.HoΨst:80"), new PathString("/un?escaped/base"), new PathString("/un?escaped"), var result = UriHelper.BuildAbsolute("http", new HostString("my.HoΨst:80"), new PathString("/un?escaped/base"), new PathString("/un?escaped"),
new QueryString("?name=val%23ue"), new FragmentString("#my%20value")); new QueryString("?name=val%23ue"), new FragmentString("#my%20value"));
Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result); Assert.Equal("http://my.xn--host-cpd:80/un%3Fescaped/base/un%3Fescaped?name=val%23ue#my%20value", result);