> Docs > Http Server > Tunnel
HttpServer can support the CONNECT method for establishing tunnels, which is most often used for HTTPS tunneling.
The CONNECT method is not enabled by default; the application needs to add it to supportedMethods
first
server.conf().supportedMethods("CONNECT", "GET", "HEAD" ... );
In the HttpHandler, the application needs to examine each CONNECT request and decide whether to allow the tunneling, based on criteria like client IP, target host:port. The target host:port is specified by request.host()
.
To grant the tunneling request, return a response with a 2xx
status code; the server will then establish a tunnel between the client and the target. To deny the tunneling request, return a response with a status code that's not 2xx
.
HttpHandler handler = request-> { if(request.method().equals("CONNECT")) // a tunneling request { if(isAllowed(request)) return HttpResponse.text(200, "tunneling request granted"); else return HttpResponse.text(403, "tunneling request denied"); } ... }; HttpServer server = new HttpServer(handler);
See also _HttpProxy.java for a simple HTTP+HTTPS proxy demo.