Browse Source

Merge branch '5.2.x'

pull/25040/head
Rossen Stoyanchev 6 years ago
parent
commit
8a59a63ade
  1. 32
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 6
      spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java
  3. 8
      spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

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

@ -1747,8 +1747,14 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (!(other instanceof HttpHeaders)) { if (!(other instanceof HttpHeaders)) {
return false; return false;
} }
HttpHeaders otherHeaders = (HttpHeaders) other; return unwrap(this).equals(unwrap((HttpHeaders) other));
return this.headers.equals(otherHeaders.headers); }
private static MultiValueMap<String, String> unwrap(HttpHeaders headers) {
while (headers.headers instanceof HttpHeaders) {
headers = (HttpHeaders) headers.headers;
}
return headers.headers;
} }
@Override @Override
@ -1763,20 +1769,17 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/** /**
* Return an {@code HttpHeaders} object that can only be read, not written to. * Apply a read-only {@code HttpHeaders} wrapper around the given headers.
*/ */
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) { public static HttpHeaders readOnlyHttpHeaders(MultiValueMap<String, String> headers) {
Assert.notNull(headers, "HttpHeaders must not be null"); Assert.notNull(headers, "HttpHeaders must not be null");
if (headers instanceof ReadOnlyHttpHeaders) { return (headers instanceof ReadOnlyHttpHeaders ?
return headers; (HttpHeaders) headers : new ReadOnlyHttpHeaders(headers));
}
else {
return new ReadOnlyHttpHeaders(headers);
}
} }
/** /**
* Return an {@code HttpHeaders} object that can be read and written to. * Remove any read-only wrapper that may have been previously applied around
* the given headers via {@link #readOnlyHttpHeaders(MultiValueMap)}.
* @since 5.1.1 * @since 5.1.1
*/ */
public static HttpHeaders writableHttpHeaders(HttpHeaders headers) { public static HttpHeaders writableHttpHeaders(HttpHeaders headers) {
@ -1784,12 +1787,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (headers == EMPTY) { if (headers == EMPTY) {
return new HttpHeaders(); return new HttpHeaders();
} }
else if (headers instanceof ReadOnlyHttpHeaders) { return (headers instanceof ReadOnlyHttpHeaders ? new HttpHeaders(headers.headers) : headers);
return new HttpHeaders(headers.headers);
}
else {
return headers;
}
} }
/** /**

6
spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -46,8 +46,8 @@ class ReadOnlyHttpHeaders extends HttpHeaders {
private List<MediaType> cachedAccept; private List<MediaType> cachedAccept;
ReadOnlyHttpHeaders(HttpHeaders headers) { ReadOnlyHttpHeaders(MultiValueMap<String, String> headers) {
super(headers.headers); super(headers);
} }

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

@ -703,4 +703,12 @@ public class HttpHeadersTests {
assertThat(readOnlyHttpHeaders.entrySet()).extracting(Entry::getKey).containsExactly(expectedKeys); assertThat(readOnlyHttpHeaders.entrySet()).extracting(Entry::getKey).containsExactly(expectedKeys);
} }
@Test // gh-25034
public void equalsUnwrapsHttpHeaders() {
HttpHeaders headers1 = new HttpHeaders();
HttpHeaders headers2 = new HttpHeaders(new HttpHeaders(headers1));
assertThat(headers1).isEqualTo(headers2);
assertThat(headers2).isEqualTo(headers1);
}
} }

Loading…
Cancel
Save