Browse Source

Added Host property to HttpHeaders

Added getHost/setHost to HttpHeaders, returning/taking a
InetSocketAddress.
pull/1141/merge
Arjen Poutsma 10 years ago committed by Brian Clozel
parent
commit
ab7107c4c5
  1. 42
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 19
      spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

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

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.http;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@ -801,6 +802,47 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable @@ -801,6 +802,47 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
return getFirstDate(EXPIRES, false);
}
/**
* Set the (new) value of the {@code Host} header.
* <p>If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0}, the host header
* will only contain the {@linkplain InetSocketAddress#getHostString() hostname}.
*/
public void setHost(InetSocketAddress host) {
String value =
host.getPort() != 0 ? String.format("%s:%d", host.getHostString(), host.getPort()) :
host.getHostString();
set(HOST, value);
}
/**
* Return the value of the required {@code Host} header.
* <p>If the header value does not contain a port, the returned
* {@linkplain InetSocketAddress#getPort() port} will be {@code 0}.
*/
public InetSocketAddress getHost() {
String value = getFirst(HOST);
if (value == null) {
return null;
}
int idx = value.lastIndexOf(':');
String hostname = null;
int port = 0;
if (idx != -1 && idx < value.length() - 1) {
hostname = value.substring(0, idx);
String portString = value.substring(idx + 1);
try {
port = Integer.parseInt(portString);
}
catch (NumberFormatException ex) {
// ignored
}
}
if (hostname == null) {
hostname = value;
}
return InetSocketAddress.createUnresolved(hostname, port);
}
/**
* Set the (new) value of the {@code If-Match} header.
* @since 4.3

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

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.http;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
@ -143,6 +144,24 @@ public class HttpHeadersTests { @@ -143,6 +144,24 @@ public class HttpHeadersTests {
assertEquals("Invalid ETag header", "\"v2.6\"", headers.getFirst("ETag"));
}
@Test
public void host() {
InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 8080);
headers.setHost(host);
assertEquals("Invalid Host header", host, headers.getHost());
assertEquals("Invalid Host header", "localhost:8080", headers.getFirst("Host"));
}
@Test
public void hostNoPort() {
InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 0);
headers.setHost(host);
assertEquals("Invalid Host header", host, headers.getHost());
assertEquals("Invalid Host header", "localhost", headers.getFirst("Host"));
}
@Test(expected = IllegalArgumentException.class)
public void illegalETag() {
String eTag = "v2.6";

Loading…
Cancel
Save