Class HttpProxy
java.lang.Object
com.codename1.backend.HttpProxy
Forwards a request to another HTTP server and streams its answer back: a reverse proxy, an API gateway route, a pass-through to an internal service.
HttpProxy images = new HttpProxy("http://images.internal:8080");
// in a handler:
return images.forward(request); // same path and query upstream
return images.forward(request, "/v2/thumbs"); // or a path of your choosing
What it guarantees:
- Bounded memory. A binary request body is streamed up from wherever it
is -- a spooled upload included -- and the response body is streamed down
as it arrives (
Web.Exchange). A text or compressed request body is the server's in-memory one (under its 8 MB limit), forwarded decoded. - Headers survive, hop-by-hop ones do not. Every end-to-end header goes
through in both directions, a repeated one (
Set-Cookie) line by line. The hop-by-hop set of RFC 9110 7.6.1 --Connection,Keep-Alive,Proxy-*,TE,Trailer,Transfer-Encoding,Upgrade-- and every header aConnectionfield names are removed each way.HostandContent-Lengthare the transport's.X-Forwarded-For,-Protoand-Hostare added unlesssetForwardedHeaders(boolean)turns them off. - Destinations are fixed. Only the target's own host is reached, plus
hosts named with
allowHost(String); a redirect elsewhere is passed back to the client, never followed. Onlyhttpandhttps. - Redirects are passed through by default.
setFollowRedirects(int)follows up to that many hops itself, for a request with no body only -- a streamed body cannot be sent twice -- and only to allowed hosts. A hop to another host drops the client'sAuthorizationandCookie. - Cancellation goes both ways. A client that disconnects mid-download
makes the next write fail, which closes the upstream exchange at once. An
upstream that fails before answering is a
502; one that fails after the status and headers went out cannot be turned into an error response any more, so the client's response is aborted -- the HTTP/1.1 connection is closed without its final chunk, the HTTP/2 stream is reset -- and the client sees a truncated response, never a complete-looking one.
The response is re-framed by this server: chunked on HTTP/1.1, DATA frames on HTTP/2, whatever length the upstream declared. One instance serves any number of concurrent requests once configured.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionLets requests -- redirects andforward(HttpServer.Request, String)with an absolute URL -- reachhost(a name, orname:port) as well as the target's own.forward(HttpServer.Request request) Forwards to the target with the request's own path and query appended.forward(HttpServer.Request request, String pathOrUrl) Forwards topathOrUrl: a path (with any query) appended to the target, or an absolute URL, which must name an allowed host.The client upstream requests go through, for its timeouts and client certificate; a default one when unset.setFollowRedirects(int hops) How many redirects to follow on the client's behalf; 0, the default, hands every 3xx back to the client as it came.setForwardedHeaders(boolean forwardedHeaders) Whether to addX-Forwarded-For,X-Forwarded-ProtoandX-Forwarded-Host; on by default.
-
Constructor Details
-
HttpProxy
target: the upstream's origin and optional base path, such ashttps://api.internal:8443orhttp://10.0.0.5/service
- Throws:
IOException
-
-
Method Details
-
setClient
-
allowHost
Lets requests -- redirects andforward(HttpServer.Request, String)with an absolute URL -- reachhost(a name, orname:port) as well as the target's own. -
setFollowRedirects
How many redirects to follow on the client's behalf; 0, the default, hands every 3xx back to the client as it came. -
setForwardedHeaders
Whether to addX-Forwarded-For,X-Forwarded-ProtoandX-Forwarded-Host; on by default. -
forward
Forwards to the target with the request's own path and query appended. -
forward
Forwards topathOrUrl: a path (with any query) appended to the target, or an absolute URL, which must name an allowed host.
-