This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IHttpResponseTrailersFeature and extensions
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/Microsoft.AspNetCore.Http.Abstractions/Extensions/ResponseTrailerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Microsoft.AspNetCore.Http | ||
{ | ||
public static class ResponseTrailerExtensions | ||
{ | ||
private const string Trailer = "Trailer"; | ||
|
||
/// <summary> | ||
/// Adds the given trailer name to the 'Trailer' response header. This must happen before the response headers are sent. | ||
/// </summary> | ||
/// <param name="response"></param> | ||
/// <param name="trailerName"></param> | ||
public static void DeclareTrailer(this HttpResponse response, string trailerName) | ||
{ | ||
response.Headers.AppendCommaSeparatedValues(Trailer, trailerName); | ||
} | ||
|
||
/// <summary> | ||
/// Indicates if the server supports sending trailer headers for this response. | ||
/// </summary> | ||
/// <param name="response"></param> | ||
/// <returns></returns> | ||
public static bool SupportsTrailers(this HttpResponse response) | ||
{ | ||
var feature = response.HttpContext.Features.Get<IHttpResponseTrailersFeature>(); | ||
return feature?.Trailers != null && !feature.Trailers.IsReadOnly; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the given trailer header to the trailers collection to be sent at the end of the response body. | ||
/// Check <see cref="SupportsTrailers" /> or an InvalidOperationException may be thrown. | ||
/// </summary> | ||
/// <param name="response"></param> | ||
/// <param name="trailerName"></param> | ||
/// <param name="trailerValues"></param> | ||
public static void AppendTrailer(this HttpResponse response, string trailerName, StringValues trailerValues) | ||
{ | ||
var feature = response.HttpContext.Features.Get<IHttpResponseTrailersFeature>(); | ||
if (feature?.Trailers == null || feature.Trailers.IsReadOnly) | ||
{ | ||
throw new InvalidOperationException("Trailers are not supported for this response."); | ||
} | ||
|
||
feature.Trailers.Append(trailerName, trailerValues); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Microsoft.AspNetCore.Http.Features/IHttpResponseTrailersFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Http.Features | ||
{ | ||
public interface IHttpResponseTrailersFeature | ||
{ | ||
IHeaderDictionary Trailers { get; set; } | ||
} | ||
} |