Revised URI building in http.server (avoid package cycle with web.util)
Issue: SPR-16434
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -33,7 +34,7 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Package-private default implementation of {@link ServerHttpRequest.Builder}.
|
||||
@@ -129,19 +130,49 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
|
||||
@Override
|
||||
public ServerHttpRequest build() {
|
||||
URI uriToUse = getUriToUse();
|
||||
return new DefaultServerHttpRequest(uriToUse, this.contextPath, this.httpHeaders,
|
||||
return new DefaultServerHttpRequest(getUriToUse(), this.contextPath, this.httpHeaders,
|
||||
this.httpMethodValue, this.cookies, this.body, this.originalRequest);
|
||||
|
||||
}
|
||||
|
||||
private URI getUriToUse() {
|
||||
if (this.uriPath == null) {
|
||||
return this.uri;
|
||||
}
|
||||
return UriComponentsBuilder.fromUri(this.uri).replacePath(this.uriPath).build(true).toUri();
|
||||
|
||||
StringBuilder uriBuilder = new StringBuilder();
|
||||
if (this.uri.getScheme() != null) {
|
||||
uriBuilder.append(this.uri.getScheme()).append(':');
|
||||
}
|
||||
if (this.uri.getUserInfo() != null || this.uri.getHost() != null) {
|
||||
uriBuilder.append("//");
|
||||
if (this.uri.getUserInfo() != null) {
|
||||
uriBuilder.append(this.uri.getUserInfo()).append('@');
|
||||
}
|
||||
if (this.uri.getHost() != null) {
|
||||
uriBuilder.append(this.uri.getHost());
|
||||
}
|
||||
if (this.uri.getPort() != -1) {
|
||||
uriBuilder.append(':').append(this.uri.getPort());
|
||||
}
|
||||
}
|
||||
if (StringUtils.hasLength(this.uriPath)) {
|
||||
uriBuilder.append(this.uriPath);
|
||||
}
|
||||
if (this.uri.getRawQuery() != null) {
|
||||
uriBuilder.append('?').append(this.uri.getRawQuery());
|
||||
}
|
||||
if (this.uri.getFragment() != null) {
|
||||
uriBuilder.append('#').append(this.uri.getFragment());
|
||||
}
|
||||
try {
|
||||
return new URI(uriBuilder.toString());
|
||||
}
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException("Invalid URI path: \"" + this.uriPath + "\"", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class DefaultServerHttpRequest extends AbstractServerHttpRequest {
|
||||
|
||||
private final String methodValue;
|
||||
@@ -158,7 +189,6 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
|
||||
private final ServerHttpRequest originalRequest;
|
||||
|
||||
|
||||
public DefaultServerHttpRequest(URI uri, @Nullable String contextPath,
|
||||
HttpHeaders headers, String methodValue, MultiValueMap<String, HttpCookie> cookies,
|
||||
Flux<DataBuffer> body, ServerHttpRequest originalRequest) {
|
||||
@@ -172,7 +202,6 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
|
||||
this.originalRequest = originalRequest;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getMethodValue() {
|
||||
return this.methodValue;
|
||||
|
||||
@@ -89,7 +89,7 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
|
||||
super(scheme, fragment);
|
||||
|
||||
this.userInfo = userInfo;
|
||||
this.userInfo = userInfo;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = (path != null ? path : NULL_PATH_COMPONENT);
|
||||
@@ -166,10 +166,8 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
queryBuilder.append('&');
|
||||
}
|
||||
queryBuilder.append(name);
|
||||
|
||||
if (value != null) {
|
||||
queryBuilder.append('=');
|
||||
queryBuilder.append(value.toString());
|
||||
queryBuilder.append('=').append(value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,8 +209,8 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
String hostTo = (this.host != null ? encodeUriComponent(this.host, charset, getHostType()) : null);
|
||||
PathComponent pathTo = this.path.encode(charset);
|
||||
MultiValueMap<String, String> paramsTo = encodeQueryParams(charset);
|
||||
return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo, hostTo, this.port,
|
||||
pathTo, paramsTo, true, false);
|
||||
return new HierarchicalUriComponents(
|
||||
schemeTo, fragmentTo, userInfoTo, hostTo, this.port, pathTo, paramsTo, true, false);
|
||||
}
|
||||
|
||||
private MultiValueMap<String, String> encodeQueryParams(Charset charset) {
|
||||
@@ -355,8 +353,8 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
PathComponent pathTo = this.path.expand(uriVariables);
|
||||
MultiValueMap<String, String> paramsTo = expandQueryParams(uriVariables);
|
||||
|
||||
return new HierarchicalUriComponents(schemeTo, fragmentTo, userInfoTo, hostTo, portTo,
|
||||
pathTo, paramsTo, false, false);
|
||||
return new HierarchicalUriComponents(
|
||||
schemeTo, fragmentTo, userInfoTo, hostTo, portTo, pathTo, paramsTo, false, false);
|
||||
}
|
||||
|
||||
private MultiValueMap<String, String> expandQueryParams(UriTemplateVariables variables) {
|
||||
@@ -388,21 +386,18 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
public String toUriString() {
|
||||
StringBuilder uriBuilder = new StringBuilder();
|
||||
if (getScheme() != null) {
|
||||
uriBuilder.append(getScheme());
|
||||
uriBuilder.append(':');
|
||||
uriBuilder.append(getScheme()).append(':');
|
||||
}
|
||||
if (this.userInfo != null || this.host != null) {
|
||||
uriBuilder.append("//");
|
||||
if (this.userInfo != null) {
|
||||
uriBuilder.append(this.userInfo);
|
||||
uriBuilder.append('@');
|
||||
uriBuilder.append(this.userInfo).append('@');
|
||||
}
|
||||
if (this.host != null) {
|
||||
uriBuilder.append(host);
|
||||
uriBuilder.append(this.host);
|
||||
}
|
||||
if (getPort() != -1) {
|
||||
uriBuilder.append(':');
|
||||
uriBuilder.append(port);
|
||||
uriBuilder.append(':').append(this.port);
|
||||
}
|
||||
}
|
||||
String path = getPath();
|
||||
@@ -414,12 +409,10 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
}
|
||||
String query = getQuery();
|
||||
if (query != null) {
|
||||
uriBuilder.append('?');
|
||||
uriBuilder.append(query);
|
||||
uriBuilder.append('?').append(query);
|
||||
}
|
||||
if (getFragment() != null) {
|
||||
uriBuilder.append('#');
|
||||
uriBuilder.append(getFragment());
|
||||
uriBuilder.append('#').append(getFragment());
|
||||
}
|
||||
return uriBuilder.toString();
|
||||
}
|
||||
@@ -428,7 +421,7 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
public URI toUri() {
|
||||
try {
|
||||
if (this.encoded) {
|
||||
return new URI(toString());
|
||||
return new URI(toUriString());
|
||||
}
|
||||
else {
|
||||
String path = getPath();
|
||||
|
||||
Reference in New Issue
Block a user