Skip to content

Commit 6882b38

Browse files
committed
8333590: UnmodifiableHeaders.toString() returns a value that represents empty headers
Reviewed-by: dfuchs, michaelm
1 parent cbb6747 commit 6882b38

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

src/jdk.httpserver/share/classes/sun/net/httpserver/UnmodifiableHeaders.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,73 +32,96 @@
3232
public class UnmodifiableHeaders extends Headers {
3333

3434
private final Headers headers; // modifiable, but no reference to it escapes
35-
private final Map<String, List<String>> map; // unmodifiable
35+
private final Map<String, List<String>> unmodifiableView; // unmodifiable
3636

3737
public UnmodifiableHeaders(Headers headers) {
3838
var h = headers;
3939
var unmodHeaders = new Headers();
4040
h.forEach((k, v) -> unmodHeaders.put(k, Collections.unmodifiableList(v)));
41-
this.map = Collections.unmodifiableMap(unmodHeaders);
41+
this.unmodifiableView = Collections.unmodifiableMap(unmodHeaders);
4242
this.headers = unmodHeaders;
4343
}
4444

45+
@Override
4546
public int size() {return headers.size();}
4647

48+
@Override
4749
public boolean isEmpty() {return headers.isEmpty();}
4850

51+
@Override
4952
public boolean containsKey(Object key) { return headers.containsKey(key); }
5053

54+
@Override
5155
public boolean containsValue(Object value) { return headers.containsValue(value); }
5256

57+
@Override
5358
public List<String> get(Object key) { return headers.get(key); }
5459

60+
@Override
5561
public String getFirst(String key) { return headers.getFirst(key); }
5662

63+
@Override
5764
public List<String> put(String key, List<String> value) {
5865
throw new UnsupportedOperationException ("unsupported operation");
5966
}
6067

68+
@Override
6169
public void add(String key, String value) {
6270
throw new UnsupportedOperationException ("unsupported operation");
6371
}
6472

73+
@Override
6574
public void set(String key, String value) {
6675
throw new UnsupportedOperationException ("unsupported operation");
6776
}
6877

78+
@Override
6979
public List<String> remove(Object key) {
7080
throw new UnsupportedOperationException ("unsupported operation");
7181
}
7282

83+
@Override
7384
public void putAll(Map<? extends String,? extends List<String>> t) {
7485
throw new UnsupportedOperationException ("unsupported operation");
7586
}
7687

88+
@Override
7789
public void clear() {
7890
throw new UnsupportedOperationException ("unsupported operation");
7991
}
8092

81-
public Set<String> keySet() { return map.keySet(); }
93+
@Override
94+
public Set<String> keySet() { return unmodifiableView.keySet(); }
8295

83-
public Collection<List<String>> values() { return map.values(); }
96+
@Override
97+
public Collection<List<String>> values() { return unmodifiableView.values(); }
8498

85-
/* TODO check that contents of set are not modifable : security */
86-
87-
public Set<Map.Entry<String, List<String>>> entrySet() { return map.entrySet(); }
99+
@Override
100+
public Set<Map.Entry<String, List<String>>> entrySet() { return unmodifiableView.entrySet(); }
88101

102+
@Override
89103
public List<String> replace(String key, List<String> value) {
90104
throw new UnsupportedOperationException("unsupported operation");
91105
}
92106

107+
@Override
93108
public boolean replace(String key, List<String> oldValue, List<String> newValue) {
94109
throw new UnsupportedOperationException ("unsupported operation");
95110
}
96111

112+
@Override
97113
public void replaceAll(BiFunction<? super String, ? super List<String>, ? extends List<String>> function) {
98114
throw new UnsupportedOperationException ("unsupported operation");
99115
}
100116

117+
@Override
101118
public boolean equals(Object o) {return headers.equals(o);}
102119

120+
@Override
103121
public int hashCode() {return headers.hashCode();}
122+
123+
@Override
124+
public String toString() {
125+
return headers.toString();
126+
}
104127
}

test/jdk/com/sun/net/httpserver/UnmodifiableHeadersTest.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8251496
26+
* @bug 8251496 8333590
2727
* @summary Test that UnmodifiableHeaders is in fact immutable
2828
* @modules jdk.httpserver/sun.net.httpserver:+open
2929
* @run testng/othervm UnmodifiableHeadersTest
@@ -44,7 +44,9 @@
4444
import org.testng.annotations.Test;
4545
import sun.net.httpserver.UnmodifiableHeaders;
4646
import static org.testng.Assert.assertEquals;
47+
import static org.testng.Assert.assertNotNull;
4748
import static org.testng.Assert.assertThrows;
49+
import static org.testng.Assert.assertTrue;
4850

4951
public class UnmodifiableHeadersTest {
5052

@@ -70,9 +72,9 @@ public Object[][] headers() {
7072
var exchange = new TestHttpExchange(headers);
7173

7274
return new Object[][] {
73-
{ exchange.getRequestHeaders() },
74-
{ Headers.of("Foo", "Bar") },
75-
{ Headers.of(Map.of("Foo", List.of("Bar"))) },
75+
{ exchange.getRequestHeaders() },
76+
{ Headers.of("Foo", "Bar") },
77+
{ Headers.of(Map.of("Foo", List.of("Bar"))) },
7678
};
7779
}
7880

@@ -83,6 +85,44 @@ public static void testUnmodifiableHeaders(Headers headers) {
8385
assertUnmodifiableList(headers);
8486
}
8587

88+
@DataProvider
89+
public Object[][] toStringHeaders() {
90+
final Headers headers = new Headers();
91+
headers.add("hello", "World");
92+
return new Object[][] {
93+
{ headers },
94+
{ Headers.of("abc", "XYZ") },
95+
{ Headers.of(Map.of("foo", List.of("Bar"))) },
96+
{ Headers.of(Map.of("Hello", List.of())) },
97+
{ Headers.of(Map.of("one", List.of("two", "THREE"))) },
98+
};
99+
}
100+
101+
/*
102+
* Verify that the String returned by Headers.toString() contains the expected
103+
* key/value(s)
104+
*/
105+
@Test(dataProvider = "toStringHeaders")
106+
public void testToString(final Headers headers) {
107+
final Headers copy = Headers.of(headers);
108+
assertNotNull(copy, "Headers.of() returned null");
109+
final String actualToString = copy.toString();
110+
assertNotNull(actualToString, "toString() returned null");
111+
for (final Map.Entry<String, List<String>> originalHeadersEntry : headers.entrySet()) {
112+
final String expectedKey = originalHeadersEntry.getKey();
113+
// We just verify the presence of key and value in the toString()
114+
// return value. We intentionally don't expect or verify that the
115+
// toString() content is in some specific form.
116+
assertTrue(actualToString.contains(expectedKey),
117+
expectedKey + " missing in output of Headers.of().toString()");
118+
final List<String> expectedVals = originalHeadersEntry.getValue();
119+
for (final String val : expectedVals) {
120+
assertTrue(actualToString.contains(val), val + " for header key "
121+
+ expectedKey + " missing in output of Headers.of().toString()");
122+
}
123+
}
124+
}
125+
86126
static final Class<UnsupportedOperationException> UOP = UnsupportedOperationException.class;
87127

88128
static void assertUnsupportedOperation(Headers headers) {

0 commit comments

Comments
 (0)