Spurious 400 errors with requests that have a body (POST, PUT, etc.)

36 views
Skip to first unread message

Seth Webster

unread,
Nov 20, 2018, 10:27:25 AM11/20/18
to Nginx-Clojure
Hi,

I have been seeing extra 400 errors coming from NGINX when using Clojure.  I put together a simple Java rewrite handler as follows:

    @Override
    public Object[] invoke(Map<String, Object>  request) {
        NginxJavaRequest nginxJavaRequest = (NginxJavaRequest) request;
        String uri = nginxJavaRequest.getVariable("request_uri");

        if (uri.endsWith("error")) {
            return new Object[]{401, ArrayMap.create("www-authenticate", "Basic realm=\"Secure Area\""),
                    "<HTML><BODY><H1>401 Unauthorized.</H1></BODY></HTML>"};
        } else {
            return PHASE_DONE;
        }
    }

If I hit the "error" endpoint with a POST or PUT, then I will get both an HTTP 401 and an HTTP 400.  

I believe what is happening is that the header of the request is being passed to the rewrite handler and this causes the 401 as expected.  However, the body of the request is not consumed.  NGINX then sees the body as a new request.  Obviously the body is not formatted as a proper request, so a 400 is generated.  This may only be an issue if keepalive connections are being used.

Is there a way to cause the body to be discarded in the event of an error so that it doesn't produce a spurious 400?  The only way I have found is to set "always_read_body on;".  However, this has a performance overhead that we would like to avoid.

Thanks,
Seth

Yuexiang Zhang

unread,
Nov 25, 2018, 8:16:27 AM11/25/18
to sethweb...@gmail.com, nginx-...@googlegroups.com
Hi,

The lastest commit fixed this issue by adding a new method request.discardRequestBody() .
Please try it. e.g.

if (uri.endsWith("error")) {
   request.discardRequestBody();
   .....
}

Thanks.
xfeep


--
You received this message because you are subscribed to the Google Groups "Nginx-Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nginx-clojur...@googlegroups.com.
To post to this group, send email to nginx-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nginx-clojure/452e1bbb-7c06-4e2b-aef6-b7d9c682dedd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Seth Webster

unread,
Nov 26, 2018, 9:37:45 AM11/26/18
to Nginx-Clojure
Thank you for the prompt response!

I will try it out.  Do you have any plans to issue a release with this fix in it in the near future?

Thanks,
Seth
Reply all
Reply to author
Forward
0 new messages