Replace string.Equals with HttpMethods.IsX (#17805)
This commit is contained in:
parent
e0a03bda1a
commit
9a3aacb56a
|
|
@ -102,10 +102,10 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
CheckSSLConfig(httpContext);
|
CheckSSLConfig(httpContext);
|
||||||
|
|
||||||
var method = httpContext.Request.Method;
|
var method = httpContext.Request.Method;
|
||||||
if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase) ||
|
if (HttpMethods.IsGet(method) ||
|
||||||
string.Equals(method, "HEAD", StringComparison.OrdinalIgnoreCase) ||
|
HttpMethods.IsHead(method) ||
|
||||||
string.Equals(method, "OPTIONS", StringComparison.OrdinalIgnoreCase) ||
|
HttpMethods.IsOptions(method) ||
|
||||||
string.Equals(method, "TRACE", StringComparison.OrdinalIgnoreCase))
|
HttpMethods.IsTrace(method))
|
||||||
{
|
{
|
||||||
// Validation not needed for these request types.
|
// Validation not needed for these request types.
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -143,10 +143,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
|
|
||||||
if (corsMetadata is IDisableCorsAttribute)
|
if (corsMetadata is IDisableCorsAttribute)
|
||||||
{
|
{
|
||||||
var isOptionsRequest = string.Equals(
|
var isOptionsRequest = HttpMethods.IsOptions(context.Request.Method);
|
||||||
context.Request.Method,
|
|
||||||
CorsConstants.PreflightHttpMethod,
|
|
||||||
StringComparison.OrdinalIgnoreCase);
|
|
||||||
|
|
||||||
var isCorsPreflightRequest = isOptionsRequest && context.Request.Headers.ContainsKey(CorsConstants.AccessControlRequestMethod);
|
var isCorsPreflightRequest = isOptionsRequest && context.Request.Headers.ContainsKey(CorsConstants.AccessControlRequestMethod);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
var requestHeaders = context.Request.Headers;
|
var requestHeaders = context.Request.Headers;
|
||||||
var origin = requestHeaders[CorsConstants.Origin];
|
var origin = requestHeaders[CorsConstants.Origin];
|
||||||
|
|
||||||
var isOptionsRequest = string.Equals(context.Request.Method, CorsConstants.PreflightHttpMethod, StringComparison.OrdinalIgnoreCase);
|
var isOptionsRequest = HttpMethods.IsOptions(context.Request.Method);
|
||||||
var isPreflightRequest = isOptionsRequest && requestHeaders.ContainsKey(CorsConstants.AccessControlRequestMethod);
|
var isPreflightRequest = isOptionsRequest && requestHeaders.ContainsKey(CorsConstants.AccessControlRequestMethod);
|
||||||
|
|
||||||
if (isOptionsRequest && !isPreflightRequest)
|
if (isOptionsRequest && !isPreflightRequest)
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.HttpOverrides
|
||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
if (string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
|
if (HttpMethods.IsPost(context.Request.Method))
|
||||||
{
|
{
|
||||||
if (_options.FormFieldName != null)
|
if (_options.FormFieldName != null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -346,7 +346,7 @@ namespace Microsoft.AspNetCore.ResponseCaching
|
||||||
var bufferStream = context.ResponseCachingStream.GetBufferStream();
|
var bufferStream = context.ResponseCachingStream.GetBufferStream();
|
||||||
if (!contentLength.HasValue || contentLength == bufferStream.Length
|
if (!contentLength.HasValue || contentLength == bufferStream.Length
|
||||||
|| (bufferStream.Length == 0
|
|| (bufferStream.Length == 0
|
||||||
&& string.Equals(context.HttpContext.Request.Method, "HEAD", StringComparison.OrdinalIgnoreCase)))
|
&& HttpMethods.IsHead(context.HttpContext.Request.Method)))
|
||||||
{
|
{
|
||||||
var response = context.HttpContext.Response;
|
var response = context.HttpContext.Response;
|
||||||
// Add a content-length if required
|
// Add a content-length if required
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc.Core;
|
using Microsoft.AspNetCore.Mvc.Core;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
|
|
@ -83,7 +84,7 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
|
|
||||||
private static bool IsNonGetRequest(ActionContext context)
|
private static bool IsNonGetRequest(ActionContext context)
|
||||||
{
|
{
|
||||||
return !string.Equals(context.HttpContext.Request.Method, "GET", StringComparison.OrdinalIgnoreCase);
|
return !HttpMethods.IsGet(context.HttpContext.Request.Method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
{
|
{
|
||||||
// only redirect for GET requests, otherwise the browser might not propagate the verb and request
|
// only redirect for GET requests, otherwise the browser might not propagate the verb and request
|
||||||
// body correctly.
|
// body correctly.
|
||||||
if (!string.Equals(filterContext.HttpContext.Request.Method, "GET", StringComparison.OrdinalIgnoreCase))
|
if (!HttpMethods.IsGet(filterContext.HttpContext.Request.Method))
|
||||||
{
|
{
|
||||||
filterContext.Result = new StatusCodeResult(StatusCodes.Status403Forbidden);
|
filterContext.Result = new StatusCodeResult(StatusCodes.Status403Forbidden);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -103,11 +103,8 @@ namespace Microsoft.AspNetCore.Mvc.Cors
|
||||||
|
|
||||||
var accessControlRequestMethod =
|
var accessControlRequestMethod =
|
||||||
httpContext.Request.Headers[CorsConstants.AccessControlRequestMethod];
|
httpContext.Request.Headers[CorsConstants.AccessControlRequestMethod];
|
||||||
if (string.Equals(
|
if (HttpMethods.IsOptions(request.Method)
|
||||||
request.Method,
|
&& !StringValues.IsNullOrEmpty(accessControlRequestMethod))
|
||||||
CorsConstants.PreflightHttpMethod,
|
|
||||||
StringComparison.OrdinalIgnoreCase) &&
|
|
||||||
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
|
|
||||||
{
|
{
|
||||||
// If this was a preflight, there is no need to run anything else.
|
// If this was a preflight, there is no need to run anything else.
|
||||||
context.Result = new StatusCodeResult(StatusCodes.Status204NoContent);
|
context.Result = new StatusCodeResult(StatusCodes.Status204NoContent);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ namespace Microsoft.AspNetCore.Mvc.Cors
|
||||||
{
|
{
|
||||||
private readonly string OriginHeader = "Origin";
|
private readonly string OriginHeader = "Origin";
|
||||||
private readonly string AccessControlRequestMethod = "Access-Control-Request-Method";
|
private readonly string AccessControlRequestMethod = "Access-Control-Request-Method";
|
||||||
private readonly string PreflightHttpMethod = "OPTIONS";
|
|
||||||
|
|
||||||
public CorsHttpMethodActionConstraint(HttpMethodActionConstraint constraint)
|
public CorsHttpMethodActionConstraint(HttpMethodActionConstraint constraint)
|
||||||
: base(constraint.HttpMethods)
|
: base(constraint.HttpMethods)
|
||||||
|
|
@ -34,7 +33,7 @@ namespace Microsoft.AspNetCore.Mvc.Cors
|
||||||
|
|
||||||
var request = context.RouteContext.HttpContext.Request;
|
var request = context.RouteContext.HttpContext.Request;
|
||||||
// Perf: Check http method before accessing the Headers collection.
|
// Perf: Check http method before accessing the Headers collection.
|
||||||
if (string.Equals(request.Method, PreflightHttpMethod, StringComparison.OrdinalIgnoreCase) &&
|
if (Http.HttpMethods.IsOptions(request.Method) &&
|
||||||
request.Headers.ContainsKey(OriginHeader) &&
|
request.Headers.ContainsKey(OriginHeader) &&
|
||||||
request.Headers.TryGetValue(AccessControlRequestMethod, out var accessControlRequestMethod) &&
|
request.Headers.TryGetValue(AccessControlRequestMethod, out var accessControlRequestMethod) &&
|
||||||
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
|
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ using System.Reflection;
|
||||||
using System.Text.Encodings.Web;
|
using System.Text.Encodings.Web;
|
||||||
using Microsoft.AspNetCore.Antiforgery;
|
using Microsoft.AspNetCore.Antiforgery;
|
||||||
using Microsoft.AspNetCore.Html;
|
using Microsoft.AspNetCore.Html;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||||
using Microsoft.AspNetCore.Mvc.Routing;
|
using Microsoft.AspNetCore.Mvc.Routing;
|
||||||
|
|
@ -307,7 +308,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures
|
||||||
{
|
{
|
||||||
defaultMethod = true;
|
defaultMethod = true;
|
||||||
}
|
}
|
||||||
else if (string.Equals(method, "post", StringComparison.OrdinalIgnoreCase))
|
else if (HttpMethods.IsPost(method))
|
||||||
{
|
{
|
||||||
defaultMethod = true;
|
defaultMethod = true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.AspNetCore.Antiforgery;
|
using Microsoft.AspNetCore.Antiforgery;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
|
@ -23,10 +24,10 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Filters
|
||||||
}
|
}
|
||||||
|
|
||||||
var method = context.HttpContext.Request.Method;
|
var method = context.HttpContext.Request.Method;
|
||||||
if (string.Equals("GET", method, StringComparison.OrdinalIgnoreCase) ||
|
if (HttpMethods.IsGet(method) ||
|
||||||
string.Equals("HEAD", method, StringComparison.OrdinalIgnoreCase) ||
|
HttpMethods.IsHead(method) ||
|
||||||
string.Equals("TRACE", method, StringComparison.OrdinalIgnoreCase) ||
|
HttpMethods.IsTrace(method) ||
|
||||||
string.Equals("OPTIONS", method, StringComparison.OrdinalIgnoreCase))
|
HttpMethods.IsOptions(method))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ namespace JwtBearerSample
|
||||||
todoApp.Run(async context =>
|
todoApp.Run(async context =>
|
||||||
{
|
{
|
||||||
var response = context.Response;
|
var response = context.Response;
|
||||||
if (context.Request.Method.Equals("POST", System.StringComparison.OrdinalIgnoreCase))
|
if (HttpMethods.IsPost(context.Request.Method))
|
||||||
{
|
{
|
||||||
var reader = new StreamReader(context.Request.Body);
|
var reader = new StreamReader(context.Request.Body);
|
||||||
var body = await reader.ReadToEndAsync();
|
var body = await reader.ReadToEndAsync();
|
||||||
|
|
|
||||||
|
|
@ -78,13 +78,13 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
|
||||||
{
|
{
|
||||||
OpenIdConnectMessage message = null;
|
OpenIdConnectMessage message = null;
|
||||||
|
|
||||||
if (string.Equals(Request.Method, "GET", StringComparison.OrdinalIgnoreCase))
|
if (HttpMethods.IsGet(Request.Method))
|
||||||
{
|
{
|
||||||
message = new OpenIdConnectMessage(Request.Query.Select(pair => new KeyValuePair<string, string[]>(pair.Key, pair.Value)));
|
message = new OpenIdConnectMessage(Request.Query.Select(pair => new KeyValuePair<string, string[]>(pair.Key, pair.Value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// assumption: if the ContentType is "application/x-www-form-urlencoded" it should be safe to read as it is small.
|
// assumption: if the ContentType is "application/x-www-form-urlencoded" it should be safe to read as it is small.
|
||||||
else if (string.Equals(Request.Method, "POST", StringComparison.OrdinalIgnoreCase)
|
else if (HttpMethods.IsPost(Request.Method)
|
||||||
&& !string.IsNullOrEmpty(Request.ContentType)
|
&& !string.IsNullOrEmpty(Request.ContentType)
|
||||||
// May have media/type; charset=utf-8, allow partial match.
|
// May have media/type; charset=utf-8, allow partial match.
|
||||||
&& Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
|
&& Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
|
||||||
|
|
@ -482,7 +482,7 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
|
||||||
|
|
||||||
OpenIdConnectMessage authorizationResponse = null;
|
OpenIdConnectMessage authorizationResponse = null;
|
||||||
|
|
||||||
if (string.Equals(Request.Method, "GET", StringComparison.OrdinalIgnoreCase))
|
if (HttpMethods.IsGet(Request.Method))
|
||||||
{
|
{
|
||||||
authorizationResponse = new OpenIdConnectMessage(Request.Query.Select(pair => new KeyValuePair<string, string[]>(pair.Key, pair.Value)));
|
authorizationResponse = new OpenIdConnectMessage(Request.Query.Select(pair => new KeyValuePair<string, string[]>(pair.Key, pair.Value)));
|
||||||
|
|
||||||
|
|
@ -501,7 +501,7 @@ namespace Microsoft.AspNetCore.Authentication.OpenIdConnect
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// assumption: if the ContentType is "application/x-www-form-urlencoded" it should be safe to read as it is small.
|
// assumption: if the ContentType is "application/x-www-form-urlencoded" it should be safe to read as it is small.
|
||||||
else if (string.Equals(Request.Method, "POST", StringComparison.OrdinalIgnoreCase)
|
else if (HttpMethods.IsPost(Request.Method)
|
||||||
&& !string.IsNullOrEmpty(Request.ContentType)
|
&& !string.IsNullOrEmpty(Request.ContentType)
|
||||||
// May have media/type; charset=utf-8, allow partial match.
|
// May have media/type; charset=utf-8, allow partial match.
|
||||||
&& Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
|
&& Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase)
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ namespace Interop.FunctionalTests
|
||||||
.ConfigureServices(AddTestLogging)
|
.ConfigureServices(AddTestLogging)
|
||||||
.Configure(app => app.Run(async context =>
|
.Configure(app => app.Run(async context =>
|
||||||
{
|
{
|
||||||
if (string.Equals(context.Request.Query["TestMethod"], "POST", StringComparison.OrdinalIgnoreCase))
|
if (HttpMethods.IsPost(context.Request.Query["TestMethod"]))
|
||||||
{
|
{
|
||||||
await context.Response.WriteAsync(_postHtml);
|
await context.Response.WriteAsync(_postHtml);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ namespace FunctionalTests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.Equals(context.Request.Method, "OPTIONS", StringComparison.OrdinalIgnoreCase))
|
if (HttpMethods.IsOptions(context.Request.Method))
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = StatusCodes.Status204NoContent;
|
context.Response.StatusCode = StatusCodes.Status204NoContent;
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue