-
Notifications
You must be signed in to change notification settings - Fork 191
Rename UriHelper.Encode #648
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,21 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Primitives; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Extensions; | ||
|
||
namespace SampleApp | ||
{ | ||
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(); | ||
timer.Start(); | ||
string myString; | ||
string[] myArray; | ||
StringValues myValues; | ||
for (int j = 0; j < 100000000; j++) | ||
{ | ||
myString = new string('a', 40); | ||
myArray = new[] { myString }; | ||
// myValues = new StringValues(myString); | ||
myValues = new StringValues(myArray); | ||
} | ||
timer.Stop(); | ||
Console.WriteLine(timer.Elapsed + ", " + Environment.WorkingSet); | ||
} | ||
{ "hello", "world" } | ||
}.ToQueryString(); | ||
|
||
var uri = UriHelper.BuildAbsolute("http", new HostString("contoso.com"), query: query); | ||
|
||
Console.WriteLine(uri); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
"dependencies": { | ||
"Microsoft.AspNetCore.Http": "1.0.0-*" | ||
}, | ||
"commands": { | ||
"SampleApp": "SampleApp" | ||
"Microsoft.AspNetCore.Http": "1.0.0-*", | ||
"Microsoft.AspNetCore.Http.Extensions": "1.0.0-*" | ||
}, | ||
"frameworks": { | ||
"net451": {} | ||
"net451": { }, | ||
"netcoreapp1.0": { | ||
"imports": [ | ||
"dnxcore50" | ||
], | ||
"dependencies": { | ||
"Microsoft.NETCore.App": { | ||
"version": "1.0.0-*", | ||
"type": "platform" | ||
} | ||
} | ||
} | ||
}, | ||
"buildOptions": { | ||
"emitEntryPoint": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,12 @@ public static class UriHelper | |
/// <summary> | ||
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers. | ||
/// </summary> | ||
/// <param name="pathBase"></param> | ||
/// <param name="path"></param> | ||
/// <param name="query"></param> | ||
/// <param name="fragment"></param> | ||
/// <param name="pathBase">The first portion of the request path associated with application root.</param> | ||
/// <param name="path">The portion of the request path that identifies the requested resource.</param> | ||
/// <param name="query">The query, if any.</param> | ||
/// <param name="fragment">The fragment, if any.</param> | ||
/// <returns></returns> | ||
public static string Encode( | ||
public static string BuildRelative( | ||
PathString pathBase = new PathString(), | ||
PathString path = new PathString(), | ||
QueryString query = new QueryString(), | ||
|
@@ -35,14 +35,14 @@ public static string Encode( | |
/// 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. | ||
/// </summary> | ||
/// <param name="scheme"></param> | ||
/// <param name="host"></param> | ||
/// <param name="pathBase"></param> | ||
/// <param name="path"></param> | ||
/// <param name="query"></param> | ||
/// <param name="fragment"></param> | ||
/// <param name="scheme">http, https, etc.</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">The first portion of the request path associated with application root.</param> | ||
/// <param name="path">The portion of the request path that identifies the requested resource.</param> | ||
/// <param name="query">The query, if any.</param> | ||
/// <param name="fragment">The fragment, if any.</param> | ||
/// <returns></returns> | ||
public static string Encode( | ||
public static string BuildAbsolute( | ||
string scheme, | ||
HostString host, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might make sense to add an overload that takes a string variation of this. Almost every call into this ends up doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Insufficient data. I see 5 references and the ones that call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🆗, less concerned about this guy. Can always add the additional overloads later if it turns out to be a burden on users. |
||
PathString pathBase = new PathString(), | ||
|
@@ -74,13 +74,13 @@ public static string Encode( | |
/// 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. | ||
/// </summary> | ||
/// <param name="uri"></param> | ||
/// <param name="uri">The Uri to encode.</param> | ||
/// <returns></returns> | ||
public static string Encode(Uri uri) | ||
{ | ||
if (uri.IsAbsoluteUri) | ||
{ | ||
return Encode( | ||
return BuildAbsolute( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious: Why is there not a corresponding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. System.Uri has minimal support for relative uris. It won't parse it into path?query#fragment for you. The most it will do is some basic escaping. |
||
scheme: uri.Scheme, | ||
host: HostString.FromUriComponent(uri), | ||
pathBase: PathString.FromUriComponent(uri), | ||
|
@@ -97,18 +97,18 @@ public static string Encode(Uri uri) | |
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers | ||
/// and other HTTP operations. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="request">The request to assemble the uri pieces from.</param> | ||
/// <returns></returns> | ||
public static string GetEncodedUrl(this HttpRequest request) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super odd to have an extension method in this class. Also, any reason the above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encode could be an extension, but I think you'd loose context of what you were encoding it for. It's only used here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I follow. Wouldn't it just be: Headers.Set(HeaderNames.Location, value == null ? null : value.GetEncodedUrl()); Value's a Uri which is the contextual part. It's odd having this method and the below extension method. They both should be consistent whatever is decided. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encoded in what way and for what purpose? This method does a particular type of encoding for use in Http headers that is not applicable elsewhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets talk in person. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HttpEncode? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Talked offline, this is 🆗 |
||
{ | ||
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> | ||
/// 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. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="request">The request to assemble the uri pieces from.</param> | ||
/// <returns></returns> | ||
public static string GetDisplayUrl(this HttpRequest request) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sample app is bogus....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it's convenient for debugging things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So are unit tests