Skip to content

Commit b8835c9

Browse files
committed
Copy MockHttpServletRequest changes to spring-web
See gh-24916
1 parent 6b410df commit b8835c9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletRequest.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -668,11 +668,14 @@ public void setServerName(String serverName) {
668668

669669
@Override
670670
public String getServerName() {
671-
String host = getHeader(HttpHeaders.HOST);
671+
String rawHostHeader = getHeader(HttpHeaders.HOST);
672+
String host = rawHostHeader;
672673
if (host != null) {
673674
host = host.trim();
674675
if (host.startsWith("[")) {
675-
host = host.substring(1, host.indexOf(']'));
676+
int indexOfClosingBracket = host.indexOf(']');
677+
Assert.state(indexOfClosingBracket > -1, () -> "Invalid Host header: " + rawHostHeader);
678+
host = host.substring(0, indexOfClosingBracket + 1);
676679
}
677680
else if (host.contains(":")) {
678681
host = host.substring(0, host.indexOf(':'));
@@ -690,12 +693,15 @@ public void setServerPort(int serverPort) {
690693

691694
@Override
692695
public int getServerPort() {
693-
String host = getHeader(HttpHeaders.HOST);
696+
String rawHostHeader = getHeader(HttpHeaders.HOST);
697+
String host = rawHostHeader;
694698
if (host != null) {
695699
host = host.trim();
696700
int idx;
697701
if (host.startsWith("[")) {
698-
idx = host.indexOf(':', host.indexOf(']'));
702+
int indexOfClosingBracket = host.indexOf(']');
703+
Assert.state(indexOfClosingBracket > -1, () -> "Invalid Host header: " + rawHostHeader);
704+
idx = host.indexOf(':', indexOfClosingBracket);
699705
}
700706
else {
701707
idx = host.indexOf(':');

0 commit comments

Comments
 (0)