Skip to content

[SPR-13876] - fix ServletServerHttpRequest URI encoding issue, provid… #955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ public HttpMethod getMethod() {
@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ public void getMethod() throws Exception {

@Test
public void getURI() throws Exception {
URI uri = new URI("http://example.com/path?query");
mockRequest.setServerName(uri.getHost());
mockRequest.setServerPort(uri.getPort());
mockRequest.setRequestURI(uri.getPath());
mockRequest.setQueryString(uri.getQuery());
assertEquals("Invalid uri", uri, request.getURI());
}
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());
// 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());
}

@Test
public void getHeaders() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public void resolveArgumentRequestEntity() throws Exception {
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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

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;
Expand All @@ -31,6 +30,7 @@
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;
Expand Down Expand Up @@ -114,7 +114,8 @@ private void testJsonpTransport(String callbackValue, boolean expectSuccess) thr
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);
}

Expand Down