RFR: 8263506: Make sun.net.httpserver.UnmodifiableHeaders unmodifiable

Michael McMahon michaelm at openjdk.java.net
Mon Mar 22 16:32:44 UTC 2021


On Mon, 22 Mar 2021 11:56:58 GMT, Julia Boes <jboes at openjdk.org> wrote:

>> src/jdk.httpserver/share/classes/sun/net/httpserver/UnmodifiableHeaders.java line 60:
>> 
>>> 58:             map.forEach((k, v) -> headers.add(k, v.get(0)));
>>> 59:             return headers.getFirst(key);
>>> 60:         }
>> 
>> Should be possible to just return a reference to the first String in the map rather than instantiating a new Headers and populating it just for the duration of the call.
>
> Headers normalizes the key before any get/getFirst, for example Headers line 131:
> 
> `public List<String> get(Object key) {
>             return map.get(normalize((String)key));
>         }`
> 
> I assume we want to maintain this behaviour, but there might be a better way than creating this local Headers?

Actually, I just noticed that the `List<String>`s are still modifiable. The constructor might have to create an intermediate `Map<String,List<String>>` with unmodifiable Lists and then you create an unmodifiable Map from that. 

As regards the comment above, yes you want to maintain that behavior. Could you normalize the key here and then just return `map.get(key).get(0)` (with suitable null checks)?

-------------

PR: https://git.openjdk.java.net/jdk/pull/3032


More information about the net-dev mailing list