Skip to content

Commit 056284b

Browse files
committed
Polish
1 parent ec55e42 commit 056284b

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -717,16 +717,7 @@ UriComponentsBuilder adaptFromForwardedHeaders(HttpHeaders headers) {
717717
String forwardedToUse = StringUtils.tokenizeToStringArray(forwardedHeader, ",")[0];
718718
Matcher matcher = FORWARDED_HOST_PATTERN.matcher(forwardedToUse);
719719
if (matcher.find()) {
720-
String hostToUse = matcher.group(1).trim();
721-
int portSeparatorIdx = hostToUse.lastIndexOf(":");
722-
if (portSeparatorIdx > hostToUse.lastIndexOf("]")) {
723-
host(hostToUse.substring(0, portSeparatorIdx));
724-
port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1)));
725-
}
726-
else {
727-
host(hostToUse);
728-
port(null);
729-
}
720+
adaptForwardedHost(matcher.group(1).trim());
730721
}
731722
matcher = FORWARDED_PROTO_PATTERN.matcher(forwardedToUse);
732723
if (matcher.find()) {
@@ -736,16 +727,7 @@ UriComponentsBuilder adaptFromForwardedHeaders(HttpHeaders headers) {
736727
else {
737728
String hostHeader = headers.getFirst("X-Forwarded-Host");
738729
if (StringUtils.hasText(hostHeader)) {
739-
String hostToUse = StringUtils.tokenizeToStringArray(hostHeader, ",")[0];
740-
int portSeparatorIdx = hostToUse.lastIndexOf(":");
741-
if (portSeparatorIdx > hostToUse.lastIndexOf("]")) {
742-
host(hostToUse.substring(0, portSeparatorIdx));
743-
port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1)));
744-
}
745-
else {
746-
host(hostToUse);
747-
port(null);
748-
}
730+
adaptForwardedHost(StringUtils.tokenizeToStringArray(hostHeader, ",")[0]);
749731
}
750732

751733
String portHeader = headers.getFirst("X-Forwarded-Port");
@@ -767,6 +749,18 @@ UriComponentsBuilder adaptFromForwardedHeaders(HttpHeaders headers) {
767749
return this;
768750
}
769751

752+
private void adaptForwardedHost(String hostToUse) {
753+
int portSeparatorIdx = hostToUse.lastIndexOf(":");
754+
if (portSeparatorIdx > hostToUse.lastIndexOf("]")) {
755+
host(hostToUse.substring(0, portSeparatorIdx));
756+
port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1)));
757+
}
758+
else {
759+
host(hostToUse);
760+
port(null);
761+
}
762+
}
763+
770764
private void resetHierarchicalComponents() {
771765
this.userInfo = null;
772766
this.host = null;

0 commit comments

Comments
 (0)