Class ReverseProxyBuilder

  • All Implemented Interfaces:
    io.muserver.MuHandlerBuilder<ReverseProxy>

    public class ReverseProxyBuilder
    extends java.lang.Object
    implements io.muserver.MuHandlerBuilder<ReverseProxy>
    A builder for creating a reverse proxy, which is a MuHandler that can be added to a Mu Server.
    Version:
    1.0
    Author:
    Daniel Flower
    • Method Detail

      • withViaName

        public ReverseProxyBuilder withViaName​(java.lang.String viaName)
        The name to add as the Via header, which defaults to private.
        Parameters:
        viaName - The name to add to the Via header.
        Returns:
        This builder
      • withHttpClient

        public ReverseProxyBuilder withHttpClient​(java.net.http.HttpClient httpClient)
        Specifies the JDK HTTP client to use to make the request to the target server. It's recommended you do not set this in order to use the default client that is optimised for reverse proxy usage.
        Parameters:
        httpClient - The HTTP client to use, or null to use the default client.
        Returns:
        This builder
      • createHttpClientBuilder

        public static java.net.http.HttpClient.Builder createHttpClientBuilder​(boolean trustAll)
        Creates a new HTTP Client builder that is suitable for use in mu reverse proxy.
        Parameters:
        trustAll - If true, then any SSL certificate is allowed.
        Returns:
        An HTTP Client builder
      • withUriMapper

        public ReverseProxyBuilder withUriMapper​(UriMapper uriMapper)
        Required value. Sets the mapper to use for creating target URIs.

        If you want to proxy all requests to a single destination, consider using UriMapper.toDomain(URI)

        If the mapper function returns null, then the handler will not proxy the request and the next handler in the chain will be invoked (or a 404 will be returned if there are no further handlers that can handle the request).

        Parameters:
        uriMapper - A mapper that creates a target URI based on a client request.
        Returns:
        This builder
      • sendLegacyForwardedHeaders

        public ReverseProxyBuilder sendLegacyForwardedHeaders​(boolean sendLegacyForwardedHeaders)
        Murp always sends Forwarded headers, however by default does not send the non-standard X-Forwarded-* headers. Set this to true to enable these legacy headers for older clients that rely on them.
        Parameters:
        sendLegacyForwardedHeaders - true to forward headers such as X-Forwarded-Host; otherwise false
        Returns:
        This builder
      • proxyHostHeader

        public ReverseProxyBuilder proxyHostHeader​(boolean sendHostToTarget)

        Specifies whether to send the original Host header to the target server.

        Reverse proxies are generally supposed to forward the original Host header to target servers, however there are cases (particularly where you are proxying to HTTPS servers) that the Host needs to match the Host of the SSL certificate (in which case you may see SNI-related errors).

        Parameters:
        sendHostToTarget - If true (which is the default) the Host request header will be sent to the target; if false then the host header will be based on the target's URL.
        Returns:
        This builder
      • discardClientForwardedHeaders

        public ReverseProxyBuilder discardClientForwardedHeaders​(boolean discardClientForwardedHeaders)
        If true, then any Forwarded or X-Forwarded-* headers that are sent from the client to this reverse proxy will be dropped (defaults to false). Set this to true if you do not trust the client.
        Parameters:
        discardClientForwardedHeaders - true to ignore Forwarded headers from the client; otherwise false
        Returns:
        This builder
      • withTotalTimeout

        public ReverseProxyBuilder withTotalTimeout​(long totalTimeoutInMillis)
        Sets the total request timeout in millis for a proxied request. Defaults to 5 minutes.
        Parameters:
        totalTimeoutInMillis - The allowed time in milliseconds for a request.
        Returns:
        This builder
      • withTotalTimeout

        public ReverseProxyBuilder withTotalTimeout​(long totalTimeout,
                                                    java.util.concurrent.TimeUnit unit)
        Sets the total request timeout in millis for a proxied request. Defaults to 5 minutes.
        Parameters:
        totalTimeout - The allowed time for a request.
        unit - The timeout unit.
        Returns:
        This builder
      • addProxyCompleteListener

        public ReverseProxyBuilder addProxyCompleteListener​(ProxyCompleteListener proxyCompleteListener)
        Registers a proxy completion listener.
        Parameters:
        proxyCompleteListener - A listener to be called when a proxy request is complete
        Returns:
        This builder
      • withRequestInterceptor

        public ReverseProxyBuilder withRequestInterceptor​(RequestInterceptor requestInterceptor)
        Adds an interceptor to the point where a request to the target server has been prepared, but not sent. This allows you to change the headers being proxied to the target server.
        Parameters:
        requestInterceptor - An interceptor that may change the target request, or null to not have an interceptor.
        Returns:
        This builder.
      • withResponseInterceptor

        public ReverseProxyBuilder withResponseInterceptor​(ResponseInterceptor responseInterceptor)
        Adds an interceptor to the point where a response to the client has been prepared, but not sent. This allows you to change the response code or headers being returned to the client.
        Parameters:
        responseInterceptor - An interceptor that may change the client response, or null to not have an interceptor.
        Returns:
        This builder.
      • withProxyListener

        public ReverseProxyBuilder withProxyListener​(ProxyListener proxyListener)
        Adds a proxy listener to observe the life cycle of a request, it's useful for debug or metric
        Parameters:
        proxyListener - proxy listener
        Returns:
        This builder.
      • reverseProxy

        public static ReverseProxyBuilder reverseProxy()
        Creates and returns a new instance of a reverse proxy builder.
        Returns:
        A builder
      • build

        public ReverseProxy build()
        Creates a new ReverseProxy which is a MuHandler. You can pass the resulting handler directly to MuServerBuilder.addHandler(MuHandler)
        Specified by:
        build in interface io.muserver.MuHandlerBuilder<ReverseProxy>