@@ -101,13 +101,13 @@ public void setRemoveOnly(boolean removeOnly) {
101101 }
102102
103103 /**
104- * Use this property to enable relative redirects as explained in and also
105- * using the same response wrapper as {@link RelativeRedirectFilter} does.
106- * Or if both filters are used , only one will wrap the response .
104+ * Use this property to enable relative redirects as explained in
105+ * {@link RelativeRedirectFilter}, and also using the same response wrapper
106+ * as that filter does, or if both are configured , only one will wrap.
107107 * <p>By default, if this property is set to false, in which case calls to
108108 * {@link HttpServletResponse#sendRedirect(String)} are overridden in order
109- * to turn relative into absolute URLs since (which Servlet containers are
110- * also required to do) also taking forwarded headers into consideration .
109+ * to turn relative into absolute URLs, also taking into account forwarded
110+ * headers.
111111 * @param relativeRedirects whether to use relative redirects
112112 * @since 4.3.10
113113 */
@@ -307,54 +307,41 @@ public ForwardedHeaderExtractingResponse(HttpServletResponse response, HttpServl
307307 this .request = request ;
308308 }
309309
310- @ Override
311- public void sendRedirect (String location ) throws IOException {
312- UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (location );
313-
314- // Absolute location
315- if (builder .build ().getScheme () != null ) {
316- super .sendRedirect (location );
317- return ;
318- }
310+ @ Override
311+ public void sendRedirect (String location ) throws IOException {
319312
320- // Network-path reference
321- if (location .startsWith ("//" )) {
322- String scheme = this .request .getScheme ();
323- super .sendRedirect (builder .scheme (scheme ).toUriString ());
324- return ;
325- }
313+ UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (location );
314+ UriComponents uriComponents = builder .build ();
326315
327- String fragment = null ;
328- int fragmentIndex = location .indexOf ('#' );
329- if (fragmentIndex != -1 ) {
330- if (location .length () > fragmentIndex ) {
331- fragment = location .substring (fragmentIndex + 1 );
332- }
333- location = location .substring (0 , fragmentIndex );
334- }
316+ // Absolute location
317+ if (uriComponents .getScheme () != null ) {
318+ super .sendRedirect (location );
319+ return ;
320+ }
335321
336- String query = null ;
337- int queryIndex = location .indexOf ('?' );
338- if (queryIndex != -1 ) {
339- if (location .length () > queryIndex ) {
340- query = location .substring (queryIndex + 1 );
341- }
342- location = location .substring (0 , queryIndex );
343- }
322+ // Network-path reference
323+ if (location .startsWith ("//" )) {
324+ String scheme = this .request .getScheme ();
325+ super .sendRedirect (builder .scheme (scheme ).toUriString ());
326+ return ;
327+ }
344328
345- // Relative to Servlet container root or to current request
346- String path = (location .startsWith (FOLDER_SEPARATOR ) ? location :
347- StringUtils .applyRelativePath (this .request .getRequestURI (), location ));
329+ String path = uriComponents .getPath ();
330+ if (path != null ) {
331+ // Relative to Servlet container root or to current request
332+ path = (path .startsWith (FOLDER_SEPARATOR ) ? path :
333+ StringUtils .applyRelativePath (this .request .getRequestURI (), path ));
334+ }
348335
349- String result = UriComponentsBuilder
350- .fromHttpRequest (new ServletServerHttpRequest (this .request ))
351- .replacePath (path )
352- .replaceQuery (query )
353- .fragment (fragment )
354- .build ().normalize ().toUriString ();
336+ String result = UriComponentsBuilder
337+ .fromHttpRequest (new ServletServerHttpRequest (this .request ))
338+ .replacePath (path )
339+ .replaceQuery (uriComponents . getQuery () )
340+ .fragment (uriComponents . getFragment () )
341+ .build ().normalize ().toUriString ();
355342
356- super .sendRedirect (result );
357- }
343+ super .sendRedirect (result );
344+ }
358345 }
359346
360347}
0 commit comments