Jetty HTTP 413 Header Full error - Java/Scala

16,876

Solution 1

Unfortunately, this is not only headers that matters (like joakime thougth). Jetty has a buffer for headers and a buffer for request.

  • If the full request (http data stream) fits in the hearder's buffer no problem.
  • If it exceeds the header's buffer, the request buffer will be user.
  • If it exceeds the request buffer then you got a standard Http response with status 413.

There is the same thing (buffer) for the answer but hopefully Http is designed to send "chunked" response.

I'm facing the same problem with an upload.

What I've found is that you can set the size of those buffers. See: http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/nio/SelectChannelConnector.html

http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/nio/AbstractNIOConnector.html

http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/AbstractConnector.html#setRequestHeaderSize(int)

You can use jetty.xml file to do it :

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"     "http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
 <Call name="addConnector">
  <Arg>
   <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
    <Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>
    <Set name="requestHeaderSize">8192</Set>
   </New>
  </Arg>
 </Call>
</Configure>

Solution 2

HTTP response code 413 is HttpStatus.REQUEST_ENTITY_TOO_LARGE.

It has nothing to do with your HttpServletResponse.getWriter().

Capture and check the request headers, you are sending a unreasonable amount of headers to the server.

Solution 3

If you're using or upgraded to Jetty 9, they've removed the SelectChannelConnector. Here is the updated jetty.xml -

<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
  <Set name="requestHeaderSize">8192</Set>      
</New> 

<Call name="addConnector">
  <Arg>
    <New class="org.eclipse.jetty.server.ServerConnector">
      <Arg name="server"><Ref id="Server" /></Arg>
      <Arg name="factories">
        <Array type="org.eclipse.jetty.server.ConnectionFactory">
          <Item>
            <New class="org.eclipse.jetty.server.HttpConnectionFactory">
              <Arg name="config"><Ref id="httpConfig" /></Arg>
            </New>
          </Item>
        </Array>
      </Arg>
      <Set name="port">8080</Set>
    </New>
  </Arg>
</Call>

See http://www.eclipse.org/jetty/documentation/current/configuring-connectors.html

Share:
16,876
Ali Salehi
Author by

Ali Salehi

Updated on June 05, 2022

Comments

  • Ali Salehi
    Ali Salehi about 2 years

    I am using Jetty 7.6 with Scalatra web framework. In some of the requests, I need to send a large text as response body to the client, I use HttpServletResponse.getWriter() to write response.

    I noticed that on client side I receive 413 Header Full error. Apparently one solution to this problem in Jetty is to increase jetty's header-buffer-size value.

    I would like to know what does HttpServletResponse.getWriter() has to do with the size of the header of request ?! As I understand HttpServletResponse.getWriter() writes into response body rather than response header.

    I appreciate if someone could explain this issue.