This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/Microsoft.AspNetCore.Http.Abstractions/Internal/PathStringHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Http.Internal | ||
{ | ||
internal class PathStringHelper | ||
{ | ||
private static bool[] ValidPathChars = { | ||
false, false, false, false, false, false, false, false, // 0x00 - 0x07 | ||
false, false, false, false, false, false, false, false, // 0x08 - 0x0F | ||
false, false, false, false, false, false, false, false, // 0x10 - 0x17 | ||
false, false, false, false, false, false, false, false, // 0x18 - 0x1F | ||
false, true, false, false, true, false, true, true, // 0x20 - 0x27 | ||
true, true, true, true, true, true, true, true, // 0x28 - 0x2F | ||
true, true, true, true, true, true, true, true, // 0x30 - 0x37 | ||
true, true, true, true, false, true, false, false, // 0x38 - 0x3F | ||
true, true, true, true, true, true, true, true, // 0x40 - 0x47 | ||
true, true, true, true, true, true, true, true, // 0x48 - 0x4F | ||
true, true, true, true, true, true, true, true, // 0x50 - 0x57 | ||
true, true, true, false, false, false, false, true, // 0x58 - 0x5F | ||
false, true, true, true, true, true, true, true, // 0x60 - 0x67 | ||
true, true, true, true, true, true, true, true, // 0x68 - 0x6F | ||
true, true, true, true, true, true, true, true, // 0x70 - 0x77 | ||
true, true, true, false, false, false, true, false, // 0x78 - 0x7F | ||
}; | ||
|
||
public static bool IsValidPathChar(char c) | ||
{ | ||
return c < ValidPathChars.Length && ValidPathChars[c]; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,9 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Text.Encodings.Web; | ||
using System.Text; | ||
using Microsoft.AspNetCore.Http.Abstractions; | ||
using Microsoft.AspNetCore.Http.Internal; | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
|
@@ -66,25 +67,83 @@ public override string ToString() | |
/// <returns>The escaped path value</returns> | ||
public string ToUriComponent() | ||
{ | ||
// TODO: Measure the cost of this escaping and consider optimizing. | ||
if (!HasValue) | ||
{ | ||
return string.Empty; | ||
} | ||
var values = _value.Split(splitChar); | ||
var changed = false; | ||
for (var i = 0; i < values.Length; i++) | ||
{ | ||
var value = values[i]; | ||
values[i] = UrlEncoder.Default.Encode(value); | ||
|
||
if (!changed && value != values[i]) | ||
StringBuilder buffer = null; | ||
|
||
var start = 0; | ||
var count = 0; | ||
var requiresEscaping = false; | ||
for (int i = 0; i < _value.Length; ++i) | ||
{ | ||
if (PathStringHelper.IsValidPathChar(_value[i])) | ||
{ | ||
changed = true; | ||
if (requiresEscaping) | ||
{ | ||
// the current segment requires escape | ||
if (buffer == null) | ||
{ | ||
buffer = new StringBuilder(_value.Length * 3); | ||
} | ||
|
||
buffer.Append(Uri.EscapeDataString(_value.Substring(start, count))); | ||
|
||
requiresEscaping = false; | ||
start = i; | ||
count = 0; | ||
} | ||
|
||
count++; | ||
} | ||
else | ||
{ | ||
if (!requiresEscaping) | ||
{ | ||
// the current segument doesn't require escape | ||
if (buffer == null) | ||
{ | ||
buffer = new StringBuilder(_value.Length * 3); | ||
} | ||
|
||
buffer.Append(_value, start, count); | ||
|
||
requiresEscaping = true; | ||
start = i; | ||
count = 0; | ||
} | ||
|
||
count++; | ||
} | ||
} | ||
|
||
if (count == _value.Length && !requiresEscaping) | ||
{ | ||
return _value; | ||
} | ||
else | ||
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. else if count > 0? 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. Actually doesn't matter, if
|
||
{ | ||
if (count > 0) | ||
{ | ||
if (buffer == null) | ||
{ | ||
buffer = new StringBuilder(_value.Length * 3); | ||
} | ||
|
||
if (requiresEscaping) | ||
{ | ||
buffer.Append(Uri.EscapeDataString(_value.Substring(start, count))); | ||
} | ||
else | ||
{ | ||
buffer.Append(_value, start, count); | ||
} | ||
} | ||
|
||
return changed ? string.Join("/", values) : _value; | ||
return buffer.ToString(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -335,7 +394,7 @@ public static implicit operator PathString(string s) | |
/// Implicitly calls ToString(). | ||
/// </summary> | ||
/// <param name="path"></param> | ||
public static implicit operator string (PathString path) | ||
public static implicit operator string(PathString path) | ||
{ | ||
return path.ToString(); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Who usually calls this method and how expensive is this change? Looks like we never measured it..