Skip to content

Commit

Permalink
Don't want to send x-rate-limit response headers #86
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGiffing committed Dec 19, 2021
1 parent 4ab6089 commit af765c4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public class Bucket4JConfiguration {
*/
private String httpResponseBody = "{ \"message\": \"Too many requests!\" }";

/**
* Hides the HTTP response headers
* x-rate-limit-remaining
* x-rate-limit-retry-after-seconds
*
* It does not effect custom defined httpResponseHeaders.
*/
private Boolean hideHttpResponseHeaders = Boolean.FALSE;

private Map<String, String> httpResponseHeaders = new HashMap<>();

public Bucket4JConfiguration() {
Expand Down Expand Up @@ -124,4 +133,12 @@ public void setMetrics(Metrics metrics) {
this.metrics = metrics;
}

public Boolean getHideHttpResponseHeaders() {
return hideHttpResponseHeaders;
}

public void setHideHttpResponseHeaders(Boolean hideHttpResponseHeaders) {
this.hideHttpResponseHeaders = hideHttpResponseHeaders;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public class FilterConfiguration<R> {
*/
private int order;

/**
* Hides the HTTP response headers
* x-rate-limit-remaining
* x-rate-limit-retry-after-seconds
*/
private Boolean hideHttpResponseHeaders = Boolean.FALSE;

/**
* The HTTP response body which should be returned when limiting the rate.
*/
Expand Down Expand Up @@ -92,4 +99,12 @@ public void setMetrics(Metrics metrics) {
this.metrics = metrics;
}

public Boolean getHideHttpResponseHeaders() {
return hideHttpResponseHeaders;
}

public void setHideHttpResponseHeaders(Boolean hideHttpResponseHeaders) {
this.hideHttpResponseHeaders = hideHttpResponseHeaders;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public FilterConfiguration<R> buildFilterConfig(Bucket4JConfiguration config,
filterConfig.setOrder(config.getFilterOrder());
filterConfig.setStrategy(config.getStrategy());
filterConfig.setHttpResponseBody(config.getHttpResponseBody());
filterConfig.setHideHttpResponseHeaders(config.getHideHttpResponseHeaders());
filterConfig.setHttpResponseHeaders(config.getHttpResponseHeaders());
filterConfig.setMetrics(config.getMetrics());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return Mono.error(new ReactiveRateLimitException(filterConfig.getHttpResponseBody()));
}
if(remainingLimit != null) {
response.getHeaders().set("X-Rate-Limit-Remaining", "" + remainingLimit);
if(!filterConfig.getHideHttpResponseHeaders()) {
response.getHeaders().set("X-Rate-Limit-Remaining", "" + remainingLimit);
}
}
return chain.filter(exchange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return Mono.error(new ReactiveRateLimitException(filterConfig.getHttpResponseBody()));
}
if(remainingLimit != null) {
response.getHeaders().set("X-Rate-Limit-Remaining", "" + remainingLimit);
if(!filterConfig.getHideHttpResponseHeaders()) {
response.getHeaders().set("X-Rate-Limit-Remaining", "" + remainingLimit);
}
}
return chain.filter(exchange);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
};
if(allConsumed) {
if(remainingLimit != null) {
httpResponse.setHeader("X-Rate-Limit-Remaining", "" + remainingLimit);
if(!filterConfig.getHideHttpResponseHeaders()) {
httpResponse.setHeader("X-Rate-Limit-Remaining", "" + remainingLimit);
}
}
filterChain.doFilter(httpRequest, httpResponse);
}
Expand All @@ -69,7 +71,9 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

private void handleHttpResponseOnRateLimiting(HttpServletResponse httpResponse, ConsumptionProbe probe) throws IOException {
httpResponse.setStatus(429);
httpResponse.setHeader("X-Rate-Limit-Retry-After-Seconds", "" + TimeUnit.NANOSECONDS.toSeconds(probe.getNanosToWaitForRefill()));
if(!filterConfig.getHideHttpResponseHeaders()) {
httpResponse.setHeader("X-Rate-Limit-Retry-After-Seconds", "" + TimeUnit.NANOSECONDS.toSeconds(probe.getNanosToWaitForRefill()));
}
httpResponse.setContentType("application/json");
filterConfig.getHttpResponseHeaders().forEach(httpResponse::setHeader);
httpResponse.getWriter().append(filterConfig.getHttpResponseBody());
Expand Down

0 comments on commit af765c4

Please sign in to comment.