Class HttpFiltersBuilder
HttpFilters from lambdas, one per hook, instead of a class that overrides the
hooks it needs:
HttpFilters filters = HttpFilters.builder()
.onRequest(req -> req.uri().endsWith("/blocked") ? forbidden() : null)
.onResponse(res -> { res.headers().set("X-Proxy", "micro"); return res; })
.onWebSocketFrame((frame, fromClient) -> frame.isPing() ? null : frame)
.onWebSocketMessage((msg, fromClient) -> msg.isText() ? msg.withText(msg.text().trim()) : msg)
.build();
MicroProxy.bootstrap().withFiltersSource((request, ctx) -> filters).start();
Registering the same hook twice runs both, in order: the first short-circuit response wins,
and transformations feed into each other (null aborts, as in HttpFiltersChain).
Bodies, WebSocket frames and messages are only parsed into pieces when a body, frame or message hook was
registered (HttpFiltersBuilder.Built.sees(org.microproxy.HttpFiltersBuilder.Body)); otherwise they are relayed by the proxy's fast path, just as for
a filters class that does not override those hooks. The built filters keep no state of their
own, so one instance can serve every request if the lambdas allow it.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe streams a built filter inspects piece by piece.static final classFilters made byHttpFiltersBuilder. -
Method Summary
Modifier and TypeMethodDescriptionallowMitm(BooleanSupplier allow) Whether CONNECT requests may be intercepted (default true when MITM is configured).allowMitmFor(Predicate<? super ClientHello> allow) Whether a TLS connection may be intercepted, decided from the client'sClientHello(its server name and ALPN protocols); seeHttpFilters.proxyToServerAllowMitm(ClientHello).The response head just before it goes to the client (proxyToClientResponse).The request head just before it goes to the server (proxyToServerRequest), with the proxy's own header changes applied.bufferRequests(int maxBytes) Buffer request bodies up tomaxBytes, soonRequestgets the whole request.bufferResponses(int maxBytes) Buffer response bodies up tomaxBytes, soonResponsegets the whole response.build()Creates a filter source combining the configured hooks.connectTimeout(Duration timeout) The connect timeout for these requests' new connections, instead of the server's (seeHttpFilters.proxyToServerConnectTimeout()).log(HttpLogger logger) Logs each exchange withlogger, around these hooks: requests as the client sent them (beforeonRequest) and as forwarded (afterbeforeSending), responses as the server sent them (beforeonResponse) and as delivered (afterbeforeResponding).mitmServerHandshake(MitmServerHandshake handshake) When the proxy runs its TLS handshake with the server for these requests' interceptedCONNECTs, instead of the protocol policy's choice (seeHttpFilters.proxyToServerMitmHandshake()).The proxy's own answer when the request fails (proxyToServerFailure): return a response to send instead of the default, ornull.onRequest(Function<HttpRequest, HttpResponse> hook) The request head as the client sent it (clientToProxyRequest); a wholeFullHttpRequestwhen buffered (bufferRequests(int)).onRequestBody(Consumer<HttpContent> hook) Each piece of a streamed request body, ending with aLastHttpContent.onResponse(UnaryOperator<HttpResponse> hook) The response head from the server (serverToProxyResponse); a wholeFullHttpResponsewhen buffered (bufferResponses(int)).Each piece of a streamed response body: return it, a replacement, ornullto abort.Each WebSocket frame, with whether it came from the client: return it, a replacement, ornullto drop it (seeHttpFilters.filterWebSocketFrame(org.microproxy.http.WebSocketFrame, boolean)).Each whole WebSocket message, joined from its frames (and inflated), with whether it came from the client: return it, a replacement, ornullto drop it (seeHttpFilters.filterWebSocketMessage(org.microproxy.http.WebSocketMessage, boolean)).resolveWith(Function<String, InetSocketAddress> resolver) Chooses the server address forhost:port;nullresolves as usual.retryBackoff(RetryBackoff backoff) The backoff between these requests' attempts at the routes theChainedProxyManageroffered, instead of the server's (seeHttpFilters.chainedProxyRetryBackoff());RetryBackoff.none()retries at once.tlsHandshakeTimeout(Duration timeout) The upstream TLS handshake timeout for these requests' new connections, instead of the server's (seeHttpFilters.proxyToServerTlsHandshakeTimeout()).tlsIdentity(Function<String, TlsServerIdentity> identity) The TLS identity (server name and verification host) of these requests' connections to servers, by the server'shost:port; seeHttpFilters.proxyToServerTlsIdentity(java.lang.String).
-
Method Details
-
onRequest
The request head as the client sent it (clientToProxyRequest); a wholeFullHttpRequestwhen buffered (bufferRequests(int)). Return a response to answer without contacting the server, ornullto continue.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
onRequestBody
Each piece of a streamed request body, ending with aLastHttpContent.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
beforeSending
The request head just before it goes to the server (proxyToServerRequest), with the proxy's own header changes applied. Return a response to answer instead, ornull.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
onResponse
The response head from the server (serverToProxyResponse); a wholeFullHttpResponsewhen buffered (bufferResponses(int)). Return it (changed or not), a replacement, ornullto abort the exchange.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
onResponseBody
Each piece of a streamed response body: return it, a replacement, ornullto abort.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
beforeResponding
The response head just before it goes to the client (proxyToClientResponse).- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
onFailure
The proxy's own answer when the request fails (proxyToServerFailure): return a response to send instead of the default, ornull.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
onWebSocketFrame
Each WebSocket frame, with whether it came from the client: return it, a replacement, ornullto drop it (seeHttpFilters.filterWebSocketFrame(org.microproxy.http.WebSocketFrame, boolean)).- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
onWebSocketMessage
public HttpFiltersBuilder onWebSocketMessage(BiFunction<WebSocketMessage, Boolean, WebSocketMessage> hook) Each whole WebSocket message, joined from its frames (and inflated), with whether it came from the client: return it, a replacement, ornullto drop it (seeHttpFilters.filterWebSocketMessage(org.microproxy.http.WebSocketMessage, boolean)). WithonWebSocketFrameas well, frames go through that hook first.- Parameters:
hook- the callback for this filter stage- Returns:
- this builder
-
resolveWith
Chooses the server address forhost:port;nullresolves as usual.- Parameters:
resolver- the function selecting a destination address- Returns:
- this builder
-
allowMitm
Whether CONNECT requests may be intercepted (default true when MITM is configured).- Parameters:
allow- the supplier deciding whether interception is allowed- Returns:
- this builder
-
allowMitmFor
Whether a TLS connection may be intercepted, decided from the client'sClientHello(its server name and ALPN protocols); seeHttpFilters.proxyToServerAllowMitm(ClientHello). Default: yes.- Parameters:
allow- the predicate deciding from the ClientHello whether interception is allowed- Returns:
- this builder
-
tlsIdentity
The TLS identity (server name and verification host) of these requests' connections to servers, by the server'shost:port; seeHttpFilters.proxyToServerTlsIdentity(java.lang.String). Return null for the default.- Parameters:
identity- the function choosing the identity for a server'shost:port- Returns:
- this builder
-
connectTimeout
The connect timeout for these requests' new connections, instead of the server's (seeHttpFilters.proxyToServerConnectTimeout()).- Parameters:
timeout- the outbound connection timeout override- Returns:
- this builder
-
tlsHandshakeTimeout
The upstream TLS handshake timeout for these requests' new connections, instead of the server's (seeHttpFilters.proxyToServerTlsHandshakeTimeout()).- Parameters:
timeout- the TLS handshake timeout override, positive- Returns:
- this builder
-
retryBackoff
The backoff between these requests' attempts at the routes theChainedProxyManageroffered, instead of the server's (seeHttpFilters.chainedProxyRetryBackoff());RetryBackoff.none()retries at once.- Parameters:
backoff- the retry backoff override- Returns:
- this builder
-
mitmServerHandshake
When the proxy runs its TLS handshake with the server for these requests' interceptedCONNECTs, instead of the protocol policy's choice (seeHttpFilters.proxyToServerMitmHandshake()).- Parameters:
handshake- when the server's handshake runs- Returns:
- this builder
-
bufferRequests
Buffer request bodies up tomaxBytes, soonRequestgets the whole request.- Parameters:
maxBytes- the maximum number of body bytes- Returns:
- this builder
-
bufferResponses
Buffer response bodies up tomaxBytes, soonResponsegets the whole response.- Parameters:
maxBytes- the maximum number of body bytes- Returns:
- this builder
-
log
Logs each exchange withlogger, around these hooks: requests as the client sent them (beforeonRequest) and as forwarded (afterbeforeSending), responses as the server sent them (beforeonResponse) and as delivered (afterbeforeResponding). Bodies are only parsed when the logger's level needs them.The logger keeps state per exchange. The proxy gives each exchange its own copy of the built filters for that state, whether they are the source (
withFiltersSource(built),plusFiltersSource(built), a member of anHttpFiltersChain) or are returned from another source, such aswithFiltersSource((request, ctx) -> built). Wrapped in filters of your own that delegate to them, they run their hooks but log nothing (with one warning).- Parameters:
logger- the exchange logger- Returns:
- this builder
-
build
Creates a filter source combining the configured hooks.- Returns:
- the combined per-request filter source
-