|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
51 | 51 | import org.springframework.util.LinkedMultiValueMap;
|
52 | 52 | import org.springframework.util.MultiValueMap;
|
53 | 53 | import org.springframework.util.StringUtils;
|
| 54 | +import org.springframework.web.util.UriComponentsBuilder; |
54 | 55 |
|
55 | 56 | /**
|
56 | 57 | * Adapt {@link ServerHttpRequest} to the Servlet {@link HttpServletRequest}.
|
@@ -90,8 +91,8 @@ public ServletServerHttpRequest(MultiValueMap<String, String> headers, HttpServl
|
90 | 91 | AsyncContext asyncContext, String servletPath, DataBufferFactory bufferFactory, int bufferSize)
|
91 | 92 | throws IOException, URISyntaxException {
|
92 | 93 |
|
93 |
| - super(HttpMethod.valueOf(request.getMethod()), initUri(request), request.getContextPath() + servletPath, |
94 |
| - initHeaders(headers, request)); |
| 94 | + super(HttpMethod.valueOf(request.getMethod()), initUri(request), |
| 95 | + request.getContextPath() + servletPath, initHeaders(headers, request)); |
95 | 96 |
|
96 | 97 | Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
|
97 | 98 | Assert.isTrue(bufferSize > 0, "'bufferSize' must be greater than 0");
|
@@ -121,14 +122,42 @@ private static MultiValueMap<String, String> createDefaultHttpHeaders(HttpServle
|
121 | 122 | return headers;
|
122 | 123 | }
|
123 | 124 |
|
124 |
| - private static URI initUri(HttpServletRequest request) throws URISyntaxException { |
125 |
| - Assert.notNull(request, "'request' must not be null"); |
126 |
| - StringBuffer url = request.getRequestURL(); |
127 |
| - String query = request.getQueryString(); |
128 |
| - if (StringUtils.hasText(query)) { |
129 |
| - url.append('?').append(query); |
| 125 | + @SuppressWarnings("JavaExistingMethodCanBeUsed") |
| 126 | + private static URI initUri(HttpServletRequest servletRequest) { |
| 127 | + Assert.notNull(servletRequest, "'request' must not be null"); |
| 128 | + String urlString = null; |
| 129 | + String query = null; |
| 130 | + boolean hasQuery = false; |
| 131 | + try { |
| 132 | + StringBuffer requestURL = servletRequest.getRequestURL(); |
| 133 | + query = servletRequest.getQueryString(); |
| 134 | + hasQuery = StringUtils.hasText(query); |
| 135 | + if (hasQuery) { |
| 136 | + requestURL.append('?').append(query); |
| 137 | + } |
| 138 | + urlString = requestURL.toString(); |
| 139 | + return new URI(urlString); |
| 140 | + } |
| 141 | + catch (URISyntaxException ex) { |
| 142 | + if (hasQuery) { |
| 143 | + try { |
| 144 | + // Maybe malformed query, try to parse and encode it |
| 145 | + query = UriComponentsBuilder.fromUriString("?" + query).build().toUri().getRawQuery(); |
| 146 | + return new URI(servletRequest.getRequestURL().toString() + "?" + query); |
| 147 | + } |
| 148 | + catch (URISyntaxException ex2) { |
| 149 | + try { |
| 150 | + // Try leaving it out |
| 151 | + return new URI(servletRequest.getRequestURL().toString()); |
| 152 | + } |
| 153 | + catch (URISyntaxException ex3) { |
| 154 | + // ignore |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + throw new IllegalStateException( |
| 159 | + "Could not resolve HttpServletRequest as URI: " + urlString, ex); |
130 | 160 | }
|
131 |
| - return new URI(url.toString()); |
132 | 161 | }
|
133 | 162 |
|
134 | 163 | @SuppressWarnings("NullAway")
|
|
0 commit comments