Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class HttpProxy
- Object
- 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 unlesssetForwardedHeadersturns them off. - Destinations are fixed. Only the target’s own host is reached, plus
hosts named with
allowHost; a redirect elsewhere is passed back to the client, never followed. Onlyhttpandhttps. - Redirects are passed through by default.
setFollowRedirectsfollows 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.
Constructors
public HttpProxy(String target)
throws IOException |
|
Methods
public HttpProxy setClient(WebClient client) | The client upstream requests go through, for its timeouts and client certificate; a default one when unset. |
public HttpProxy allowHost(String host) | Lets requests – redirects and forward(HttpServer.Request, String) with an absolute URL – reach host (a name, or name:port) as well as the target’s own. |
public HttpProxy 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. |
public HttpProxy setForwardedHeaders(boolean forwardedHeaders) | Whether to add X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host; on by default. |
public HttpProxy renameRequestHeader(String from, String to) | Sends the client’s from header upstream as to, as Spring Cloud Gateway’s MapRequestHeader filter does – X-Api-Key to Authorization, say. |
public HttpProxy renameResponseHeader(String from, String to) | Answers the client with the upstream’s from header under the name to. |
public HttpProxy rewriteStatus(int from, int to) | Answers the client with to whenever the upstream answers from, as Spring Cloud Gateway’s SetStatus filter does: an upstream that answers 401 to a missing key the client should see as 403, say. |
public HttpServer.Response forward(HttpServer.Request request) | Forwards to the target with the request’s own path and query appended. |
public HttpServer.Response forward(HttpServer.Request request, String pathOrUrl) | Forwards to pathOrUrl: a path (with any query) appended to the target, or an absolute URL, which must name an allowed host. |
Inherited methods
Constructor details
HttpProxy
public HttpProxy(String target)
throws IOExceptiontarget: the upstream’s origin and optional base path, such ashttps://api.internal:8443orhttp://10.0.0.5/service
Throws
Method details
setClient
public HttpProxy setClient(WebClient client)The client upstream requests go through, for its timeouts and client
certificate; a default one when unset.
allowHost
public HttpProxy allowHost(String host)Lets requests – redirects and
forward(HttpServer.Request, String)
with an absolute URL – reach host (a name, or name:port) as well as
the target’s own.setFollowRedirects
public HttpProxy 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
public HttpProxy setForwardedHeaders(boolean forwardedHeaders)Whether to add
X-Forwarded-For, X-Forwarded-Proto and
X-Forwarded-Host; on by default.renameRequestHeader
public HttpProxy renameRequestHeader(String from, String to)Sends the client’s
from header upstream as to, as Spring Cloud
Gateway’s MapRequestHeader filter does – X-Api-Key to
Authorization, say. An upstream header already called to is
replaced. Hop-by-hop and framing headers cannot be renamed.Parameters
fromString- the header the client sends; case does not matter
toString- the name it is forwarded under
Returns
this proxy
renameResponseHeader
public HttpProxy renameResponseHeader(String from, String to)Answers the client with the upstream’s
from header under the name
to.Parameters
fromString- the header the upstream sends; case does not matter
toString- the name the client receives it under
Returns
this proxy
rewriteStatus
public HttpProxy rewriteStatus(int from, int to)Answers the client with
to whenever the upstream answers from, as
Spring Cloud Gateway’s SetStatus filter does: an upstream that
answers 401 to a missing key the client should see as 403, say. The
body and headers are passed on as they came.Parameters
fromint- the upstream status, 100 to 599
toint- the status the client gets, 100 to 599
Returns
this proxy
forward
public HttpServer.Response forward(HttpServer.Request request)Forwards to the target with the request’s own path and query appended.
forward
public HttpServer.Response forward(HttpServer.Request request, String pathOrUrl)Forwards to
pathOrUrl: a path (with any query) appended to the target,
or an absolute URL, which must name an allowed host.