Browse Source

Avoid double encoding URI in ServletServerHttpRequest

Issue: SPR-13876
pull/927/merge
Sammy Chu 9 years ago committed by Rossen Stoyanchev
parent
commit
5185953f29
  1. 9
      spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java
  2. 9
      spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java
  3. 3
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java
  4. 5
      spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java

9
spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

@ -88,9 +88,12 @@ public class ServletServerHttpRequest implements ServerHttpRequest { @@ -88,9 +88,12 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
@Override
public URI getURI() {
try {
return new URI(this.servletRequest.getScheme(), null, this.servletRequest.getServerName(),
this.servletRequest.getServerPort(), this.servletRequest.getRequestURI(),
this.servletRequest.getQueryString(), null);
StringBuffer url = this.servletRequest.getRequestURL();
String queryStr = this.servletRequest.getQueryString();
if (StringUtils.hasText(queryStr)) {
url.append('?').append(queryStr);
}
return new URI(url.toString());
}
catch (URISyntaxException ex) {
throw new IllegalStateException("Could not get HttpServletRequest URI: " + ex.getMessage(), ex);

9
spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

@ -56,11 +56,14 @@ public class ServletServerHttpRequestTests { @@ -56,11 +56,14 @@ public class ServletServerHttpRequestTests {
@Test
public void getURI() throws Exception {
URI uri = new URI("http://example.com/path?query");
URI uri = new URI("https://example.com/%E4%B8%AD%E6%96%87?redirect=https%3A%2F%2Fgithub.com%2Fspring-projects%2Fspring-framework");
mockRequest.setScheme(uri.getScheme());
mockRequest.setServerName(uri.getHost());
mockRequest.setServerPort(uri.getPort());
mockRequest.setRequestURI(uri.getPath());
mockRequest.setQueryString(uri.getQuery());
// NOTE: should use getRawPath() instead of getPath() is decoded, while HttpServletRequest.setRequestURI() is encoded
mockRequest.setRequestURI(uri.getRawPath());
// NOTE: should use getRawQuery() instead of getQuery() is decoded, while HttpServletRequest.getQueryString() is encoded
mockRequest.setQueryString(uri.getRawQuery());
assertEquals("Invalid uri", uri, request.getURI());
}

3
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

@ -175,7 +175,8 @@ public class HttpEntityMethodProcessorMockTests { @@ -175,7 +175,8 @@ public class HttpEntityMethodProcessorMockTests {
assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled());
RequestEntity<?> requestEntity = (RequestEntity<?>) result;
assertEquals("Invalid method", HttpMethod.GET, requestEntity.getMethod());
assertEquals("Invalid url", new URI("http", null, "www.example.com", 80, "/path", null, null), requestEntity.getUrl());
// using default port (which is 80), so do not need to append the port (-1 means ignore)
assertEquals("Invalid url", new URI("http", null, "www.example.com", -1, "/path", null, null), requestEntity.getUrl());
assertEquals("Invalid argument", body, requestEntity.getBody());
}

5
spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java

@ -20,7 +20,6 @@ import java.sql.Date; @@ -20,7 +20,6 @@ import java.sql.Date;
import org.junit.Before;
import org.junit.Test;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.WebSocketHandler;
@ -31,6 +30,7 @@ import org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSes @@ -31,6 +30,7 @@ import org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSes
import org.springframework.web.socket.sockjs.transport.session.PollingSockJsSession;
import org.springframework.web.socket.sockjs.transport.session.StreamingSockJsSession;
import org.springframework.web.socket.sockjs.transport.session.StubSockJsServiceConfig;
import org.springframework.web.util.UriUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -114,7 +114,8 @@ public class HttpSendingTransportHandlerTests extends AbstractHttpRequestTests @@ -114,7 +114,8 @@ public class HttpSendingTransportHandlerTests extends AbstractHttpRequestTests
setRequest("POST", "/");
if (callbackValue != null) {
this.servletRequest.setQueryString("c=" + callbackValue);
// need to encode the query parameter
this.servletRequest.setQueryString("c=" + UriUtils.encodeQueryParam(callbackValue, "UTF-8"));
this.servletRequest.addParameter("c", callbackValue);
}

Loading…
Cancel
Save