/
ResponseTrailerRemoveTransform.cs
56 lines (45 loc) · 1.72 KB
/
ResponseTrailerRemoveTransform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features;
namespace Yarp.ReverseProxy.Transforms;
/// <summary>
/// Removes a response trailer.
/// </summary>
public class ResponseTrailerRemoveTransform : ResponseTrailersTransform
{
public ResponseTrailerRemoveTransform(string headerName, ResponseCondition condition)
{
if (string.IsNullOrEmpty(headerName))
{
throw new ArgumentException($"'{nameof(headerName)}' cannot be null or empty.", nameof(headerName));
}
HeaderName = headerName;
Condition = condition;
}
internal string HeaderName { get; }
internal ResponseCondition Condition { get; }
// Assumes the response status code has been set on the HttpContext already.
/// <inheritdoc/>
public override ValueTask ApplyAsync(ResponseTrailersTransformContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
Debug.Assert(context.ProxyResponse is not null);
if (Condition == ResponseCondition.Always
|| Success(context) == (Condition == ResponseCondition.Success))
{
var responseTrailersFeature = context.HttpContext.Features.Get<IHttpResponseTrailersFeature>();
var responseTrailers = responseTrailersFeature?.Trailers;
// Support should have already been checked by the caller.
Debug.Assert(responseTrailers is not null);
Debug.Assert(!responseTrailers.IsReadOnly);
responseTrailers.Remove(HeaderName);
}
return default;
}
}