You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jose María Zaragoza <de...@gmail.com> on 2014/02/27 09:18:31 UTC

Filter behaviour ( settinf Content-Length header )

Hello:

I'm using Tomcat 6.0.24 and I'm testing how to return Content-Length header
So I've defined a Filter and declared in web.xml of my web application

If I do this:

HttpServletResponse httpResponse = (HttpServletResponse) response;
chain.doFilter(request, response);
httpResponse.setHeader("Content-Length", "200");

doesn't work ( it doesn't set the header and
Transfer-Encoding:chunked is returned )
)

but

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Content-Length", "200");
chain.doFilter(request, response);

does and it works OK

Why ?
Does anyone use a filter to set Content-Length header ?
Must I declared in web.xml of Tomcat ?


Regards

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


Re: Filter behaviour ( settinf Content-Length header )

Posted by Lorenzo Fini <lo...@gmail.com>.
Did you try in the servlet?


2014-02-27 15:31 GMT+01:00 Jose María Zaragoza <de...@gmail.com>:

> Thanks
>
>
> And what do you recommend to me for forcing to return  a Content-Type ?
> Some weird clients require it
>
> If I cannot do it with a Filter , where can I do it ?
>
> 2014-02-27 12:41 GMT+01:00 Konstantin Kolinko <kn...@gmail.com>:
> > 2014-02-27 12:18 GMT+04:00 Jose María Zaragoza <de...@gmail.com>:
> >> Hello:
> >>
> >> I'm using Tomcat 6.0.24 and I'm testing how to return Content-Length
> header
> >> So I've defined a Filter and declared in web.xml of my web application
> >>
> >> If I do this:
> >>
> >> HttpServletResponse httpResponse = (HttpServletResponse) response;
> >> chain.doFilter(request, response);
> >> httpResponse.setHeader("Content-Length", "200");
> >>
> >> doesn't work ( it doesn't set the header and
> >> Transfer-Encoding:chunked is returned )
> >> )
> >>
> >> but
> >>
> >> HttpServletResponse httpResponse = (HttpServletResponse) response;
> >> httpResponse.setHeader("Content-Length", "200");
> >> chain.doFilter(request, response);
> >>
> >> does and it works OK
> >>
> >> Why ?
> >> Does anyone use a filter to set Content-Length header ?
> >> Must I declared in web.xml of Tomcat ?
> >>
> >>
> >
> > 1. Why are you using such an old build of 6.0.x?
> > 2. You cannot set headers when any part of the response has already
> > been sent to client. At that time headers have already been sent over
> > the wire.
> > See ServletResponse.isCommitted().
> > 3. You should be careful with that header. If you set it to a wrong
> > value you may break something.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> > For additional commands, e-mail: users-help@tomcat.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Filter behaviour ( settinf Content-Length header )

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

Jose,

On 2/27/14, 3:22 PM, Christopher Schultz wrote:
> Jose,
> 
> On 2/27/14, 1:29 PM, Jose María Zaragoza wrote:
>> 2014-02-27 17:24 GMT+01:00 Konstantin Kolinko 
>> <kn...@gmail.com>:
>>> 2014-02-27 18:31 GMT+04:00 Jose María Zaragoza 
>>> <de...@gmail.com>:
>>>> 
>>>> And what do you recommend to me for forcing to return  a 
>>>> Content-Type ? Some weird clients require it
>>>> 
>>>> If I cannot do it with a Filter , where can I do it ?
>>>> 
>>> 
>>> You can do it in a Filter.
>>> 
>>> As I said, 1. The header must be set before writing anything
>>> to the output stream. That is per HTTP/1.1 protocol. 2. The
>>> header must have correct value.
>>> 
>>> How to implement that is up to you (do not expect me to teach
>>> you java programming, but maybe others here will do).
>>> 
>>> If you do not know the length before response is generated, a 
>>> solution can be to buffer the response before writing it out.
>>> 
>>> Buffering can be done by writing an adapter around servlet 
>>> response that replaces default output stream with a buffered
>>> one. The adapter can be implemented by extending 
>>> javax.servlet.http.HttpServletResponseWrapper class.  Some 
>>> caching frameworks have filters that perform such buffering
>>> and caching.
> 
> 
>> Thanks. Finally, I did it with a servlet and it works
> 
>> I tried with a Filter and a HttpServletResponseWrapper. Long time
>>  before asking here. And I wrapped the response output stream
>> into a FilterOutputStream , so I could count every byte written I
>> got the right content length . All OK and I was happy. But , when
>> I tried set the header with wrapper.setHeader("Content-Length",
>> count ) , and as you told me before, the response headers had
>> already been flushed ( i checked isCommited() ) I could modify
>> output stream data but I couldn't modify response headers. I
>> don't understand but it is
> 
> Don't forget that you actually need to buffer the data. You can't
> just wrap the container's OutputStream (or Writer) in a
> FilterOutputStream -- the bytes will flow through your stream
> (writer) back to the client while you are counting them.
> 
> If you aren't using a big byte array (or something similar), then
> you aren't buffering.

You may be interested in this thread from 2009:
http://markmail.org/thread/fumpfuspt7a3nesz

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJTD58JAAoJEBzwKT+lPKRYFeEP/RYqIt/69O/nBEI807rWvVKC
OGF2Cc41TwGpI2FG8H5BEvFUQNYBm3+Jjh7keN1kOdfvmKsGGpATFcVg3qnaDr22
qdT//2cUfARaCG3YrEyV02YTGH5aFuVPY9bM5qG4we2KSYwxeUlB150jWu/wRPLt
8VZnJmAuxUWkNqX0HnzT6P8k53npQhK7kT8Dj+pbHIfKfnJeU98BCUsDvbRHPCgJ
WRlDsEqzfOmSLM8pWKXv2NTu+cNcCf9mfEuNMyFFb35ySNnbq0Mo9ndCIRIUTKQL
jRhzLuKrpWDY+XnTADvT+IBkw6rSg9GWfchdfGi5m/RtNVpz09YdpFp4KcIPUdrb
okf4hPyCbdPGm1Rm0YDTiwxjZOtNjClnIQ+WEWuOxR49C1BYhAIclYNzJ87sWnwk
iQRteRTU+fcMLMvG1azS53jiiQsmndFIzQFJ53szIDmU8F08oybJG14IQI7nJP1l
wmQCf4yTTDKJijw1lO9z79mpi7CRaZr6vgPWV7y9+d/H9KyRXutjSafABG+iUB7I
9vTF+85AI0FX1OV5CA6te7eBqNrvwrzSreCSyXLuEJvqprhXtb+3XQApj8N3ml90
g6h7GfRnP8xwuiIs2MmMowoZ3JdtK44IjRkI/gKNwNvXlmKCSAM8m6tF5g32Bkuf
geCSTdZxgOTq/2NpUFfA
=XcJc
-----END PGP SIGNATURE-----

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


Re: Filter behaviour ( settinf Content-Length header )

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

Jose,

On 2/27/14, 1:29 PM, Jose María Zaragoza wrote:
> 2014-02-27 17:24 GMT+01:00 Konstantin Kolinko
> <kn...@gmail.com>:
>> 2014-02-27 18:31 GMT+04:00 Jose María Zaragoza
>> <de...@gmail.com>:
>>> 
>>> And what do you recommend to me for forcing to return  a
>>> Content-Type ? Some weird clients require it
>>> 
>>> If I cannot do it with a Filter , where can I do it ?
>>> 
>> 
>> You can do it in a Filter.
>> 
>> As I said, 1. The header must be set before writing anything to
>> the output stream. That is per HTTP/1.1 protocol. 2. The header
>> must have correct value.
>> 
>> How to implement that is up to you (do not expect me to teach you
>> java programming, but maybe others here will do).
>> 
>> If you do not know the length before response is generated, a
>> solution can be to buffer the response before writing it out.
>> 
>> Buffering can be done by writing an adapter around servlet
>> response that replaces default output stream with a buffered one.
>> The adapter can be implemented by extending 
>> javax.servlet.http.HttpServletResponseWrapper class.  Some
>> caching frameworks have filters that perform such buffering and
>> caching.
> 
> 
> Thanks. Finally, I did it with a servlet and it works
> 
> I tried with a Filter and a HttpServletResponseWrapper. Long time 
> before asking here. And I wrapped the response output stream into a
> FilterOutputStream , so I could count every byte written I got the
> right content length . All OK and I was happy. But , when I tried
> set the header with wrapper.setHeader("Content-Length", count ) ,
> and as you told me before, the response headers had already been
> flushed ( i checked isCommited() ) I could modify output stream
> data but I couldn't modify response headers. I don't understand but
> it is

Don't forget that you actually need to buffer the data. You can't just
wrap the container's OutputStream (or Writer) in a FilterOutputStream
- -- the bytes will flow through your stream (writer) back to the client
while you are counting them.

If you aren't using a big byte array (or something similar), then you
aren't buffering.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJTD56OAAoJEBzwKT+lPKRYQ2EP/2FySYxzV177T3YB6Mf+7+ss
a9DGruZmfJ8EsD21qOO+qjnNOs7pc6sBHGjpCFRDzVut7hhAUzNJVXsVr/bUKzVg
fzrwFCEb+EDmE2fNdVjpDeP16HPOqzfBGwgSltNpYg40q0XXXwDdnZA8xDwUHwib
Iqdz7CJnZj6+5jiSMKOwFunEbC4d3U706/8AbgpUfScNQF9K+sNjwQfgEPQWtudb
IMF2n48Fm4MTfid+UQgQI5XGM9xFhLf+owPYTWyb+bO13ooLyCL8YFMBAKTSumfm
9HSojPOGz7cNOtUZjwIdCNtrcog6sZFoiAJ27Kxgs5qNPH8wMYkkrXMN28sI44A+
MG+8EICHcjRqP+e4Vb8wgb5jJwwK7Sqt5/DqFtNn/xv1m8ISCIe0pl9rgqYAfd5d
QtoNDhjW/V9aG+uhaYCHxEwSrVgscnuun04T49NK/Z4QsswerHmsDHSs79JXr8YW
iRjRj3GWpet5NTZ/k6WvO/5aw24uuk9OD+GMWpFAk3zmYOIh0CVE1WeVbHUXjUyo
mFZm4wxU3ccmQk2vqCoXqX5RVuaApMmc2M1wl+A5jzAqxwPWyBkPrQNg/k0SZ0/2
MSYG+qftA8sqF1v1Ydpb4U+hfNUXCKeatQY38rn43wXmEVkw/UOPtJQ8f3fAUFuL
7OHvXmOo/R3gE9cndILK
=pOC4
-----END PGP SIGNATURE-----

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


Re: Filter behaviour ( settinf Content-Length header )

Posted by Jose María Zaragoza <de...@gmail.com>.
2014-02-27 17:24 GMT+01:00 Konstantin Kolinko <kn...@gmail.com>:
> 2014-02-27 18:31 GMT+04:00 Jose María Zaragoza <de...@gmail.com>:
>>
>> And what do you recommend to me for forcing to return  a Content-Type ?
>> Some weird clients require it
>>
>> If I cannot do it with a Filter , where can I do it ?
>>
>
> You can do it in a Filter.
>
> As I said,
> 1. The header must be set before writing anything to the output
> stream. That is per HTTP/1.1 protocol.
> 2. The header must have correct value.
>
> How to implement that is up to you (do not expect me to teach you java
> programming, but maybe others here will do).
>
> If you do not know the length before response is generated, a solution
> can be to buffer the response before writing it out.
>
> Buffering can be done by writing an adapter around servlet response
> that replaces default output stream with a buffered one. The adapter
> can be implemented by extending
> javax.servlet.http.HttpServletResponseWrapper class.  Some caching
> frameworks have filters that perform such buffering and caching.


Thanks.
Finally, I did it with a servlet and it works

I tried with a Filter and a HttpServletResponseWrapper. Long time
before asking here.
And I wrapped the response output stream into a FilterOutputStream ,
so I could count every byte written
I got the right content length . All OK and I was happy.
But , when I tried set the header with
wrapper.setHeader("Content-Length", count ) , and as you told me
before, the response headers had already been flushed ( i checked
isCommited() )
I could modify output stream data but I couldn't modify response
headers. I don't understand but it is

Regards

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


Re: Filter behaviour ( settinf Content-Length header )

Posted by Konstantin Kolinko <kn...@gmail.com>.
2014-02-27 18:31 GMT+04:00 Jose María Zaragoza <de...@gmail.com>:
>
> And what do you recommend to me for forcing to return  a Content-Type ?
> Some weird clients require it
>
> If I cannot do it with a Filter , where can I do it ?
>

You can do it in a Filter.

As I said,
1. The header must be set before writing anything to the output
stream. That is per HTTP/1.1 protocol.
2. The header must have correct value.

How to implement that is up to you (do not expect me to teach you java
programming, but maybe others here will do).

If you do not know the length before response is generated, a solution
can be to buffer the response before writing it out.

Buffering can be done by writing an adapter around servlet response
that replaces default output stream with a buffered one. The adapter
can be implemented by extending
javax.servlet.http.HttpServletResponseWrapper class.  Some caching
frameworks have filters that perform such buffering and caching.

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


Re: Filter behaviour ( settinf Content-Length header )

Posted by Jose María Zaragoza <de...@gmail.com>.
Thanks


And what do you recommend to me for forcing to return  a Content-Type ?
Some weird clients require it

If I cannot do it with a Filter , where can I do it ?

2014-02-27 12:41 GMT+01:00 Konstantin Kolinko <kn...@gmail.com>:
> 2014-02-27 12:18 GMT+04:00 Jose María Zaragoza <de...@gmail.com>:
>> Hello:
>>
>> I'm using Tomcat 6.0.24 and I'm testing how to return Content-Length header
>> So I've defined a Filter and declared in web.xml of my web application
>>
>> If I do this:
>>
>> HttpServletResponse httpResponse = (HttpServletResponse) response;
>> chain.doFilter(request, response);
>> httpResponse.setHeader("Content-Length", "200");
>>
>> doesn't work ( it doesn't set the header and
>> Transfer-Encoding:chunked is returned )
>> )
>>
>> but
>>
>> HttpServletResponse httpResponse = (HttpServletResponse) response;
>> httpResponse.setHeader("Content-Length", "200");
>> chain.doFilter(request, response);
>>
>> does and it works OK
>>
>> Why ?
>> Does anyone use a filter to set Content-Length header ?
>> Must I declared in web.xml of Tomcat ?
>>
>>
>
> 1. Why are you using such an old build of 6.0.x?
> 2. You cannot set headers when any part of the response has already
> been sent to client. At that time headers have already been sent over
> the wire.
> See ServletResponse.isCommitted().
> 3. You should be careful with that header. If you set it to a wrong
> value you may break something.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

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


Re: Filter behaviour ( settinf Content-Length header )

Posted by Konstantin Kolinko <kn...@gmail.com>.
2014-02-27 12:18 GMT+04:00 Jose María Zaragoza <de...@gmail.com>:
> Hello:
>
> I'm using Tomcat 6.0.24 and I'm testing how to return Content-Length header
> So I've defined a Filter and declared in web.xml of my web application
>
> If I do this:
>
> HttpServletResponse httpResponse = (HttpServletResponse) response;
> chain.doFilter(request, response);
> httpResponse.setHeader("Content-Length", "200");
>
> doesn't work ( it doesn't set the header and
> Transfer-Encoding:chunked is returned )
> )
>
> but
>
> HttpServletResponse httpResponse = (HttpServletResponse) response;
> httpResponse.setHeader("Content-Length", "200");
> chain.doFilter(request, response);
>
> does and it works OK
>
> Why ?
> Does anyone use a filter to set Content-Length header ?
> Must I declared in web.xml of Tomcat ?
>
>

1. Why are you using such an old build of 6.0.x?
2. You cannot set headers when any part of the response has already
been sent to client. At that time headers have already been sent over
the wire.
See ServletResponse.isCommitted().
3. You should be careful with that header. If you set it to a wrong
value you may break something.

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