Search code examples
embedded-jettyjetty-9

How to produce a custom error response for 414 URI Too Large


I recently switched from Tomcat to Jetty and since then, I can see this kind of WARN in my logs.

WARN org.eclipse.jetty.http.HttpParser - URI is too large >65535

WARN org.eclipse.jetty.http.HttpParser - bad HTTP parsed: 414 for HttpChannelOverHttp@34ff28{r=0,c=false,a=IDLE,uri=null}

I am looking for a way to change the response returned when this happens (I would prefer a json response over the HTML one that is returned by default) and not log a warning. Is this something that can be done? After some reseach, it seems that this error is logged before any place were I could inject some code to handle this (like a servlet filter for exemple)


Solution

  • This is the category of errors known as "Bad Request" and occur very early in the processing of the incoming request.

    Some examples of Bad Requests

    400 Bad Request              (common)
    412 Precondition Failed      (rather esoteric, rare)
    413 Request Entity Too Large (very common)
    414 URI Too Long             (very common)
    431 Requested Header Fields Too Large  (not produced by Jetty, yet)
    

    A Bad Request has no request URI, and consequently cannot be sent down to the context for that context to handle it.

    These kinds of bad requests also have no headers, so you couldn't even check the Accept header to make a determination if you should even send a response back as application/json

    The most common reasons people see these response codes:

    • Something is Port Scanning
    • A client is attempting to connect to your HTTP server, but isn't a HTTP client (eg: misconfigured email client)
    • A request is made from a badly implemented HTTP User-Agent
    • Someone is probing your server for vulnerabilities (of which there are many surrounding the overflow of various request metadata)
    • Use of your server (such as an API) is out of spec from what you have documented (eg: trying to send large documents as encoded query strings)