Table of Contents

Class HttpTransformer

Namespace
Yarp.ReverseProxy.Forwarder
Assembly
Yarp.ReverseProxy.dll
public class HttpTransformer
Inheritance
HttpTransformer
Inherited Members

Constructors

HttpTransformer()

Used to create derived instances.

protected HttpTransformer()

Fields

Default

A default set of transforms that adds X-Forwarded-* headers, removes the original Host value and copies all other request and response fields and headers, except for some protocol specific values.

public static readonly HttpTransformer Default

Field Value

HttpTransformer

Empty

An empty transformer that copies all request and response fields and headers, except for some protocol specific values.

public static readonly HttpTransformer Empty

Field Value

HttpTransformer

Methods

TransformRequestAsync(HttpContext, HttpRequestMessage, string)

A callback that is invoked prior to sending the proxied request. All HttpRequestMessage fields are initialized except RequestUri, which will be initialized after the callback if no value is provided. See MakeDestinationAddress(string, PathString, QueryString) for constructing a custom request Uri. The string parameter represents the destination URI prefix that should be used when constructing the RequestUri. The headers are copied by the base implementation, excluding some protocol headers like HTTP/2 pseudo headers (":authority"). This method may be overridden to conditionally produce a response, such as for error conditions, and prevent the request from being proxied. This is indicated by setting the HttpResponse.StatusCode to a value other than 200, or calling HttpResponse.StartAsync(), or writing to the HttpResponse.Body or BodyWriter.

[Obsolete("This overload of TransformRequestAsync is obsolete. Override and use the overload accepting a CancellationToken instead.")]
public virtual ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix)

Parameters

httpContext HttpContext

The incoming request.

proxyRequest HttpRequestMessage

The outgoing proxy request.

destinationPrefix string

The uri prefix for the selected destination server which can be used to create the RequestUri.

Returns

ValueTask

TransformRequestAsync(HttpContext, HttpRequestMessage, string, CancellationToken)

A callback that is invoked prior to sending the proxied request. All HttpRequestMessage fields are initialized except RequestUri, which will be initialized after the callback if no value is provided. See MakeDestinationAddress(string, PathString, QueryString) for constructing a custom request Uri. The string parameter represents the destination URI prefix that should be used when constructing the RequestUri. The headers are copied by the base implementation, excluding some protocol headers like HTTP/2 pseudo headers (":authority"). This method may be overridden to conditionally produce a response, such as for error conditions, and prevent the request from being proxied. This is indicated by setting the HttpResponse.StatusCode to a value other than 200, or calling HttpResponse.StartAsync(), or writing to the HttpResponse.Body or BodyWriter.

public virtual ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken)

Parameters

httpContext HttpContext

The incoming request.

proxyRequest HttpRequestMessage

The outgoing proxy request.

destinationPrefix string

The uri prefix for the selected destination server which can be used to create the RequestUri.

cancellationToken CancellationToken

Indicates that the request is being canceled.

Returns

ValueTask

TransformResponseAsync(HttpContext, HttpResponseMessage?)

A callback that is invoked when the proxied response is received. The status code and reason phrase will be copied to the HttpContext.Response before the callback is invoked, but may still be modified there. The headers will be copied to HttpContext.Response.Headers by the base implementation, excludes certain protocol headers like Transfer-Encoding: chunked.

[Obsolete("This overload of TransformResponseAsync is obsolete. Override and use the overload accepting a CancellationToken instead.")]
public virtual ValueTask<bool> TransformResponseAsync(HttpContext httpContext, HttpResponseMessage? proxyResponse)

Parameters

httpContext HttpContext

The incoming request.

proxyResponse HttpResponseMessage

The response from the destination. This can be null if the destination did not respond.

Returns

ValueTask<bool>

A bool indicating if the response should be proxied to the client or not. A derived implementation that returns false may send an alternate response inline or return control to the caller for it to retry, respond, etc.

TransformResponseAsync(HttpContext, HttpResponseMessage?, CancellationToken)

A callback that is invoked when the proxied response is received. The status code and reason phrase will be copied to the HttpContext.Response before the callback is invoked, but may still be modified there. The headers will be copied to HttpContext.Response.Headers by the base implementation, excludes certain protocol headers like Transfer-Encoding: chunked.

public virtual ValueTask<bool> TransformResponseAsync(HttpContext httpContext, HttpResponseMessage? proxyResponse, CancellationToken cancellationToken)

Parameters

httpContext HttpContext

The incoming request.

proxyResponse HttpResponseMessage

The response from the destination. This can be null if the destination did not respond.

cancellationToken CancellationToken

Indicates that the request is being canceled.

Returns

ValueTask<bool>

A bool indicating if the response should be proxied to the client or not. A derived implementation that returns false may send an alternate response inline or return control to the caller for it to retry, respond, etc.

TransformResponseTrailersAsync(HttpContext, HttpResponseMessage)

A callback that is invoked after the response body to modify trailers, if supported. The trailers will be copied to the HttpContext.Response by the base implementation.

[Obsolete("This overload of TransformResponseTrailersAsync is obsolete. Override and use the overload accepting a CancellationToken instead.")]
public virtual ValueTask TransformResponseTrailersAsync(HttpContext httpContext, HttpResponseMessage proxyResponse)

Parameters

httpContext HttpContext

The incoming request.

proxyResponse HttpResponseMessage

The response from the destination.

Returns

ValueTask

TransformResponseTrailersAsync(HttpContext, HttpResponseMessage, CancellationToken)

A callback that is invoked after the response body to modify trailers, if supported. The trailers will be copied to the HttpContext.Response by the base implementation.

public virtual ValueTask TransformResponseTrailersAsync(HttpContext httpContext, HttpResponseMessage proxyResponse, CancellationToken cancellationToken)

Parameters

httpContext HttpContext

The incoming request.

proxyResponse HttpResponseMessage

The response from the destination.

cancellationToken CancellationToken

Indicates that the request is being canceled.

Returns

ValueTask