Rewrite of response body

Hi all
I am hoping someone can help me, is it possible to rewrite URLs in a response body ( not response header). I have a requirement that makes an api request through the HAProxy (as an api gateway) that returns a list of URLs( amongst other items) in the response body that are used to make further api requests to backend, however I want all requests to use the HAProxy api gateway and to mask urls from users, any ideas on how to rewrite items in a response body.

Hopefully someone can give me a clue how to achieve this
Regards

Ian

1 Like

To my knowledge HAProxy doesn’t support rewrites of bodies (neither for requests or responses).

I could perhaps look at the SPOE feature of HAProxy which could be abused to perhaps implement this. (I’m not sure, but it’s a good place to look.)

Alternatively you could use Lua-based services that do the request and mangle the response, however I would suspect this will have noticeable performance impact…

The best choice though, is to just return proper response body…

thanks for the response, I will look into your suggestions. Unfortunately I don’t have the power to change the response body.

Personally – if I really needed to mangle the response body – I would just write a HTTP proxy server in Go (or perhaps even in NodeJS), and just “stick” it between the client and the actual server with support of HAProxy like so:

[client] -> HAProxy -> [mangler] -> HAProxy -> [server]

I usually do so with this with Varnish. The reason to send requests from the “mangler” through HAProxy again is mainly for control and logging, and the fact that the “mangler” doesn’t now require to know the backend servers.

I need to do the same thing, as our webservers respond with an inappropriate JSON, which requires a fix. Unfortunately, we can’t get it right on the webservers.

The response body is quite small (e.g. 200-300 Bytes), and the URI is rarely requested.

We are using haproxy 2.1. with LUA support enabled.
I’ve read about SPOE and LUA, but have not used these yet.
At the same time, the official documentation states “HAProxy is not: a data scrubber : it will not modify the body of requests nor responses.”

Has anyone used HAProxy for a similar purpose yet? Or is it just inappropriate for this task?

Thanks
Toni

You can do probably do it with LUA, and certainly with SPOE, but I’m not aware of any ready-to-use solutions yet. You’d have to put in the work.

when lua or spoe while be abel to do rewrite response body ?
in lua while be usefull to abel to do that in event manear (stream loop) !

in that way you can modify the response body on the fly without reading complete body from backend before modifing the response…

like filter in nginx and apache httpd.

on nginx with openresty the call lua script at each readed block:

on apache they can modify on the fly with apache bucket brigade (in output but also in input):

some commercial reverse proxy offer this type of modification, like F5 Big IP or Pulse vADC…

On the pulse secure vadc (formely zeus reverse proxy) you can do like that:

$status = http.getResponseCode();
if( $status != 200 ) break;
$type = http.getResponseHeader("Content-Type");
if( !string.startsWith( $type, "text/") ) break;
http.stream.startResponse( $rcode, $type );
while( $line = http.stream.readResponse( 2048, "\n" ) ) {
$line = string.replaceAll( $line, "TrafficScript", "TRAFFICSCRIPT" );
http.stream.writeResponse( $line );
}
http.stream.finishResponse();