You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Alexander Müller <Al...@brz.gv.at> on 2009/06/08 10:39:21 UTC

Header names lower case

Hi,

I noticed the Tomcat implementation of HttpServletRequest.getHeaderNames() returns all header names in lower case. Is there any possibility to get them with their original case?

Thanks,
Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> The HTTP spec (HTTP/1.1, section 4.2:
> http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html) states that HTTP
> header field names are case-insensitive.
> 
> You should not be relying on a particular caseification (?) of header
> fields.
> 
> Are you just interested in what the client sent?
> 
> I think you'd have to modify the Connector in order to do that. In order
> for the headers to be case-insensitive, case must be normalized when
> assembling headers (the same header is allowed multiple times, and the
> servlet spec requires that all those field values be available in a
> single Enumeration when calling getHeaders). The implementers chose
> lower case as their normalization technique. In order to undo that,
> you'll have to modify the source of Tomcat.
> 
> - -chris

Thanks Chris, as I just mentioned in my reply to Markus, I am in the need of the original case as I am forwarding the headers and the receiving party apparently expects a particular case.

Anyhow, I already almost thought there would be no way to "configure" this, but wanted to be sure.

Thanks again,
Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alexander,

On 6/8/2009 4:39 AM, Alexander Müller wrote:
> I noticed the Tomcat implementation of
> HttpServletRequest.getHeaderNames() returns all header names in lower
> case. Is there any possibility to get them with their original case?

The HTTP spec (HTTP/1.1, section 4.2:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html) states that HTTP
header field names are case-insensitive.

You should not be relying on a particular caseification (?) of header
fields.

Are you just interested in what the client sent?

I think you'd have to modify the Connector in order to do that. In order
for the headers to be case-insensitive, case must be normalized when
assembling headers (the same header is allowed multiple times, and the
servlet spec requires that all those field values be available in a
single Enumeration when calling getHeaders). The implementers chose
lower case as their normalization technique. In order to undo that,
you'll have to modify the source of Tomcat.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkotOT0ACgkQ9CaO5/Lv0PBglACdEN+OPLMHnZxFOdcimd18ro2n
OLwAn0TMHNUmNqxpGkmL12p1YLEl8AFt
=BVx/
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
>> I noticed the Tomcat implementation of HttpServletRequest.getHeaderNames()
>> returns all header names in lower case. Is there any possibility to
>> get them with their original case?
> 
> Want to give us a hint about the Tomcat version you're looking at?
> 
>  - Chuck

I noticed it on Tomcat 5.0.30 and 6.0.20.

Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Header names lower case

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Alexander Müller [mailto:Alexander.Mueller@brz.gv.at]
> Subject: Re: Header names lower case
> 
> 
> > I noticed the Tomcat implementation of HttpServletRequest.getHeaderNames()
> > returns all header names in lower case. Is there any possibility to
> > get them with their original case?
> 
> Nobody having any idea about this behaviour?

Want to give us a hint about the Tomcat version you're looking at?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> Hi,
> 
> I noticed the Tomcat implementation of HttpServletRequest.getHeaderNames() 
> returns all header names in lower case. Is there any possibility to get them 
> with their original case?

Nobody having any idea about this behaviour?

Thanks again,
Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> definitiv möglicherweise?
> Martin 

Most likely ;), but what should the snippet tell me?

Alexander

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Header names lower case

Posted by Martin Gainty <mg...@hotmail.com>.
javax.servlet.http.HttpServlet  
//to receive
    service() method
   {
    Enumeration reqHeaderEnum = req.getHeaderNames();
    while( reqHeaderEnum.hasMoreElements() ) {
        String headerName = (String)reqHeaderEnum.nextElement();
//THIS IS THE HEADER NAME
        responseString += CRLF + headerName + ": " +
        req.getHeader(headerName); 
//THIS IS THE VALUE assigned to HEADER_NAME
    }

//to send
  /**
     * <p>Receives an HTTP HEAD request from the protected
     * <code>service</code> method and handles the
     * request.
     * The client sends a HEAD request when it wants
     * to see only the headers of a response, such as
     * Content-Type or Content-Length. The HTTP HEAD
     * method counts the output bytes in the response
     * to set the Content-Length header accurately.
     *
     * <p>If you override this method, you can avoid computing
     * the response body and just set the response headers
     * directly to improve performance. Make sure that the
     * <code>doHead</code> method you write is both safe
     * and idempotent (that is, protects itself from being
     * called multiple times for one HTTP HEAD request).
     *
     * <p>If the HTTP HEAD request is incorrectly formatted,
     * <code>doHead</code> returns an HTTP "Bad Request"
     * message.
     *
     * @param req    the request object that is passed
     *            to the servlet
     *            
     * @param resp    the response object that the servlet
     *            uses to return the headers to the clien
     *
     * @exception IOException        if an input or output error occurs
     *
     * @exception ServletException    if the request for the HEAD
     *                    could not be handled
     */
    protected void doHead(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    }

definitiv möglicherweise?
Martin 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.






> Date: Mon, 8 Jun 2009 18:34:04 +0200
> From: Alexander.Mueller@brz.gv.at
> To: users@tomcat.apache.org
> Subject: Re: Header names lower case
> 
> 
> > "apparently"? Hm, in this case I'd double-check whether whatever problem
> > you're having is indeed caused by the case of the header names. Just to
> > make sure you're not barking at the wrong tree.
> > If the receiving party is really relying on the case of header names,
> > I'd ask them to fix their broken code.
> 
> Maybe better "definitely" :)
> 
> If I send the headers with the proper capitalisation I get through, otherwise I am getting an error I wouldnt send the header. Unfortunately this is one of the situations where you cant ask the other parties to correct their code, but have to adapt by yourself ;).
> 
> Alexander
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

_________________________________________________________________
Insert movie times and more without leaving Hotmail®. 
http://windowslive.com/Tutorial/Hotmail/QuickAdd?ocid=TXT_TAGLM_WL_HM_Tutorial_QuickAdd_062009

Re: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> Just to nitpick, there is no "proper capitalisation", since the RFC says 
> that headers are case-insensitive.

I know, I referred to "proper" as in what the target machine expects.

> Apart from that, I would offer the following subjective advice, in the 
> form of suggestions :
> - it sounds like you are trying to use Tomcat as some kind of 
> transparent proxy here

Its not actually a transparent one, but just a normal reverse proxy.

> , and maybe in this case Tomcat is not the right 
> tool for the job. Maybe you should use some kind of front-end to Tomcat, 
> which will catch these requests, and proxy them to the broken back-end, 
> instead of trying to do this with Tomcat, which does not really have the 
> built-in mechanisms for that kind of thing.
> (I'm thinking of Apache httpd, with mod_proxy, mod_rewrite, 
> mod_setenvif, mod_perl, etc..)
> - you may want to have a look at the urlrewrite filter at www.tuckey.org 
> (yes, I know I sound like a salesman for that thing, and I'm starting to 
> think I should ask for a commission).  It can check incoming headers, 
> and I believe it can delete and/or rewrite some of them before even 
> forwarding them to your Tomcat application.

Unfortunately it is not only about simple proxying through but the application has to do more, particularly header validation and modification.

> - if you really persist in wanting to do this at the application level, 
> I would look at it in another way : rather than trying to be a 
> transparent proxy, have your Tomcat application :
>    a) receive and process the client request, including the lower-cased 
> headers
>    b) from your application, issue a brand /new/ HTTP request to the 
> broken  back-end, composing the headers that you know it expects.  For 
> that, you could use some library like the Apache Commons HTTPClient.

Thats what I am already doing and where I encountered this bug/feature/whatever :).

> The problem with trying to modify the Tomcat code for this, is that you 
> will set yourself up for all kinds of annoyances, because you cannot 
> even be sure how all /your/ different clients may capitalise these 
> headers or not when they send the request to you.  So you could be faced 
> with having to check if you got a header like "User-Agent" or 
> "USER-AGENT" or "user-agent" and so on...

The choice of modifying the actual source code would have been probably the last as this would come with too many implications (security fixes, upgrades, maintainability, ....).


Thanks,
Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by André Warnier <aw...@ice-sa.com>.
Alexander Müller wrote:
>> "apparently"? Hm, in this case I'd double-check whether whatever problem
>> you're having is indeed caused by the case of the header names. Just to
>> make sure you're not barking at the wrong tree.
>> If the receiving party is really relying on the case of header names,
>> I'd ask them to fix their broken code.
> 
> Maybe better "definitely" :)
> 
> If I send the headers with the proper capitalisation I get through, otherwise I am getting an error I wouldnt send the header. Unfortunately this is one of the situations where you cant ask the other parties to correct their code, but have to adapt by yourself ;).
> 
Just to nitpick, there is no "proper capitalisation", since the RFC says 
that headers are case-insensitive.
Apart from that, I would offer the following subjective advice, in the 
form of suggestions :
- it sounds like you are trying to use Tomcat as some kind of 
transparent proxy here, and maybe in this case Tomcat is not the right 
tool for the job. Maybe you should use some kind of front-end to Tomcat, 
which will catch these requests, and proxy them to the broken back-end, 
instead of trying to do this with Tomcat, which does not really have the 
built-in mechanisms for that kind of thing.
(I'm thinking of Apache httpd, with mod_proxy, mod_rewrite, 
mod_setenvif, mod_perl, etc..)
- you may want to have a look at the urlrewrite filter at www.tuckey.org 
(yes, I know I sound like a salesman for that thing, and I'm starting to 
think I should ask for a commission).  It can check incoming headers, 
and I believe it can delete and/or rewrite some of them before even 
forwarding them to your Tomcat application.
- if you really persist in wanting to do this at the application level, 
I would look at it in another way : rather than trying to be a 
transparent proxy, have your Tomcat application :
   a) receive and process the client request, including the lower-cased 
headers
   b) from your application, issue a brand /new/ HTTP request to the 
broken  back-end, composing the headers that you know it expects.  For 
that, you could use some library like the Apache Commons HTTPClient.
- if your purpose is just to transparently proxy these requests, you 
could also think about issuing a redirect response to the browser, and 
let it deal with it himself.

The problem with trying to modify the Tomcat code for this, is that you 
will set yourself up for all kinds of annoyances, because you cannot 
even be sure how all /your/ different clients may capitalise these 
headers or not when they send the request to you.  So you could be faced 
with having to check if you got a header like "User-Agent" or 
"USER-AGENT" or "user-agent" and so on...

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> Is it only certain headers that are a problem, or is it all headers?
> 
> If it's only some, you could specifically code these such that they are
> handled with the desired capitalization, and pass everything else through.
> 
> Or, if there are some rules to the capitalization requirements (like the
> leading character is always caps, and anything following a - is caps,
> like Accept-Charset instead of accept-charset), then
> 
> - -chris

Thanks Chris, thats exactly what I am doing now. It's only a few of application specific header and as the proxy is capable of plugins, I am simply setting those headers with their required capitalisation and their respective values.

Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alexander,

On 6/8/2009 12:34 PM, Alexander Müller wrote:
>> "apparently"? Hm, in this case I'd double-check whether whatever
>> problem you're having is indeed caused by the case of the header
>> names. Just to make sure you're not barking at the wrong tree. If
>> the receiving party is really relying on the case of header names, 
>> I'd ask them to fix their broken code.
> 
> Maybe better "definitely" :)
> 
> If I send the headers with the proper capitalisation I get through,
> otherwise I am getting an error I wouldnt send the header.
> Unfortunately this is one of the situations where you cant ask the
> other parties to correct their code, but have to adapt by yourself
> ;).

Is it only certain headers that are a problem, or is it all headers?

If it's only some, you could specifically code these such that they are
handled with the desired capitalization, and pass everything else through.

Or, if there are some rules to the capitalization requirements (like the
leading character is always caps, and anything following a - is caps,
like Accept-Charset instead of accept-charset), then

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkouZaMACgkQ9CaO5/Lv0PCcDwCdGIkvXcbIj8Ie6KQJGOw6/wnk
JAcAn3nA06AY21ZTe300/I3fSprhln48
=Jn/H
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> "apparently"? Hm, in this case I'd double-check whether whatever problem
> you're having is indeed caused by the case of the header names. Just to
> make sure you're not barking at the wrong tree.
> If the receiving party is really relying on the case of header names,
> I'd ask them to fix their broken code.

Maybe better "definitely" :)

If I send the headers with the proper capitalisation I get through, otherwise I am getting an error I wouldnt send the header. Unfortunately this is one of the situations where you cant ask the other parties to correct their code, but have to adapt by yourself ;).

Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Markus Schönhaber <to...@list-post.mks-mail.de>.
Alexander Müller:

>> RFC 2616 defines field names as case-insensitive. Relying on a
>> header field's case therefore seems at least non-portable to me. 
>> Maybe you should elaborate what problem you are really trying to
>> solve.
> 
> For me the case wouldnt be important, but I am forwarding (basically
> a proxy) the headers and the receiving party apparently is case
> sensitive.

"apparently"? Hm, in this case I'd double-check whether whatever problem
you're having is indeed caused by the case of the header names. Just to
make sure you're not barking at the wrong tree.
If the receiving party is really relying on the case of header names,
I'd ask them to fix their broken code.

-- 
Regards
  mks

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Alexander Müller <Al...@brz.gv.at>.
> I can't answer your question but I'm curious: why is a HTTP header
> name's case of any importance to you?
> 
> RFC 2616 defines field names as case-insensitive. Relying on a header
> field's case therefore seems at least non-portable to me.
> Maybe you should elaborate what problem you are really trying to solve.

For me the case wouldnt be important, but I am forwarding (basically a proxy) the headers and the receiving party apparently is case sensitive.

Alexander


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Header names lower case

Posted by Markus Schönhaber <to...@list-post.mks-mail.de>.
Alexander Müller:

> I noticed the Tomcat implementation of
> HttpServletRequest.getHeaderNames() returns all header names in lower
> case. Is there any possibility to get them with their original case?

I can't answer your question but I'm curious: why is a HTTP header
name's case of any importance to you?

RFC 2616 defines field names as case-insensitive. Relying on a header
field's case therefore seems at least non-portable to me.
Maybe you should elaborate what problem you are really trying to solve.

-- 
Regards
  mks

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org