Search code examples
vert.xvertx-httpclient

How to capture HttpProxy responses


I have a reverse proxy server using Vert.x Web Proxy, implemented with pretty standard code:

  ...

  HttpClient client = theVertx.createHttpClient();
  HttpProxy proxy = HttpProxy.reverseProxy(client);
  
  Route anyroute = theRouter.route("/*");
  anyroute.handler(ProxyHandler.create(proxy));     
  anyroute.failureHandler(hnd->{
     System.out.println("Failure. Return code: "+hnd.response().getStatusCode());
  });

  ...

This code works well enough, but I would like to be able to capture return codes in order to do some custom actions (like displaying custom web pages when 400 failures occur). I am seeing that the code in the failureHandler() method is not being called when I cause a 404 error to occur. Apparently, what happens is that if I make a request for a resource that doe not exist on the target server, the proxy actually returns the 404 error sent by the target server back to the browser!

I would like to capture that 404 error when the proxy receives it and do something with it. Unfortunately, it is unclear from the web proxy documentation (or any of the rather few code examples I have seen on the web) that the web proxy is even capable of doing this.

Is it possible to write a handler that can capture the response that comes from an HttpProxy? If it is, how would I do this?


Solution

  • The answer to this question is simple: do not use HttpProxy if you want to capture responses.

    After looking through the Vertx Web Proxy source code, it became clear to me that the HttpProxy has no capability for capturing responses. It became necessary to implement a web proxy that did so.

    I assimilated the bulk of the Vertx Web Proxy code into my application, and created a proxy library that not only captures responses but captures requests as well. The new web proxy, called F3WebProxy, has lambdas that allow for coding custom actions when a proxy begins processing a request, and coding custom actions when the response is received. The interface is very simple and easy to use.