Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpHeaders getFirst() case insensitivity

I'm fetching HTTP headers with Spring's RestTemplate.

HTTP headers are case insensitive, but the documentation for HttpHeaders doesn't seem to acknowledge this.

A quick test shows that things are working as expected...

HttpHeaders headers = restTemplate.headForHeaders(url);
Long a = Long.parseLong(headers.getFirst("Content-Length"));
Long b = Long.parseLong(headers.getFirst("content-length"));
assert( a.equals(b) ); // passes

Can I be sure that this test will pass under all Spring configurations?

like image 468
Brent Bradburn Avatar asked Nov 15 '25 07:11

Brent Bradburn


1 Answers

Per this commit it is explicitly stated that HttpHeaders are case-insensitive:

Note that HttpHeaders generally treats header names in a case-insensitive manner.


Old answer

HttpHeaders has only one public constructor and its body:

public HttpHeaders() {
    this(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH), false);
}

And according to LinkedCaseInsensitiveMap docs:

LinkedHashMap variant that stores String keys in a case-insensitive manner, for example for key-based access in a results table.

Preserves the original order as well as the original casing of keys, while allowing for contains, get and remove calls with any case of key.

So yes, it always works in case insensitive manner.

But why you do not use HttpHeaders#getContentLength()? :

// no need to convert String to long
long contentLength = httpHeaders.getContentLength();
like image 81
Denis Zavedeev Avatar answered Nov 17 '25 20:11

Denis Zavedeev