Add fromHttpRequest to UriComponentsBuilder
Before this change, detection of X-Forwarded-* headers was only built into ServletUriComponentsBuilder. This change adds a new method for creating a UriComponentsBuilder from an existing HttpRequest. This is equivalent to the fromUri method + X-Forwarded-* header values.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -18,11 +18,14 @@ package org.springframework.web.servlet.support;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
@@ -112,34 +115,11 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||
* Initialize a builder with a scheme, host,and port (but not path and query).
|
||||
*/
|
||||
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
|
||||
String scheme = request.getScheme();
|
||||
String host = request.getServerName();
|
||||
int port = request.getServerPort();
|
||||
|
||||
String hostHeader = request.getHeader("X-Forwarded-Host");
|
||||
if (StringUtils.hasText(hostHeader)) {
|
||||
String[] hosts = StringUtils.commaDelimitedListToStringArray(hostHeader);
|
||||
String hostToUse = hosts[0];
|
||||
if (hostToUse.contains(":")) {
|
||||
String[] hostAndPort = StringUtils.split(hostToUse, ":");
|
||||
host = hostAndPort[0];
|
||||
port = Integer.parseInt(hostAndPort[1]);
|
||||
}
|
||||
else {
|
||||
host = hostToUse;
|
||||
port = -1;
|
||||
}
|
||||
}
|
||||
|
||||
String portHeader = request.getHeader("X-Forwarded-Port");
|
||||
if (StringUtils.hasText(portHeader)) {
|
||||
port = Integer.parseInt(portHeader);
|
||||
}
|
||||
|
||||
String protocolHeader = request.getHeader("X-Forwarded-Proto");
|
||||
if (StringUtils.hasText(protocolHeader)) {
|
||||
scheme = protocolHeader;
|
||||
}
|
||||
HttpRequest httpRequest = new ServletServerHttpRequest(request);
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
|
||||
String scheme = uriComponents.getScheme();
|
||||
String host = uriComponents.getHost();
|
||||
int port = uriComponents.getPort();
|
||||
|
||||
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
|
||||
builder.scheme(scheme);
|
||||
|
||||
Reference in New Issue
Block a user