Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServletResponse.setBufferSize doesn't work in Tomcat 7?

I'm increasing the buffer size of a response using ServletResponse.setBufferSize but Tomcat 7 is still throwing an exception that the buffer size is not big enough. Is this a bug in Tomcat 7.0.32?

Here is my pseudo/code -

@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage)
    throws IOException, HttpMessageNotWritableException {

    if (some condition)
    {
        ((ServletServerHttpResponse)outputMessage).getServletResponse().setBufferSize(Integer.MAX_VALUE);
        outputMessage.getHeaders().set("Custom-Header", gson.toJson(big payload));
    }

    // ...

This is the exception -

org.apache.coyote.http11.HeadersTooLargeException: An attempt was made to write more data to the response headers than there was room available in the buffer. Increase maxHttpHeaderSize on the connector or write less data into the response headers.

Do I need to reset the buffer? reset() did not work.

EDIT: I'm looking for the ability to change the maximum header size at the individual response level based upon "some condition".

like image 901
Josh Unger Avatar asked Feb 07 '14 22:02

Josh Unger


2 Answers

The message is about header size, not total buffer size. From the Tomcat Configuration Guide section on "The HTTP Connector"

maxHttpHeaderSize

The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 8192 (8 KB).

like image 165
Jim Garrison Avatar answered Nov 09 '22 17:11

Jim Garrison


What you are doing

You are setting the size of the buffer for writig repsonse body. It has nothing to do with the response headers. This is mainly used for flushing responses once the buffer size is reached.

What you need to do

You need to set the maxHttpHeaderSize property in Tomcat Configuration

like image 2
Ramesh PVK Avatar answered Nov 09 '22 18:11

Ramesh PVK