7

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 ?

angryITguy
  • 9,332
  • 8
  • 54
  • 82
Malax
  • 9,436
  • 9
  • 48
  • 64

2 Answers2

15

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.

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Actually, its Jetty 5. I'll investigate this issue, thank for pointing me to the Java EE documentation... ;-) – Malax Sep 04 '09 at 14:44
  • I think Jetty might be up to version 6 (or 7?), you might want to take a look at upgrading. – matt b Sep 04 '09 at 14:58
  • Sorry to resurrect an old thread, but I just wanted to note that Tomcat 7.0.39 also returns case sensitive headers. – bmauter Sep 24 '13 at 16:29
  • @bmauter: Do you mean that if you request `CONTENT-TYPE` it won't give you the value for `Content-Type`? That sounds broken... – Jon Skeet Sep 24 '13 at 16:31
  • @JonSkeet Correct. I asked for "Authorization" and got nothing when iOS sent "authorization". I can reproduce this at will. – bmauter Sep 24 '13 at 16:58
  • @bmauter: Odd. I'd file a bug against Tomcat (or see if there's an existing one). It's clearly violating the spec, judging by your description. – Jon Skeet Sep 24 '13 at 16:59
  • @JonSkeet Nevermind, it looks like I am an idiot. Sorry about that. – bmauter Sep 24 '13 at 17:01
  • Actually, Tomcat 8.0.9 is doing this way: the headers are all lowercase. :(( – Raul Luna Oct 04 '17 at 07:16
  • 1
    same as @JonSkeet , getHeader("Authorization") is failing to recognize header "authorization", and this is on tomcat8. When i change the actual header to "Authorization" then it works. – abdel Mar 16 '18 at 16:34
2

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) {
abdel
  • 643
  • 1
  • 5
  • 13