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?
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.
aries dimagiba
Greenhorn
Joined: Sep 29, 2012
Posts: 12
posted
0
Anyone got an idea? Your help will much appreciated
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?
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).
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
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.
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.
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
Joined: Sep 29, 2012
Posts: 12
posted
0
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
Ranch Hand
Joined: Aug 27, 2012
Posts: 361
posted
0
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
Joined: Sep 29, 2012
Posts: 12
posted
0
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 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.
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.
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.
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:
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
Joined: Sep 29, 2012
Posts: 12
posted
0
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:
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.