Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpServletRequest and getHeader(): How to handle case insensitive headers properly?

Since I discovered that HTTP headers are case-insensive according to the RFC, i was wondering how I can access HTTP headers in a case-insensitive way with Servlets. There is a #getHeader(String) method to obtain a header but it turned out that this method treats the header fields case sensitive.

Is there a "case insensitive" way to get header fields ? Or do i have to iterate over all the header fields to find the header field I was looking for ?

like image 614
Malax Avatar asked Sep 04 '09 14:09

Malax


People also ask

Is getHeader case sensitive?

The docs for getHeader(String) state: The header name is case insensitive. so it sounds like a bug in the container you're using.

Should HTTP headers be capitalized?

Just as in HTTP/1. x, header field names are strings of ASCII characters that are compared in a case-insensitive fashion. However, header field names MUST be converted to lowercase prior to their encoding in HTTP/2.

What is request getHeader?

getHeader. java.lang.String getHeader(java.lang.String name) Returns the value of the specified request header as a String . If the request did not include a header of the specified name, this method returns null . If there are multiple headers with the same name, this method returns the first head in the request.


2 Answers

Which servlet container are you using? The docs for getHeader(String) state:

The header name is case insensitive.

so it sounds like a bug in the container you're using.

like image 91
Jon Skeet Avatar answered Oct 07 '22 23:10

Jon Skeet


tomcat 8.0.24 impl of getHeader delegates to 'org.apache.tomcat.util.http.MimeHeaders' which eventually calls this method below which in turn does case insensitive check

313  public MessageBytes getValue(String name) {
like image 31
abdel Avatar answered Oct 08 '22 00:10

abdel