From 5bee4e6b4c1ea0ddc5cb35b7c04437600754f312 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Fri, 15 Oct 2021 18:07:36 +0800 Subject: [PATCH 1/2] Query params and cookies of FizzServerHttpRequestDecorator can be modified --- .../ext/FizzServerHttpRequestDecorator.java | 83 ++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpRequestDecorator.java b/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpRequestDecorator.java index 3ccba00..42ea1d7 100644 --- a/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpRequestDecorator.java +++ b/fizz-common/src/main/java/we/spring/http/server/reactive/ext/FizzServerHttpRequestDecorator.java @@ -17,16 +17,28 @@ package we.spring.http.server.reactive.ext; +import io.netty.handler.codec.http.cookie.Cookie; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.NettyDataBuffer; import org.springframework.core.io.buffer.PooledDataBuffer; +import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.AbstractServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequestDecorator; +import org.springframework.lang.Nullable; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; +import reactor.netty.http.server.HttpServerRequest; import we.util.NettyDataBufferUtils; +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author hongqiaowei @@ -34,20 +46,87 @@ import java.nio.charset.StandardCharsets; public class FizzServerHttpRequestDecorator extends ServerHttpRequestDecorator { - private HttpHeaders headers; + private static final Pattern QUERY_PATTERN = Pattern.compile("([^&=]+)(=?)([^&]+)?"); + + private AbstractServerHttpRequest delegate; + + private HttpServerRequest nativeRequest; + + @Nullable + private MultiValueMap queryParams; + + private HttpHeaders headers; + + @Nullable + private MultiValueMap cookies; private Flux body = Flux.empty(); public FizzServerHttpRequestDecorator(ServerHttpRequest delegate) { super(delegate); - headers = HttpHeaders.writableHttpHeaders(delegate.getHeaders()); + this.delegate = (AbstractServerHttpRequest) delegate; + nativeRequest = this.delegate.getNativeRequest(); + } + + @Override + public MultiValueMap getQueryParams() { + if (queryParams == null) { + queryParams = initQueryParams(); + } + return queryParams; + } + + private MultiValueMap initQueryParams() { + MultiValueMap queryParams = new LinkedMultiValueMap<>(); + String query = getURI().getRawQuery(); + if (query != null) { + Matcher matcher = QUERY_PATTERN.matcher(query); + while (matcher.find()) { + String name = decodeQueryParam(matcher.group(1)); + String eq = matcher.group(2); + String value = matcher.group(3); + value = (value != null ? decodeQueryParam(value) : (StringUtils.hasLength(eq) ? "" : null)); + queryParams.add(name, value); + } + } + return queryParams; + } + + private String decodeQueryParam(String value) { + try { + return URLDecoder.decode(value, "UTF-8"); + } catch (UnsupportedEncodingException e) { + return URLDecoder.decode(value); + } } @Override public HttpHeaders getHeaders() { + if (headers == null) { + headers = HttpHeaders.writableHttpHeaders(delegate.getHeaders()); + } return headers; } + @Override + public MultiValueMap getCookies() { + if (cookies == null) { + cookies = initCookies(); + } + return cookies; + } + + private MultiValueMap initCookies() { + MultiValueMap cookies = new LinkedMultiValueMap<>(); + for (CharSequence name : nativeRequest.cookies().keySet()) { + for (Cookie cookie : nativeRequest.cookies().get(name)) { + HttpCookie httpCookie = new HttpCookie(name.toString(), cookie.value()); + cookies.add(name.toString(), httpCookie); + } + } + return cookies; + } + public void setEmptyBody() { this.body = Flux.empty(); } From eb793b30923d649431f0aad5cf3203c743eb5976 Mon Sep 17 00:00:00 2001 From: hongqiaowei Date: Fri, 15 Oct 2021 18:14:41 +0800 Subject: [PATCH 2/2] Release 2.3.3-beta3 --- fizz-bootstrap/pom.xml | 2 +- fizz-common/pom.xml | 2 +- fizz-core/pom.xml | 2 +- fizz-plugin/pom.xml | 2 +- fizz-spring-boot-starter/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/fizz-bootstrap/pom.xml b/fizz-bootstrap/pom.xml index b29836d..513ea6d 100644 --- a/fizz-bootstrap/pom.xml +++ b/fizz-bootstrap/pom.xml @@ -12,7 +12,7 @@ com.fizzgate fizz-bootstrap - 2.3.3-beta2 + 2.3.3-beta3 1.8 diff --git a/fizz-common/pom.xml b/fizz-common/pom.xml index 3faf8d8..be2a091 100644 --- a/fizz-common/pom.xml +++ b/fizz-common/pom.xml @@ -5,7 +5,7 @@ fizz-gateway-community com.fizzgate - 2.3.3-beta2 + 2.3.3-beta3 ../pom.xml 4.0.0 diff --git a/fizz-core/pom.xml b/fizz-core/pom.xml index 9523a14..9c45170 100644 --- a/fizz-core/pom.xml +++ b/fizz-core/pom.xml @@ -5,7 +5,7 @@ fizz-gateway-community com.fizzgate - 2.3.3-beta2 + 2.3.3-beta3 ../pom.xml 4.0.0 diff --git a/fizz-plugin/pom.xml b/fizz-plugin/pom.xml index ca6430e..752e244 100644 --- a/fizz-plugin/pom.xml +++ b/fizz-plugin/pom.xml @@ -5,7 +5,7 @@ fizz-gateway-community com.fizzgate - 2.3.3-beta2 + 2.3.3-beta3 ../pom.xml 4.0.0 diff --git a/fizz-spring-boot-starter/pom.xml b/fizz-spring-boot-starter/pom.xml index 3710ace..e390d5c 100644 --- a/fizz-spring-boot-starter/pom.xml +++ b/fizz-spring-boot-starter/pom.xml @@ -5,7 +5,7 @@ fizz-gateway-community com.fizzgate - 2.3.3-beta2 + 2.3.3-beta3 ../pom.xml 4.0.0 diff --git a/pom.xml b/pom.xml index 2da1398..7dc823a 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ fizz-gateway-community ${project.artifactId} fizz gateway community - 2.3.3-beta2 + 2.3.3-beta3 pom fizz-common