[#4265] Not allow to add/set DefaultHttpHeaders to itself.

Motivation:

We should prevent to add/set DefaultHttpHeaders to itself to prevent unexpected side-effects.

Modifications:

Throw IllegalArgumentException if user tries to pass the same instance to set/add.

Result:

No surprising side-effects.
This commit is contained in:
Norman Maurer 2015-09-24 08:33:51 +02:00
parent f96777312d
commit d4079d1403
2 changed files with 18 additions and 0 deletions

View File

@ -59,6 +59,9 @@ public class DefaultHttpHeaders extends HttpHeaders {
@Override @Override
public HttpHeaders add(HttpHeaders headers) { public HttpHeaders add(HttpHeaders headers) {
if (headers instanceof DefaultHttpHeaders) { if (headers instanceof DefaultHttpHeaders) {
if (headers == this) {
throw new IllegalArgumentException("can't add to itself.");
}
DefaultHttpHeaders defaultHttpHeaders = (DefaultHttpHeaders) headers; DefaultHttpHeaders defaultHttpHeaders = (DefaultHttpHeaders) headers;
HeaderEntry e = defaultHttpHeaders.head.after; HeaderEntry e = defaultHttpHeaders.head.after;
while (e != defaultHttpHeaders.head) { while (e != defaultHttpHeaders.head) {
@ -74,6 +77,9 @@ public class DefaultHttpHeaders extends HttpHeaders {
@Override @Override
public HttpHeaders set(HttpHeaders headers) { public HttpHeaders set(HttpHeaders headers) {
if (headers instanceof DefaultHttpHeaders) { if (headers instanceof DefaultHttpHeaders) {
if (headers == this) {
throw new IllegalArgumentException("can't add to itself.");
}
clear(); clear();
DefaultHttpHeaders defaultHttpHeaders = (DefaultHttpHeaders) headers; DefaultHttpHeaders defaultHttpHeaders = (DefaultHttpHeaders) headers;
HeaderEntry e = defaultHttpHeaders.head.after; HeaderEntry e = defaultHttpHeaders.head.after;

View File

@ -67,4 +67,16 @@ public class HttpHeadersTest {
HttpHeaders headers = new DefaultHttpHeaders(false); HttpHeaders headers = new DefaultHttpHeaders(false);
headers.set("test", (CharSequence) null); headers.set("test", (CharSequence) null);
} }
@Test(expected = IllegalArgumentException.class)
public void testAddSelf() {
HttpHeaders headers = new DefaultHttpHeaders(false);
headers.add(headers);
}
@Test(expected = IllegalArgumentException.class)
public void testSetSelf() {
HttpHeaders headers = new DefaultHttpHeaders(false);
headers.set(headers);
}
} }