Jetty HTTP 413 Header Full error - Java/Scala

Ali Salehi picture Ali Salehi · Aug 11, 2012 · Viewed 16.6k times · Source

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.

Answer

Sylvain picture Sylvain · Aug 14, 2012

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>