You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2013/03/26 15:58:23 UTC

Adding Content-Length response header

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

All,

Like most folks on the list, I have quite a few dynamic resources
being served through Tomcat. For nearly all of them, the
Content-Length response header is /not/ included in the response
because either content length isn't known when the response is
committed, and therefore the Content-Length header simply cannot be
sent or the response is not being buffered in the application, and so
the application isn't counting bytes and doesn't know the content
length even if it could be reported.

I'm wondering if Tomcat can do anything about that.

Here's the situation I have: I've got a response that I'm fairly sure
fits into the response's buffer size, and I'd like to send a
Content-Length header in that case. I could probably put a wrapper
around the response's ServletOutputStream that counts bytes and then
looks for "done" conditions (OutputStream.close, etc.), then adds a
Content-Length header if the response hasn't yet been committed.

I wonder if Tomcat could do something like that more easily -- since
the buffer is being managed by Tomcat already, and Tomcat knows when
the response will be committed/completed.

So,

A: Is this something that Tomcat can do already? I don't see any
configuration options that sound like they would do this kind of thing.

B: Is this something appropriate for a container to do in general?

C: If I were to write a Filter to accomplish this manually, are there
any potential dangers if a traditional non-synchronous
request/response is being processed?

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRUbePAAoJEBzwKT+lPKRYOGYP/Aos+mNfVqYKBNKNBeVzvDyr
Io2y3uWlHi3E5nYza9v+mqCmkwgmCycEKgwj96bceJM3mBgzhjmr0tTXNBB7PJGF
7HWwrOYGq1JtyT/dHdEnj6swd431beHZ1+9YvMTBI68OVPXLVNCapvMw88i7C4cG
B6CGY1XIpmjBG701MhdlDDF9UMN2AwGEuQtHQAAD7d3tl51ilLVjHXURKviiHN/a
VuP5RFHF8lZfbT+JeGeX1rRH5+toLMdlhU9vbhdwMyTXVB7bpwoFm5ONbfcKmlRu
pLqHwY9wBGi9TjAxD2s1hxVozacL1wcX/eziH3rh9ZdwZIpHxLbZLSZB0+TJeWFC
ILg2NTQ40V8msGqHTx6R1PrGqaLeCrPZnps7VIETh8DbCb248dzjMnAXtzrFWIYb
toLliJMZIXhf2sx91FbVUxcm6siyoaJoqd7Wi1l0aLgXgk9ucQZVF32ZD2T8Rw0g
7ZRZAUkOc9sqijpX6JQ2no71H5G5GOKDahmOUwW3ErLl/upCIQQRnu/BB6kP4wxS
DzPchIuGFJFMiEz7x31ldes/2aRY4xOun10hX7fKcr3reOn0nupHV2iD7kFfNeLH
KCL4X+BV2J4f2DsPxf8CzGNxPnRMhu/iX8LuXmSxQZhfPBlCW9peztgR8MjTFzFk
FvoIBJUOfDNiX9N2jzKG
=n26I
-----END PGP SIGNATURE-----

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


Re: Adding Content-Length response header

Posted by Nick Williams <ni...@nicholaswilliams.net>.
On Mar 26, 2013, at 3:15 PM, Christopher Schultz wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Konstantin,
> 
> On 3/26/13 11:08 AM, Konstantin Kolinko wrote:
>> 2013/3/26 Christopher Schultz <ch...@christopherschultz.net>:
>>> 
>>> Like most folks on the list, I have quite a few dynamic
>>> resources being served through Tomcat. For nearly all of them,
>>> the Content-Length response header is /not/ included in the
>>> response because either content length isn't known when the
>>> response is committed, and therefore the Content-Length header
>>> simply cannot be sent or the response is not being buffered in
>>> the application, and so the application isn't counting bytes and
>>> doesn't know the content length even if it could be reported.
>>> 
>>> I'm wondering if Tomcat can do anything about that.
>>> 
>>> Here's the situation I have: I've got a response that I'm fairly
>>> sure fits into the response's buffer size, and I'd like to send
>>> a Content-Length header in that case. I could probably put a
>>> wrapper around the response's ServletOutputStream that counts
>>> bytes and then looks for "done" conditions (OutputStream.close,
>>> etc.), then adds a Content-Length header if the response hasn't
>>> yet been committed.
>>> 
>>> I wonder if Tomcat could do something like that more easily --
>>> since the buffer is being managed by Tomcat already, and Tomcat
>>> knows when the response will be committed/completed.
>>> 
>>> So,
>>> 
>>> A: Is this something that Tomcat can do already? I don't see any 
>>> configuration options that sound like they would do this kind of
>>> thing.
>>> 
>>> B: Is this something appropriate for a container to do in
>>> general?
>>> 
>>> C: If I were to write a Filter to accomplish this manually, are
>>> there any potential dangers if a traditional non-synchronous 
>>> request/response is being processed?
>> 
>> 1. Your Tomcat version = ?
> 
> Duh. I'm running both Tomcat 6.0.36 and Tomcat 7.0.35.
> 
>> 2. Are you interested in GET/POST requests, or HEAD requests?
>> 
>> HEAD requests were fixed by this commit, from 5 days ago 
>> http://svn.apache.org/r1459087
> 
> Mostly POST requests. I'll take a look at the commit to see what was
> changed and see if it might be relevant.

FYI, that commit 5 days ago was just when it was merged to 7.x. The issue was actually fixed nearly a year ago in trunk. I just pointed out last week that it should be merged to 7.

https://issues.apache.org/bugzilla/show_bug.cgi?id=53454

> 
>> 3. Tomcat is already able to provide Content-Length header when
>> all response fits into a buffer.
>> 
>> But, if an explicit flush() is called, Tomcat has to start sending 
>> data (usually using chunked encoding) and thus content length  is
>> not known at the time when response headers are being sent.
> 
> Hmm... I might be flushing the buffer explicitly. That's probably a
> stupid idea because Tomcat should ultimately flush everything for me.
> I'll check on that.
> 
>> What generates your response? A servlet, a filter, or a JSP?
> 
> All servlets in these cases. I'll follow-up with better answers to the
> above.
> 
> Thanks,
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
> 
> iQIcBAEBCAAGBQJRUgHzAAoJEBzwKT+lPKRYjp8QAJmDS7eebC0S8tM+yG0C+PTy
> tyczL2vaxu1bwLlt5LDwFwQ9q6S7NtPvGQiRIGqg3VDWz9H2q3UVDRR3v/zhyxdE
> ZPutdXWRPPmU26ZyEF4W3ttyxGmtm+y4zpAkUfLW/h0Zl9gVDV6TqwYX9GsylEyV
> /RrAN4AHKh2uBJc6SnV61wU5a+7LrBTy9Tlxi0om7UGf8sGoXTGDtmEpYYUfJDQu
> +cUN3OTaOStdBXR+gLQ17NSlY+/nTZVoMMIfrJHTVBjAWpAQQwAXCZMaCfmdbaYS
> EkNk8cehmnHyhYhy6JckbpC1qaugDwVERaOl86LcuSsKFzPAdFaoHS+XF6Qp2GDk
> gtiw6wpTiM6DF6f2BP6ZXJLcddmtZMhtygGsKLXyxmR9hguOKVioVPIi5GDnifZR
> 0PtTjmY1dis/Xcfe/WKIKJER8jRtiBpzLHOLpomFh95hL1y7dnfRUYv9EzlrtV1k
> TdQurstjKsd7P7XWUsSpyyjXmluCWZQoCJbzVwEdMMmar6kfaAGqgCj4FAHV/JTH
> wNKRqd461q1hj/dKKJk65KbyKSFP2y4zpF/irtVvH4y0JyNAJWzmxdC8XJHu3EAp
> Q9UsvPuZNtICR3twPoFzicJm1wXLV00TCg7XYiXExNZarDnzA6GAtOVMx5Oxmuaq
> osWzVzmulIbdy9enhTZR
> =AVzf
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> 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: Adding Content-Length response header

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

Konstantin,

On 3/26/13 11:08 AM, Konstantin Kolinko wrote:
> 2013/3/26 Christopher Schultz <ch...@christopherschultz.net>:
>> 
>> Like most folks on the list, I have quite a few dynamic
>> resources being served through Tomcat. For nearly all of them,
>> the Content-Length response header is /not/ included in the
>> response because either content length isn't known when the
>> response is committed, and therefore the Content-Length header
>> simply cannot be sent or the response is not being buffered in
>> the application, and so the application isn't counting bytes and
>> doesn't know the content length even if it could be reported.
>> 
>> I'm wondering if Tomcat can do anything about that.
>> 
>> Here's the situation I have: I've got a response that I'm fairly
>> sure fits into the response's buffer size, and I'd like to send
>> a Content-Length header in that case. I could probably put a
>> wrapper around the response's ServletOutputStream that counts
>> bytes and then looks for "done" conditions (OutputStream.close,
>> etc.), then adds a Content-Length header if the response hasn't
>> yet been committed.
>> 
>> I wonder if Tomcat could do something like that more easily --
>> since the buffer is being managed by Tomcat already, and Tomcat
>> knows when the response will be committed/completed.
>> 
>> So,
>> 
>> A: Is this something that Tomcat can do already? I don't see any 
>> configuration options that sound like they would do this kind of
>> thing.
>> 
>> B: Is this something appropriate for a container to do in
>> general?
>> 
>> C: If I were to write a Filter to accomplish this manually, are
>> there any potential dangers if a traditional non-synchronous 
>> request/response is being processed?
> 
> 1. Your Tomcat version = ?

Duh. I'm running both Tomcat 6.0.36 and Tomcat 7.0.35.

> 2. Are you interested in GET/POST requests, or HEAD requests?
> 
> HEAD requests were fixed by this commit, from 5 days ago 
> http://svn.apache.org/r1459087

Mostly POST requests. I'll take a look at the commit to see what was
changed and see if it might be relevant.

> 3. Tomcat is already able to provide Content-Length header when
> all response fits into a buffer.
> 
> But, if an explicit flush() is called, Tomcat has to start sending 
> data (usually using chunked encoding) and thus content length  is
> not known at the time when response headers are being sent.

Hmm... I might be flushing the buffer explicitly. That's probably a
stupid idea because Tomcat should ultimately flush everything for me.
I'll check on that.

> What generates your response? A servlet, a filter, or a JSP?

All servlets in these cases. I'll follow-up with better answers to the
above.

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRUgHzAAoJEBzwKT+lPKRYjp8QAJmDS7eebC0S8tM+yG0C+PTy
tyczL2vaxu1bwLlt5LDwFwQ9q6S7NtPvGQiRIGqg3VDWz9H2q3UVDRR3v/zhyxdE
ZPutdXWRPPmU26ZyEF4W3ttyxGmtm+y4zpAkUfLW/h0Zl9gVDV6TqwYX9GsylEyV
/RrAN4AHKh2uBJc6SnV61wU5a+7LrBTy9Tlxi0om7UGf8sGoXTGDtmEpYYUfJDQu
+cUN3OTaOStdBXR+gLQ17NSlY+/nTZVoMMIfrJHTVBjAWpAQQwAXCZMaCfmdbaYS
EkNk8cehmnHyhYhy6JckbpC1qaugDwVERaOl86LcuSsKFzPAdFaoHS+XF6Qp2GDk
gtiw6wpTiM6DF6f2BP6ZXJLcddmtZMhtygGsKLXyxmR9hguOKVioVPIi5GDnifZR
0PtTjmY1dis/Xcfe/WKIKJER8jRtiBpzLHOLpomFh95hL1y7dnfRUYv9EzlrtV1k
TdQurstjKsd7P7XWUsSpyyjXmluCWZQoCJbzVwEdMMmar6kfaAGqgCj4FAHV/JTH
wNKRqd461q1hj/dKKJk65KbyKSFP2y4zpF/irtVvH4y0JyNAJWzmxdC8XJHu3EAp
Q9UsvPuZNtICR3twPoFzicJm1wXLV00TCg7XYiXExNZarDnzA6GAtOVMx5Oxmuaq
osWzVzmulIbdy9enhTZR
=AVzf
-----END PGP SIGNATURE-----

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


Re: Adding Content-Length response header

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

Konstantin,

On 3/26/13 11:08 AM, Konstantin Kolinko wrote:
> 2. Are you interested in GET/POST requests, or HEAD requests?
> 
> HEAD requests were fixed by this commit, from 5 days ago 
> http://svn.apache.org/r1459087

I am dealing almost entirely with POST requests.

> 3. Tomcat is already able to provide Content-Length header when
> all response fits into a buffer.
> 
> But, if an explicit flush() is called, Tomcat has to start sending 
> data (usually using chunked encoding) and thus content length  is
> not known at the time when response headers are being sent.

It seems I am flushing the buffer twice: once after emitting the XML
header (processing instruction and document element) and then once
again just before the end of the doPost method.

I'll try removing those to see if I can get a Content-Length in the
response. If that works, I'll do a bunch of testing to see if the lack
of flush presents a problem: I want my servlet to handle all
interaction with the client -- that is, the container shouldn't
respond with text/plain or text/html or anything like that. I should
be handling all exceptions except for IOExceptions writing to the
OutputStream, so that probably isn't a problem.

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRVNi5AAoJEBzwKT+lPKRYc0wP/2S57Ac0RvBSDxUb8UAYUUom
mE5Y3q/JsyKMBeoM4n6KXfyuIwbYFsCeBPbi+d5WolqV8C9LvHD6rDpIEAMd+TUX
ZWoA2uivNvDoG9o6Kw53OfNaWwxjWnUVsRLnekp5QbnbABxo+3TeGPii2ErU0Btf
e5RAkkRtyotzuVFmGKn+/HpbMUhq0z4WZD0sHH/nFbQRNZjK+E7u8UrW/m/zTufm
YJJcKV7hF8VcndYT35qsicA7djxQ+KXXpDHr92nCZ5BVWNygxtzTwFO34+7Hed52
8qq0IjO2tdmOMc48EBVxckTFJiEWzZkLrU5dem62Xcf1wmQTrsMLIeKdGxR03e3h
ZWSD7NO5lTA4keX2EbTWJMxdgnf6R7R7HJ5r3QrdUaffgPFesY9u05YJMYivoHvS
LqTB7l3kw3BR4xhoF1tWQNUxS5i04ApeX1ShmSH+81/cwmAz0OIEpzMPl/2Mic5i
z8zpQQ5oEx8GBSRru4cZHtZHPJzzIRwQU5XKzEFECZ8sNgKxUALKyJI3+9hKO3X7
maCQJ0q1E9maETPWFgQQBJli/waRJ5KBXLZp072V26z7uWRuxDeXptiLY+MRuzLu
e2lcK70V/n7MYUPVnbXi5B30QymGLkdB9MemfyEc4qdYD1UhrgeyIcOcnm2D/mT1
oleOI/I98f+lOCZAF+P+
=wx+V
-----END PGP SIGNATURE-----

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


Re: Adding Content-Length response header

Posted by Konstantin Kolinko <kn...@gmail.com>.
2013/3/26 Christopher Schultz <ch...@christopherschultz.net>:
>
> Like most folks on the list, I have quite a few dynamic resources
> being served through Tomcat. For nearly all of them, the
> Content-Length response header is /not/ included in the response
> because either content length isn't known when the response is
> committed, and therefore the Content-Length header simply cannot be
> sent or the response is not being buffered in the application, and so
> the application isn't counting bytes and doesn't know the content
> length even if it could be reported.
>
> I'm wondering if Tomcat can do anything about that.
>
> Here's the situation I have: I've got a response that I'm fairly sure
> fits into the response's buffer size, and I'd like to send a
> Content-Length header in that case. I could probably put a wrapper
> around the response's ServletOutputStream that counts bytes and then
> looks for "done" conditions (OutputStream.close, etc.), then adds a
> Content-Length header if the response hasn't yet been committed.
>
> I wonder if Tomcat could do something like that more easily -- since
> the buffer is being managed by Tomcat already, and Tomcat knows when
> the response will be committed/completed.
>
> So,
>
> A: Is this something that Tomcat can do already? I don't see any
> configuration options that sound like they would do this kind of thing.
>
> B: Is this something appropriate for a container to do in general?
>
> C: If I were to write a Filter to accomplish this manually, are there
> any potential dangers if a traditional non-synchronous
> request/response is being processed?

1. Your Tomcat version = ?

2. Are you interested in GET/POST requests, or HEAD requests?

HEAD requests were fixed by this commit, from 5 days ago
 http://svn.apache.org/r1459087

3. Tomcat is already able to provide Content-Length header when all
response fits into a buffer.

But, if an explicit flush() is called, Tomcat has to start sending
data (usually using chunked encoding) and thus content length  is not
known at the time when response headers are being sent.

What generates your response? A servlet, a filter, or a JSP?

Best regards,
Konstantin Kolinko

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


RE: Adding Content-Length response header

Posted by Leo Donahue - RDSA IT <Le...@mail.maricopa.gov>.
>-----Original Message-----
>From: Christopher Schultz [mailto:chris@christopherschultz.net]
>Subject: Adding Content-Length response header
>
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA256
>
>All,
>
>Here's the situation I have: I've got a response that I'm fairly sure fits into the
>response's buffer size, and I'd like to send a Content-Length header in that
>case. I could probably put a wrapper around the response's
>ServletOutputStream that counts bytes and then looks for "done" conditions
>(OutputStream.close, etc.), then adds a Content-Length header if the
>response hasn't yet been committed.

What about using a ByteArrayOutputStream?  
It has a size method and you could use that in the response.setContentLength()  and write out the ByteArrayOutputStream to your ServletOutputStream?  
Maybe?

>
>Thanks,
>- -chris