Skip to content

Commit f4de861

Browse files
committed
Fix encoding issue in ServerHttpRequest.mutate()
Issue: SPR-16434
1 parent 637e09f commit f4de861

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/DefaultServerHttpRequestBuilder.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.net.InetSocketAddress;
2020
import java.net.URI;
21-
import java.net.URISyntaxException;
2221
import java.util.LinkedList;
2322
import java.util.List;
2423
import java.util.Map;
@@ -34,6 +33,7 @@
3433
import org.springframework.util.Assert;
3534
import org.springframework.util.LinkedMultiValueMap;
3635
import org.springframework.util.MultiValueMap;
36+
import org.springframework.web.util.UriComponentsBuilder;
3737

3838
/**
3939
* Package-private default implementation of {@link ServerHttpRequest.Builder}.
@@ -139,13 +139,7 @@ private URI getUriToUse() {
139139
if (this.uriPath == null) {
140140
return this.uri;
141141
}
142-
try {
143-
return new URI(this.uri.getScheme(), this.uri.getUserInfo(), uri.getHost(), uri.getPort(),
144-
uriPath, uri.getQuery(), uri.getFragment());
145-
}
146-
catch (URISyntaxException ex) {
147-
throw new IllegalStateException("Invalid URI path: \"" + this.uriPath + "\"");
148-
}
142+
return UriComponentsBuilder.fromUri(this.uri).replacePath(this.uriPath).build(true).toUri();
149143
}
150144

151145
private static class DefaultServerHttpRequest extends AbstractServerHttpRequest {

spring-web/src/test/java/org/springframework/http/server/reactive/ServerHttpRequestTests.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@
3333
import org.springframework.mock.web.test.MockHttpServletResponse;
3434
import org.springframework.util.MultiValueMap;
3535

36-
import static org.junit.Assert.assertEquals;
36+
import static org.junit.Assert.*;
3737

3838
/**
3939
* Unit tests for {@link AbstractServerHttpRequest}.
@@ -85,26 +85,33 @@ public void queryParamsWithNoValue() throws Exception {
8585
assertEquals(Collections.singletonList(null), params.get("a"));
8686
}
8787

88+
@Test // SPR-16434
89+
public void mutatePathWithEncodedQueryParams() throws Exception {
90+
ServerHttpRequest request = createHttpRequest("/path?name=%E6%89%8E%E6%A0%B9")
91+
.mutate().path("/mutatedPath").build();
92+
assertEquals("/mutatedPath", request.getURI().getRawPath());
93+
assertEquals("name=%E6%89%8E%E6%A0%B9", request.getURI().getRawQuery());
94+
}
95+
96+
8897
private ServerHttpRequest createHttpRequest(String path) throws Exception {
89-
HttpServletRequest request = new MockHttpServletRequest("GET", path) {
90-
@Override
91-
public ServletInputStream getInputStream() {
92-
return new TestServletInputStream();
93-
}
94-
};
98+
HttpServletRequest request = createEmptyBodyHttpServletRequest(path);
9599
AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
96100
return new ServletServerHttpRequest(request, asyncContext, "", new DefaultDataBufferFactory(), 1024);
97101
}
98102

99-
private static class TestServletInputStream extends DelegatingServletInputStream {
100-
101-
public TestServletInputStream() {
102-
super(new ByteArrayInputStream(new byte[0]));
103-
}
104-
105-
@Override
106-
public void setReadListener(ReadListener readListener) {
107-
// Ignore
108-
}
103+
private HttpServletRequest createEmptyBodyHttpServletRequest(String path) {
104+
return new MockHttpServletRequest("GET", path) {
105+
@Override
106+
public ServletInputStream getInputStream() {
107+
return new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])) {
108+
@Override
109+
public void setReadListener(ReadListener readListener) {
110+
// Ignore
111+
}
112+
};
113+
}
114+
};
109115
}
116+
110117
}

0 commit comments

Comments
 (0)