• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpServletRequest - catch duplicate request header names

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am running into a problem with HttpServletRequest. I have an application in which it is to retrieve request headers. If the application finds duplicate header names, it's supposed to throw an error. However, after a bit of Googling, I found out that HttpServletRequest ignores duplicate header names, and proceeds with getting the first header name it encounters. Is there anything I can do to catch those duplicate request headers?

I'm using Tomcat 6 as the application container.

This is the code:



Attached was test screenshot using RESTClient:
This is the log:

name: host value: localhost:8080
name: user-agent value: CocoaRestClient/8 CFNetwork/520.4.3 Darwin/11.4.2 (x86_64)(MacBookPro9%2C2)
name: content-length value: 23
name: accept value:
name: abcd value: 1234556
name: accept-language value: en-us
name: accept-encoding value: gzip, deflate
name: content-type value:
application/x-www-form-urlencoded
name: connection value: keep-alive

As you can see, only the first occurence of "abcd" has been caught and the other one was discarded. Hope somebody can lend me a hand on this. Thank you very much

This is the sample servlet so that you can replicate the problem I am having.

Screen-Shot-2012-09-29-at-8.04.08-PM.png
[Thumbnail for Screen-Shot-2012-09-29-at-8.04.08-PM.png]
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone got an idea? Your help will much appreciated
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to be patient, it's just been 3 hours since you asked the question and it's a weekend too. If anyone knows the answer they'll reply.

As for the question, I'm not sure what the spec says about duplicate request headers. If those are allowed, then your application probably should consider what it means to have different values for the same header. For example, how do you decide which header value is more appropriate?
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:You have to be patient, it's just been 3 hours since you asked the question and it's a weekend too. If anyone knows the answer they'll reply.

As for the question, I'm not sure what the spec says about duplicate request headers. If those are allowed, then your application probably should consider what it means to have different values for the same header. For example, how do you decide which header value is more appropriate?



Sorry sir for being impatient. As for the specs, duplicate request header names are not allowed and should be handled (such as throwing an error).
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aries dimagiba wrote:As for the specs, duplicate request header names are not allowed


That's not correct. According to the HTTP spec,


Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.


http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The interface ServletRequest deleclares a method

 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

aries dimagiba wrote:As for the specs, duplicate request header names are not allowed


That's not correct. According to the HTTP spec,


Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.


http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2



Thanks for the reply Sir. However, what I meant was the specification of the project I am handling.
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:The interface ServletRequest deleclares a method




Thanks for the reply Sir. Can I extract the duplicated headers with that method?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aries dimagiba wrote:

Richard Tookey wrote:The interface ServletRequest deleclares a method




Thanks for the reply Sir. Can I extract the duplicated headers with that method?



How about reading the Javadoc for the method and then writing a quick test Servlet if you are still not sure.
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

How about reading the Javadoc for the method and then writing a quick test Servlet if you are still not sure.



I've tried that Sir but I think that method is concerned only on parameters being passed, not on the request headers.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the text that Ulf has quoted. It clearly explains how those values will be represented, the delimiter and even the order of those values. All you have to do is parse it as per your needs.
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:Please read the text that Ulf has quoted. It clearly explains how those values will be represented, the delimiter and even the order of those values. All you have to do is parse it as per your needs.



I see Sir, however, i dont know how to parse that using Java. Can you show me how? Thanks.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aries dimagiba wrote:

Richard Tookey wrote:

How about reading the Javadoc for the method and then writing a quick test Servlet if you are still not sure.



I've tried that Sir but I think that method is concerned only on parameters being passed, not on the request headers.



Sorry I misunderstood your requirement. Please ignore everything I posted on this.
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:

aries dimagiba wrote:

Richard Tookey wrote:

How about reading the Javadoc for the method and then writing a quick test Servlet if you are still not sure.



I've tried that Sir but I think that method is concerned only on parameters being passed, not on the request headers.



Sorry I misunderstood your requirement. Please ignore everything I posted on this.



That's fine Sir, appreciated your effort for this. Thank you very much
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aries dimagiba wrote:

Jaikiran Pai wrote:Please read the text that Ulf has quoted. It clearly explains how those values will be represented, the delimiter and even the order of those values. All you have to do is parse it as per your needs.



I see Sir, however, i dont know how to parse that using Java. Can you show me how? Thanks.



I just spent a few seconds to see if there are already available APIs for this. Guess what http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeaders%28java.lang.String%29
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:

aries dimagiba wrote:

Jaikiran Pai wrote:Please read the text that Ulf has quoted. It clearly explains how those values will be represented, the delimiter and even the order of those values. All you have to do is parse it as per your needs.



I see Sir, however, i dont know how to parse that using Java. Can you show me how? Thanks.



I just spent a few seconds to see if there are already available APIs for this. Guess what http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeaders%28java.lang.String%29



Thank you Sir, but request.getHeaders and request.getHeaderNames() are already present in the code i posted above.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying the other value for the header name isn't being passed on to your application at all?
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:Are you saying the other value for the header name isn't being passed on to your application at all?



Yes Sir.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are those duplicates headers being added?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, you asked the same question here http://stackoverflow.com/questions/12652182/catching-duplicate-request-headers-with-httpservletrequest which is fine, but when asking the same question in multiple different forums, please do let others know about it.
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:How are those duplicates headers being added?



Jaikiran Pai wrote:By the way, you asked the same question here http://stackoverflow.com/questions/12652182/catching-duplicate-request-headers-with-httpservletrequest which is fine, but when asking the same question in multiple different forums, please do let others know about it.



Indeed Sir Jaikiran, just to gather more information regarding my problem. Yes, I'll do that asap. To answer your question, I add the duplicate headers using an app called Cocoa RESTClient for testing my application.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just did a quick test with my local Tomcat installation and used curl as the client to send those duplicate headers. It worked fine. The server side (Tomcat) application received those duplicate headers and printed both the values. It's the same code as yours:



The curl client invocation was:



The output was:

Printing headers:


Header name: host header value: localhost:8080
Header name: accept header value: */*
Header name: foo header value: bar
Header name: foo header value: baragain
Header name: abc header value: xyz



As you can see the "foo" header was received with 2 different values. So I believe this isn't a Tomcat issue. I'm on Tomcat 5.5.x by the way. You'll have to check with the cocoa-rest-client to make sure the headers are indeed being passed with different values.
 
aries dimagiba
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jaikiran Pai wrote:I just did a quick test with my local Tomcat installation and used curl as the client to send those duplicate headers. It worked fine. The server side (Tomcat) application received those duplicate headers and printed both the values. It's the same code as yours:



The curl client invocation was:



The output was:

Printing headers:


Header name: host header value: localhost:8080
Header name: accept header value: */*
Header name: foo header value: bar
Header name: foo header value: baragain
Header name: abc header value: xyz



As you can see the "foo" header was received with 2 different values. So I believe this isn't a Tomcat issue. I'm on Tomcat 5.5.x by the way. You'll have to check with the cocoa-rest-client to make sure the headers are indeed being passed with different values.



Thank you very much Sir, i did test it using curl command and the duplicate headers were really detected and my application threw the error I expected. It seems this is a bug on RESTClient.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aries dimagiba wrote: i did test it using curl command and the duplicate headers were really detected and my application threw the error I expected


Good to know
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic