diff --git a/core/src/main/java/com/predic8/membrane/core/http/Header.java b/core/src/main/java/com/predic8/membrane/core/http/Header.java index cb8a432f3..4ab026b22 100644 --- a/core/src/main/java/com/predic8/membrane/core/http/Header.java +++ b/core/src/main/java/com/predic8/membrane/core/http/Header.java @@ -33,7 +33,7 @@ /** * The headers of an HTTP message. */ -public class Header { +public class Header implements Map { private static final Logger log = LoggerFactory.getLogger(Header.class.getName()); @@ -431,10 +431,69 @@ public static long parseKeepAliveHeader(String keepAliveHeaderValue, String para return Long.parseLong(m.group(1)); } + @Override + public int size() { + return fields.size(); + } + + @Override + public boolean isEmpty() { + return fields.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return false; + } + + @Override + public boolean containsValue(Object value) { + return false; + } + + @Override + public String get(Object key) { + return null; + } + + @org.jetbrains.annotations.Nullable + @Override + public String put(String key, String value) { + return null; + } + + @Override + public String remove(Object key) { + return null; + } + + @Override + public void putAll(@org.jetbrains.annotations.NotNull Map m) { + + } + public void clear() { fields.clear(); } + @org.jetbrains.annotations.NotNull + @Override + public Set keySet() { + return null; + } + + @org.jetbrains.annotations.NotNull + @Override + public Collection values() { + return null; + } + + @org.jetbrains.annotations.NotNull + @Override + public Set> entrySet() { + return null; + } + public void setNoCacheResponseHeaders() { setValue(EXPIRES, "Tue, 03 Jul 2001 06:00:00 GMT"); setValue(CACHE_CONTROL, "no-store, no-cache, must-revalidate, max-age=0"); diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/ratelimit/RateLimitInterceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/ratelimit/RateLimitInterceptor.java index 4879c3037..11ded9b65 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/ratelimit/RateLimitInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/ratelimit/RateLimitInterceptor.java @@ -21,6 +21,7 @@ import com.predic8.membrane.core.lang.spel.*; import org.slf4j.*; import org.springframework.expression.*; +import org.springframework.expression.spel.*; import org.springframework.expression.spel.standard.*; import java.time.*; @@ -92,12 +93,19 @@ public RateLimitInterceptor(Duration requestLimitDuration, int requestLimit) { @Override public Outcome handleRequest(Exchange exc) throws Exception { - if (!strategy.isRequestLimitReached(getKey(exc))) - return CONTINUE; + + try { + if (!strategy.isRequestLimitReached(getKey(exc))) + return CONTINUE; + } catch (SpelEvaluationException e) { + log.error("Cannot evaluate keyExpression '{}' cause is {}",keyExpression,e.getMessage()); + exc.setResponse(createProblemDetails(500, "/internal-error", "Internal Server Error")); + return RETURN; + } Map details = new HashMap<>(); details.put("message","The quota of the ratelimiter is exceeded. Try again in %s seconds.".formatted(strategy.getLimitReset(exc.getRemoteAddrIp()))); - exc.setResponse(createProblemDetails(429, "/ratelimiter/exceeded", "Rate Limit is Exceeded", details)); + exc.setResponse(createProblemDetails(429, "/ratelimiter/exceeded", "Rate limit is exceeded", details)); setHeaderRateLimitFieldsOnResponse(exc); return RETURN; diff --git a/core/src/test/java/com/predic8/membrane/core/lang/spel/ExchangeEvaluationContextTest.java b/core/src/test/java/com/predic8/membrane/core/lang/spel/ExchangeEvaluationContextTest.java new file mode 100644 index 000000000..a064b5057 --- /dev/null +++ b/core/src/test/java/com/predic8/membrane/core/lang/spel/ExchangeEvaluationContextTest.java @@ -0,0 +1,32 @@ +package com.predic8.membrane.core.lang.spel; + +import com.predic8.membrane.core.exchange.*; +import com.predic8.membrane.core.http.*; +import org.apache.http.client.methods.*; +import org.junit.jupiter.api.*; +import org.springframework.expression.*; +import org.springframework.expression.spel.standard.*; + +import java.net.*; + +import static org.junit.jupiter.api.Assertions.*; + +class ExchangeEvaluationContextTest { + + Exchange exc; + + @BeforeEach + void setup() throws URISyntaxException { + exc = new Request.Builder().header("Authentication","foo").buildExchange(); + + + } + + @Test + void getMethod() { + + Expression expression = new SpelExpressionParser().parseExpression("headers.Authentication"); + + expression.getValue(new ExchangeEvaluationContext(exc, exc.getRequest()).getStandardEvaluationContext(), String.class); + } +} \ No newline at end of file diff --git a/distribution/examples/rateLimiter/proxies.xml b/distribution/examples/rateLimiter/proxies.xml index 16d443a23..31d9d220c 100644 --- a/distribution/examples/rateLimiter/proxies.xml +++ b/distribution/examples/rateLimiter/proxies.xml @@ -6,10 +6,10 @@ - + - +