How to remove request body after checking headers in Servlet

I want to check the request header whether it contains a specific header or not before continuing with the body. For example, I want to check if multipart / form-data contains "Authorization" in the header or not. If this is not the case, there is no need to continue loading the multi-page body, which is usually large enough to download files.

Does the servlet allow this? I tried to do a Google search by accident, but there is no luck. Here is the code I'm trying to execute on my servlet, but it still continues with getting the body until this doPost called. It seems that the stream is fully received before the servlet is called.

 @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/plain"); if (request.getHeader("Authorization") == null) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); out.println("Status: " + HttpServletResponse.SC_UNAUTHORIZED + " - UNAUTHORIZED"); return; } // ... the rests } 
+2
source share
2 answers

This is a limitation of HTTP. You cannot send a response when the request has not been fully read.

+3
source

RFC 2616 says:

The HTTP / 1.1 client (or later) sending the message body MUST monitor the network connection for the error state while it is sending the request. If the client sees the error state, he MUST immediately stop the transfer of the body.

Therefore, I do not agree, this is not an HTTP restriction, but a servlet.

+1
source

Source: https://habr.com/ru/post/1333680/