diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/Extensions/ResponseTrailerExtensions.cs b/src/Microsoft.AspNetCore.Http.Abstractions/Extensions/ResponseTrailerExtensions.cs new file mode 100644 index 0000000000..745cbfd9de --- /dev/null +++ b/src/Microsoft.AspNetCore.Http.Abstractions/Extensions/ResponseTrailerExtensions.cs @@ -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"; + + /// + /// Adds the given trailer name to the 'Trailer' response header. This must happen before the response headers are sent. + /// + /// + /// + public static void DeclareTrailer(this HttpResponse response, string trailerName) + { + response.Headers.AppendCommaSeparatedValues(Trailer, trailerName); + } + + /// + /// Indicates if the server supports sending trailer headers for this response. + /// + /// + /// + public static bool SupportsTrailers(this HttpResponse response) + { + var feature = response.HttpContext.Features.Get(); + return feature?.Trailers != null && !feature.Trailers.IsReadOnly; + } + + /// + /// Adds the given trailer header to the trailers collection to be sent at the end of the response body. + /// Check or an InvalidOperationException may be thrown. + /// + /// + /// + /// + public static void AppendTrailer(this HttpResponse response, string trailerName, StringValues trailerValues) + { + var feature = response.HttpContext.Features.Get(); + if (feature?.Trailers == null || feature.Trailers.IsReadOnly) + { + throw new InvalidOperationException("Trailers are not supported for this response."); + } + + feature.Trailers.Append(trailerName, trailerValues); + } + } +} diff --git a/src/Microsoft.AspNetCore.Http.Features/IHttpResponseTrailersFeature.cs b/src/Microsoft.AspNetCore.Http.Features/IHttpResponseTrailersFeature.cs new file mode 100644 index 0000000000..edd8fbea35 --- /dev/null +++ b/src/Microsoft.AspNetCore.Http.Features/IHttpResponseTrailersFeature.cs @@ -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; } + } +}