Class ProtocolPolicy

java.lang.Object
org.microproxy.ProtocolPolicy

public final class ProtocolPolicy extends Object
How the proxy behaves, where it can behave in more than one way, for the exchanges of clients that speak one protocol: HTTP/1.x, HTTP/2 or HTTP/3 (the protocol the client speaks to the proxy, FlowContext.getHttpVersion()). Configured on the bootstrap per protocol (HttpProxyServerBootstrap.onHttp1(java.util.function.Consumer<org.microproxy.Http1PolicyBuilder>), HttpProxyServerBootstrap.onHttp2(java.util.function.Consumer<org.microproxy.ProtocolPolicy.Builder>), HttpProxyServerBootstrap.onHttp3(java.util.function.Consumer<org.microproxy.ProtocolPolicy.Builder>)) and for all of them (HttpProxyServerBootstrap.onAllProtocols(java.util.function.Consumer<org.microproxy.ProtocolPolicy.Builder>)), so that a proxy can, say, keep LittleProxy's order of events for its HTTP/1.1 clients while serving HTTP/2 and HTTP/3 clients natively:

 MicroProxy.bootstrap()
         .onHttp1(p -> p.preset(ProtocolPolicy.littleProxy()))
         .onHttp3(p -> p.tlsRetryWithoutSni(true))
         .start();
 

A policy sets some of its fields and leaves the others to whatever applies below it. For an exchange the proxy takes each field from, in order: the request's own filters, where a hook decides that field for one request; the policy of the client's protocol; the policy for all protocols (onAllProtocols and the bootstrap's own setters, such as HttpProxyServerBootstrap.withTlsRetryWithoutSni(boolean)); and last the protocol's defaults, which are defaults() except for HTTP/1 with HttpProxyServerBootstrap.withLittleProxyCompatibility(), which makes them littleProxy().

Which policy applies: an HTTP/1.x connection's exchanges (its CONNECTs included) take HTTP/1's; an HTTP/2 connection's streams (their CONNECTs and extended CONNECTs included) HTTP/2's; an HTTP/3 connection's streams HTTP/3's. The requests a client sends inside a tunnel the proxy intercepts take the policy of what the client speaks inside it, which may differ from that of the CONNECT that opened it (HTTP/1.1 inside a CONNECT stream of an HTTP/2 connection, say). SOCKS clients and transparent TLS connections, whose CONNECT the proxy makes up, are HTTP/1 connections until what they carry says otherwise (HTTP/2 negotiated inside the intercepted TLS); a reverse proxy's clients take the policy of the protocol they speak to it; transparent QUIC and the QUIC inside a CONNECT-UDP flow are HTTP/3.

Instances are immutable and thread-safe, and equal when they set the same fields alike.