Changing headers on proxy response
by Jeff Williams
Hi,
I am currently using a customer handler wrapping the proxy handler. I want
to set some response headers from my handler, but if they exist in the
response from the proxy's backend, they are overridden. Example code:
Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(new MyHandler(new ProxyHandler(proxy, 30000,
ResponseCodeHandler.HANDLE_404))).build();
class MyHandler extends HttpHandler {
private static final HttpString ACCESS_CONTROL_EXPOSE_HEADERS = new
HttpString("Access-Control-Expose-Headers");
private HTTPHandler next;
public MyHandler(HttpHandler next) {
this.next = next;
}
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(ACCESS_CONTROL_EXPOSE_HEADERS, "new
ACEH");
}
}
The problem here is that the backend server responds with an
Access-Control-Expose-Headers header and that replaces the one I set in
MyHandler.
Is there any way I can change headers after proxy has received the response
from backend and before it starts sending the response to the client?
Regards,
Jeff
10 years, 1 month
ResponseRateLimitingHandler questions
by ralf_boogie_blues@bluewin.ch
Hi Stuart
Thanks a lot for the ResponseRateLimitingHandler. I played a little bit with this handler and do have some questions.
I added some trace logs in order to understand the logic. Of course, I didn't understand everything;-) May you will correct me.
First, I tested the download response with a 1 MBytes file. The response limit was set to 100000 Bytes/s. This should download the whole file in about 10s but the result was 18s.
Then, I started to dig into the handler.
How I cReated the handler.
final ResponseRateLimitingHandler responseLimiter = new ResponseRateLimitingHandler(proxy, 100000, 1, TimeUnit.SECONDS);
The trace logs showed me this (with the settings above):
the handler writes chunks as big as the buffer is defined, I think on my machine 16KBytes.
it repeats this until the bytes written are greater than the rate limit.
then, the handler calculates the time to sleep until the next chunks can be written.
Problem 1: The handler writes always more than the specified limit. Because of this, the handler calculates n * units for the time to sleep. So, in my test the handler slept always 2s instead of 1s.
Problem 2: The size of the chunks is probably too static. I think the chunks should be size relatively to the rate limit.
I changed the handler slightly in this way:
I introduced along to the existing parameters also a chunkSize.
canSend(): if (byteCount < (bytes - chunks)) { return true; } // it doesn't write anymore than allowed
handleWritten(long written): nextSendTime = startTime + time; // instead of: nextSendTime = startTime + (units * time);
writeFinal and transferFrom writes not more than bytes <= chunkSize.
This changes gave me the expected result of about 10s download time.
This is quite a long mail :-) It could be still a mistake I did. Let me know what you think.
Regards, Ralf
10 years, 1 month