Search code examples
javahttpnetwork-programmingjetty

How to cancel reading request with Jetty?


I am using Jetty 8 in blocking I/O mode. The servlet doPost reads the request content from the request InputStream (which is actually an instance of Jetty HttpInput).

What if a client sends a very large request and does it very slowly ? Obviously, it will take a lot of time to read the request. So, I would like to cancel the reading after a few seconds and send an error response to the client. How can I do that without much changes i.e. using Jetty with blocking I/O and without continuation ?


Solution

  • Jetty 9+ answer

    Use HttpServletResponse.sendError(-1) to terminate the connection.

    Original Jetty 8 answer

    Write a ServletFilter that does the detection of the slow request. When you determine it is slow, ...

    Also, look into the QoSFilter, it does a lot of this kind of stuff already.