Skip to content

Commit 5fb8d0f

Browse files
authored
Adding nullable type functionality for HttpRequest Context Type (#32203)
* Added "?" to make type string nullable for ContentType. Updated PublicAPI.Unshipped.txt to reflect change to nullable type. * Added *REMOVED* counter part to addition as requested. * Added "?" to update DefaultHTTPRequest ContentType property to allow nullable. * Added null value marker for EscapedValueOrEmptyMarker function used by ToString function. * Updated contentType paramater to include null values in TryGetEncodingForMediaType function. * Implemented TryParse on content type. If false, returns false for TryEncodingForMediaType. If true, continues through Encoding process.
1 parent b737986 commit 5fb8d0f

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ IEnumerator IEnumerable.GetEnumerator()
6969
internal string ToStringWithoutPreamble()
7070
=> ToString().Substring(LogPreamble.Length);
7171

72-
internal static string EscapedValueOrEmptyMarker(string potentialValue)
72+
internal static string EscapedValueOrEmptyMarker(string? potentialValue)
7373
// Encode space as +
7474
=> potentialValue?.Length > 0 ? potentialValue.Replace(' ', '+') : EmptyEntry;
7575

src/Http/Http.Abstractions/src/HttpRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public abstract class HttpRequest
9696
/// Gets or sets the Content-Type header.
9797
/// </summary>
9898
/// <returns>The Content-Type header.</returns>
99-
public abstract string ContentType { get; set; }
99+
public abstract string? ContentType { get; set; }
100100

101101
/// <summary>
102102
/// Gets or sets the request body <see cref="Stream"/>.

src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*REMOVED*Microsoft.AspNetCore.Routing.RouteValueDictionary.TryAdd(string! key, object! value) -> bool
55
*REMOVED*static Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, System.Type! middleware, params object![]! args) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
66
*REMOVED*static Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware<TMiddleware>(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, params object![]! args) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
7+
*REMOVED*abstract Microsoft.AspNetCore.Http.HttpRequest.ContentType.get -> string!
78
Microsoft.AspNetCore.Http.IResult
89
Microsoft.AspNetCore.Http.IResult.ExecuteAsync(Microsoft.AspNetCore.Http.HttpContext! httpContext) -> System.Threading.Tasks.Task!
910
Microsoft.AspNetCore.Http.Metadata.IFromBodyMetadata
@@ -18,6 +19,7 @@ Microsoft.AspNetCore.Http.Metadata.IFromServiceMetadata
1819
Microsoft.AspNetCore.Http.Endpoint.Endpoint(Microsoft.AspNetCore.Http.RequestDelegate? requestDelegate, Microsoft.AspNetCore.Http.EndpointMetadataCollection? metadata, string? displayName) -> void
1920
Microsoft.AspNetCore.Http.Endpoint.RequestDelegate.get -> Microsoft.AspNetCore.Http.RequestDelegate?
2021
Microsoft.AspNetCore.Routing.RouteValueDictionary.TryAdd(string! key, object? value) -> bool
22+
abstract Microsoft.AspNetCore.Http.HttpRequest.ContentType.get -> string?
2123
static Microsoft.AspNetCore.Builder.UseExtensions.Use(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, System.Func<Microsoft.AspNetCore.Http.HttpContext!, Microsoft.AspNetCore.Http.RequestDelegate!, System.Threading.Tasks.Task!>! middleware) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
2224
static Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, System.Type! middleware, params object?[]! args) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
2325
static Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.UseMiddleware<TMiddleware>(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, params object?[]! args) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!

src/Http/Http/src/Internal/DefaultHttpRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public override IRequestCookieCollection Cookies
146146
set { RequestCookiesFeature.Cookies = value; }
147147
}
148148

149-
public override string ContentType
149+
public override string? ContentType
150150
{
151151
get { return Headers.ContentType; }
152152
set { Headers.ContentType = value; }

src/Middleware/HttpLogging/src/MediaTypeHelpers.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@ internal static class MediaTypeHelpers
2020
Encoding.Latin1 // TODO allowed by default? Make this configurable?
2121
};
2222

23-
public static bool TryGetEncodingForMediaType(string contentType, List<MediaTypeState> mediaTypeList, [NotNullWhen(true)] out Encoding? encoding)
23+
public static bool TryGetEncodingForMediaType(string? contentType, List<MediaTypeState> mediaTypeList, [NotNullWhen(true)] out Encoding? encoding)
2424
{
2525
encoding = null;
2626
if (mediaTypeList == null || mediaTypeList.Count == 0 || string.IsNullOrEmpty(contentType))
2727
{
2828
return false;
2929
}
3030

31-
var mediaType = new MediaTypeHeaderValue(contentType);
31+
if (!MediaTypeHeaderValue.TryParse(contentType, out var mediaType))
32+
{
33+
return false;
34+
}
3235

3336
if (mediaType.Charset.HasValue)
3437
{

0 commit comments

Comments
 (0)