Skip to content

Commit eda62e7

Browse files
authored
Use Uri.SchemeDelimiter instead of hardcoded "://" for const of SchemeDelimiter in UriHelper (#28140)
* Use Uri.SchemeDelimiter instead of hardcoded "://" for const of SchemeDelimiter in UriHelper This PR is to update of use Uri.SchemeDelimiter instead of having hardcoded value of "://" for const value of SchemeDelimiter Summary of the changes (Less than 80 chars) - set value of SchemeDelimiter const to Uri.SchemeDelimiter Addresses #bugnumber (in this specific format)? N/A (just minor refactor over existing code in `src/Http/Http.Extensions/src/UriHelper.cs`) * update other classes to use Uri.SchemaDelimiter
1 parent 54e9c76 commit eda62e7

File tree

9 files changed

+19
-19
lines changed

9 files changed

+19
-19
lines changed

src/Http/Http.Extensions/src/UriHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static class UriHelper
1414
private const char ForwardSlash = '/';
1515
private const char Hash = '#';
1616
private const char QuestionMark = '?';
17-
private const string SchemeDelimiter = "://";
17+
private static readonly string SchemeDelimiter = Uri.SchemeDelimiter;
1818

1919
/// <summary>
2020
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.

src/Http/Http/src/BindingAddress.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public override string ToString()
4949
{
5050
if (IsUnixPipe)
5151
{
52-
return Scheme.ToLowerInvariant() + "://" + Host.ToLowerInvariant();
52+
return Scheme.ToLowerInvariant() + Uri.SchemeDelimiter + Host.ToLowerInvariant();
5353
}
5454
else
5555
{
56-
return Scheme.ToLowerInvariant() + "://" + Host.ToLowerInvariant() + ":" + Port.ToString(CultureInfo.InvariantCulture) + PathBase;
56+
return Scheme.ToLowerInvariant() + Uri.SchemeDelimiter + Host.ToLowerInvariant() + ":" + Port.ToString(CultureInfo.InvariantCulture) + PathBase;
5757
}
5858
}
5959

@@ -79,12 +79,12 @@ public static BindingAddress Parse(string address)
7979
{
8080
address = address ?? string.Empty;
8181

82-
int schemeDelimiterStart = address.IndexOf("://", StringComparison.Ordinal);
82+
int schemeDelimiterStart = address.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal);
8383
if (schemeDelimiterStart < 0)
8484
{
8585
throw new FormatException($"Invalid url: '{address}'");
8686
}
87-
int schemeDelimiterEnd = schemeDelimiterStart + "://".Length;
87+
int schemeDelimiterEnd = schemeDelimiterStart + Uri.SchemeDelimiter.Length;
8888

8989
var isUnixPipe = address.IndexOf(UnixPipeHostPrefix, schemeDelimiterEnd, StringComparison.Ordinal) == schemeDelimiterEnd;
9090

src/Middleware/Rewrite/src/RedirectRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public virtual void ApplyRule(RewriteContext context)
6262
return;
6363
}
6464

65-
if (newPath.IndexOf("://", StringComparison.Ordinal) == -1 && newPath[0] != '/')
65+
if (newPath.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) == -1 && newPath[0] != '/')
6666
{
6767
newPath = '/' + newPath;
6868
}

src/Middleware/Rewrite/src/RewriteRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public virtual void ApplyRule(RewriteContext context)
6060
result = "/";
6161
}
6262

63-
if (result.IndexOf("://", StringComparison.Ordinal) >= 0)
63+
if (result.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) >= 0)
6464
{
6565
string scheme;
6666
HostString host;

src/Middleware/Rewrite/src/UrlActions/RedirectAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void ApplyAction(RewriteContext context, BackReferenceCollection
4545
return;
4646
}
4747

48-
if (pattern.IndexOf("://", StringComparison.Ordinal) == -1 && pattern[0] != '/')
48+
if (pattern.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) == -1 && pattern[0] != '/')
4949
{
5050
pattern = '/' + pattern;
5151
}

src/Middleware/Rewrite/src/UrlActions/RewriteAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override void ApplyAction(RewriteContext context, BackReferenceCollection
6161

6262

6363
// TODO PERF, substrings, object creation, etc.
64-
if (pattern.IndexOf("://", StringComparison.Ordinal) >= 0)
64+
if (pattern.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal) >= 0)
6565
{
6666
string scheme;
6767
HostString host;

src/Mvc/Mvc.Core/src/Routing/UrlHelperBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected string GenerateUrl(string protocol, string host, string virtualPath, s
220220
protocol = string.IsNullOrEmpty(protocol) ? "http" : protocol;
221221
builder.Append(protocol);
222222

223-
builder.Append("://");
223+
builder.Append(Uri.SchemeDelimiter);
224224

225225
host = string.IsNullOrEmpty(host) ? ActionContext.HttpContext.Request.Host.Value : host;
226226
builder.Append(host);
@@ -282,7 +282,7 @@ protected string GenerateUrl(string protocol, string host, string path)
282282
protocol = string.IsNullOrEmpty(protocol) ? "http" : protocol;
283283
builder.Append(protocol);
284284

285-
builder.Append("://");
285+
builder.Append(Uri.SchemeDelimiter);
286286

287287
host = string.IsNullOrEmpty(host) ? ActionContext.HttpContext.Request.Host.Value : host;
288288
builder.Append(host);

src/Security/Authentication/Core/src/AuthenticationHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ protected HttpResponse Response
7676
protected ISystemClock Clock { get; }
7777

7878
/// <summary>
79-
/// Gets the <see cref="IOptionsMonitor{TOptions}"/> to detect changes to options.
79+
/// Gets the <see cref="IOptionsMonitor{TOptions}"/> to detect changes to options.
8080
/// </summary>
8181
protected IOptionsMonitor<TOptions> OptionsMonitor { get; }
8282

8383
/// <summary>
84-
/// The handler calls methods on the events which give the application control at certain points where processing is occurring.
84+
/// The handler calls methods on the events which give the application control at certain points where processing is occurring.
8585
/// If it is not provided a default instance is supplied which does nothing when the methods are called.
8686
/// </summary>
8787
protected virtual object? Events { get; set; }
@@ -99,9 +99,9 @@ protected HttpResponse Response
9999
/// </summary>
100100
protected string CurrentUri
101101
{
102-
get => Request.Scheme + "://" + Request.Host + Request.PathBase + Request.Path + Request.QueryString;
102+
get => Request.Scheme + Uri.SchemeDelimiter + Request.Host + Request.PathBase + Request.Path + Request.QueryString;
103103
}
104-
104+
105105
/// <summary>
106106
/// Initializes a new instance of <see cref="AuthenticationHandler{TOptions}"/>.
107107
/// </summary>
@@ -174,7 +174,7 @@ protected virtual async Task InitializeEventsAsync()
174174
/// <param name="targetPath">The path.</param>
175175
/// <returns>The absolute url.</returns>
176176
protected string BuildRedirectUri(string targetPath)
177-
=> Request.Scheme + "://" + Request.Host + OriginalPathBase + targetPath;
177+
=> Request.Scheme + Uri.SchemeDelimiter + Request.Host + OriginalPathBase + targetPath;
178178

179179
/// <summary>
180180
/// Resolves the scheme that this authentication operation is forwarded to.

src/Servers/HttpSys/src/UrlPrefix.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ public static UrlPrefix Create(string prefix)
109109
string path = null;
110110
var whole = prefix ?? string.Empty;
111111

112-
var schemeDelimiterEnd = whole.IndexOf("://", StringComparison.Ordinal);
112+
var schemeDelimiterEnd = whole.IndexOf(Uri.SchemeDelimiter, StringComparison.Ordinal);
113113
if (schemeDelimiterEnd < 0)
114114
{
115115
throw new FormatException("Invalid prefix, missing scheme separator: " + prefix);
116116
}
117-
var hostDelimiterStart = schemeDelimiterEnd + "://".Length;
117+
var hostDelimiterStart = schemeDelimiterEnd + Uri.SchemeDelimiter.Length;
118118

119119
var pathDelimiterStart = whole.IndexOf("/", hostDelimiterStart, StringComparison.Ordinal);
120120
if (pathDelimiterStart < 0)
@@ -186,7 +186,7 @@ public static UrlPrefix Create(string prefix)
186186
public string Path { get; }
187187

188188
internal string PathWithoutTrailingSlash { get; }
189-
189+
190190
/// <summary>
191191
/// Gets a string representation of the prefix
192192
/// </summary>

0 commit comments

Comments
 (0)