Skip to content

Commit ab7107c

Browse files
poutsmabclozel
authored andcommitted
Added Host property to HttpHeaders
Added getHost/setHost to HttpHeaders, returning/taking a InetSocketAddress.
1 parent f2faf84 commit ab7107c

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.http;
1818

1919
import java.io.Serializable;
20+
import java.net.InetSocketAddress;
2021
import java.net.URI;
2122
import java.nio.charset.Charset;
2223
import java.nio.charset.StandardCharsets;
@@ -801,6 +802,47 @@ public long getExpires() {
801802
return getFirstDate(EXPIRES, false);
802803
}
803804

805+
/**
806+
* Set the (new) value of the {@code Host} header.
807+
* <p>If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0}, the host header
808+
* will only contain the {@linkplain InetSocketAddress#getHostString() hostname}.
809+
*/
810+
public void setHost(InetSocketAddress host) {
811+
String value =
812+
host.getPort() != 0 ? String.format("%s:%d", host.getHostString(), host.getPort()) :
813+
host.getHostString();
814+
set(HOST, value);
815+
}
816+
817+
/**
818+
* Return the value of the required {@code Host} header.
819+
* <p>If the header value does not contain a port, the returned
820+
* {@linkplain InetSocketAddress#getPort() port} will be {@code 0}.
821+
*/
822+
public InetSocketAddress getHost() {
823+
String value = getFirst(HOST);
824+
if (value == null) {
825+
return null;
826+
}
827+
int idx = value.lastIndexOf(':');
828+
String hostname = null;
829+
int port = 0;
830+
if (idx != -1 && idx < value.length() - 1) {
831+
hostname = value.substring(0, idx);
832+
String portString = value.substring(idx + 1);
833+
try {
834+
port = Integer.parseInt(portString);
835+
}
836+
catch (NumberFormatException ex) {
837+
// ignored
838+
}
839+
}
840+
if (hostname == null) {
841+
hostname = value;
842+
}
843+
return InetSocketAddress.createUnresolved(hostname, port);
844+
}
845+
804846
/**
805847
* Set the (new) value of the {@code If-Match} header.
806848
* @since 4.3

spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.http;
1818

19+
import java.net.InetSocketAddress;
1920
import java.net.URI;
2021
import java.net.URISyntaxException;
2122
import java.nio.charset.Charset;
@@ -143,6 +144,24 @@ public void eTag() {
143144
assertEquals("Invalid ETag header", "\"v2.6\"", headers.getFirst("ETag"));
144145
}
145146

147+
@Test
148+
public void host() {
149+
InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 8080);
150+
headers.setHost(host);
151+
assertEquals("Invalid Host header", host, headers.getHost());
152+
assertEquals("Invalid Host header", "localhost:8080", headers.getFirst("Host"));
153+
154+
}
155+
156+
@Test
157+
public void hostNoPort() {
158+
InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 0);
159+
headers.setHost(host);
160+
assertEquals("Invalid Host header", host, headers.getHost());
161+
assertEquals("Invalid Host header", "localhost", headers.getFirst("Host"));
162+
163+
}
164+
146165
@Test(expected = IllegalArgumentException.class)
147166
public void illegalETag() {
148167
String eTag = "v2.6";

0 commit comments

Comments
 (0)