From 21236cc98e995d534cbe62234cd4725ad54a787a Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Tue, 30 Aug 2016 11:08:41 -0700 Subject: [PATCH] [Fixes #5016] Perf: Check for OPTIONS before looking for CORS headers --- .../Internal/HttpMethodActionConstraint.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/HttpMethodActionConstraint.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/HttpMethodActionConstraint.cs index 187c4efed0..4e1a94250c 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/HttpMethodActionConstraint.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/HttpMethodActionConstraint.cs @@ -69,15 +69,14 @@ namespace Microsoft.AspNetCore.Mvc.Internal var request = context.RouteContext.HttpContext.Request; var method = request.Method; - if (request.Headers.ContainsKey(OriginHeader)) + + // Perf: Check http method before accessing the Headers collection. + if (string.Equals(method, PreflightHttpMethod, StringComparison.OrdinalIgnoreCase) && + request.Headers.ContainsKey(OriginHeader)) { // Update the http method if it is preflight request. var accessControlRequestMethod = request.Headers[AccessControlRequestMethod]; - if (string.Equals( - request.Method, - PreflightHttpMethod, - StringComparison.OrdinalIgnoreCase) && - !StringValues.IsNullOrEmpty(accessControlRequestMethod)) + if (!StringValues.IsNullOrEmpty(accessControlRequestMethod)) { method = accessControlRequestMethod; }