-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Avoid allocating FormFeature in HttpRequest.HasFormContentType #50830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
---|---|---|
|
@@ -147,10 +147,8 @@ public override string? ContentType | |
set { Headers.ContentType = value; } | ||
} | ||
|
||
public override bool HasFormContentType | ||
{ | ||
get { return FormFeature.HasFormContentType; } | ||
} | ||
public override bool HasFormContentType => HttpExtensions.HasAnyFormContentType(ContentType) | ||
|| _features.Fetch(ref _features.Cache.Form, _features.Collection, collection => collection.Get<IFormFeature>()) != null; | ||
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. Isn't this just checking for the presence of the form feature? If present, it should check the form feature's HasFormContentType. The current form feature should also take precedence if it exists. |
||
|
||
public override IFormCollection Form | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
internal static class HttpExtensions | ||
{ | ||
internal const string UrlEncodedFormContentType = "application/x-www-form-urlencoded"; | ||
internal const string MultipartFormContentType = "multipart/form-data"; | ||
private const string UrlEncodedFormContentType = "application/x-www-form-urlencoded"; | ||
private const string MultipartFormContentType = "multipart/form-data"; | ||
|
||
internal static bool IsValidHttpMethodForForm(string method) => | ||
HttpMethods.IsPost(method) || HttpMethods.IsPut(method) || HttpMethods.IsPatch(method); | ||
|
||
internal static bool IsValidContentTypeForForm(string? contentType) | ||
{ | ||
if (contentType == null) | ||
{ | ||
return false; | ||
} | ||
|
||
// Abort early if this doesn't look like it could be a form-related content-type | ||
internal static bool HasAnyFormContentType([NotNullWhen(true)] string? contentType) | ||
=> HasApplicationFormContentType(contentType) || HasMultipartFormContentType(contentType); | ||
|
||
if (contentType.Length < MultipartFormContentType.Length) | ||
{ | ||
return false; | ||
} | ||
internal static bool HasApplicationFormContentType([NotNullWhen(true)] string? contentType) | ||
{ | ||
// Content-Type: application/x-www-form-urlencoded; charset=utf-8 | ||
return contentType != null && contentType.Contains(UrlEncodedFormContentType, StringComparison.OrdinalIgnoreCase); | ||
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. This is less precise / robust than the prior implementation. It's ok in the normal scenario, but it can cause strange random bugs for other inputs. E.g. this now matches "web-application/x-www-form-urlencoded-foo". We have tools that can get the same precision as before without allocating: 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.
Ah, perfect! This is exactly what I was looking for. |
||
} | ||
|
||
return contentType.Equals(UrlEncodedFormContentType, StringComparison.OrdinalIgnoreCase) || | ||
contentType.StartsWith(MultipartFormContentType, StringComparison.OrdinalIgnoreCase); | ||
internal static bool HasMultipartFormContentType([NotNullWhen(true)] string? contentType) | ||
{ | ||
// Content-Type: multipart/form-data; boundary=----WebKitFormBoundarymx2fSWqWSd0OxQqq | ||
return contentType != null && contentType.Contains(MultipartFormContentType, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} |
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.
nit: I don't like that the ContentType property allocates a
MediaTypeHeaderValue
. IMO it should be a method.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.
The type is more reliable, methods just re-invent the parsers.