From ab16adab2e3cd7505cfb68353917bdc42f4ba0d1 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 20 Jan 2016 17:57:56 -0500 Subject: [PATCH] Avoid double encoding URI in ServletServerHttpRequest Issue: SPR-13876 --- .../http/server/ServletServerHttpRequest.java | 11 +++++++---- .../server/ServletServerHttpRequestTests.java | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java index 2b5ac8a93e8..a906da520d0 100644 --- a/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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 query = this.servletRequest.getQueryString(); + if (StringUtils.hasText(query)) { + url.append('?').append(query); + } + return new URI(url.toString()); } catch (URISyntaxException ex) { throw new IllegalStateException("Could not get HttpServletRequest URI: " + ex.getMessage(), ex); diff --git a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java index 490b0369b03..906e04352be 100644 --- a/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java +++ b/spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +64,20 @@ public class ServletServerHttpRequestTests { assertEquals("Invalid uri", uri, request.getURI()); } + // SPR-13876 + + @Test + public void getUriWithEncoding() throws Exception { + 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.getRawPath()); + mockRequest.setQueryString(uri.getRawQuery()); + assertEquals("Invalid uri", uri, request.getURI()); + } + @Test public void getHeaders() throws Exception { String headerName = "MyHeader";