Is there a way to get a raw HTTP request stream from a Java servlet handler?

I am trying to put some entries in order to grab the raw HTTP request coming into my application. My Java code is inside the SpringMVC controller. I have access to the "HttpServletRequest" object. But I could not find a way to get a raw HTTP request from it. There is a reader, but reads only the content of the message. What I want is the whole shebang, address, headers, body. Is there an easy way to do this?

Thanks in advance.

+2
source share
3 answers

No, servlets do not provide api for receiving a raw request - for this you may need a sniffer, for example wirehark.

You can get the headers of the processed requests and uri:

and etc.

+3
source

No.

The servlet does not provide such an API, and it would be difficult to implement, because (basically) you cannot read the same data from Socket twice. It is easy to obtain header information, but source headers cannot be captured in the servlet container. To get bodies, you need to capture them yourself, as your application reads / writes the corresponding streams.

Your alternatives:

  • Write your own server-side implementation of the HTTP protocol. (May not be suitable for your application.)

  • You can get the header information you need with filters, although they do not show raw requests.

  • Some servlet containers have query logging; for example, with Tomcat there is a beast called RequestDumperValve, which you can configure in the file "server.xml".

  • Implement a proxy server that is located between the client and your "real" server.

  • Sniffing packages.

Which best depends on what you are really trying to achieve.

Followup:

If "badness" is in the headers, the RequestDumperValve approach is probably best for debugging. Go to $ CATALINA_HOME / conf / server.xml, find "RequestDumperValve" and uncomment the item. Then restart Tomcat. You can also make an equivalent in the webapp file "context.xml". Reset requests and responses end in "logs / catalina.out" by default. Please note that this will give a lot of results, so you do not want to do this in production ... except as a last resort.

If the error is related to the content of the POST or PUT request, you need to modify the application to save a copy of the content when it reads it from the input stream. I do not know any shortcuts for this.

Also, if you want to leave the log for a long period of time, you probably have to solve the problem yourself by calling the HttpServletRequest API and logging headers, etc. RequestDumperValve generates too large a result and discards ALL requests, not only bad ones.

+5
source

I was able to read my raw request in my web application deployed on Tomcat 5.5

All I had to do was read the HttpServletRequest through the / Spring servlet controller using request.getInputStream ().
This should be the first API approach to the request. before any filter or other command starts the mass with a request that causes it to be fully read by the web server.

What is the problem with this approach?

0
source

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