Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to limit post size in tomcat 8? How to make a custom reply for it?

I have this configured in my server.xml file

<Connector URIEncoding="UTF-8" connectionTimeout="20000" maxHttpHeaderSize="65536" **maxPostSize="1024"** port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

However, requests more than 1 KB pass as if maxPostSize is never set. Can anyone suggest what can cause that?

Another thing, I would like to know how to make a custom http reply from tomcat if the post size is more than 1 KB

UPDATE Since I have been for so long in this issue. I had the opportunity to look at the source code for tomcat to check what is happening here exactly: click here

I noticed from lines 2541 till 2550, they are using getContentLength() although the documentation says "maxPostSize: The maximum size in bytes". How come could this be in bytes? It looks more to characters count to me and it can be done in servlet side. Can someone explain what I am missing here?

like image 751
Khaled Ahmed Sobhy Avatar asked Feb 26 '17 15:02

Khaled Ahmed Sobhy


People also ask

What is maxPostSize in Tomcat?

maxPostSize. The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

What is maxHttpHeaderSize?

maxHttpHeaderSize. The maximum size of the request and response HTTP header, specified in bytes. If not specified, this attribute is set to 8192 (8 KB). maxKeepAliveRequests. The maximum number of HTTP requests which can be pipelined until the connection is closed by the server.

What is the default header size in Tomcat?

In a Spring Boot application, the max HTTP header size is configured using server. max-http-header-size. The actual default value for Tomcat and Jetty is 8kB, and the default value for Undertow is 1MB.

What is Tomcat acceptCount?

acceptCount -- The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.


1 Answers

According to tomcat doc the maxPostSize is 2M.

The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.

If you would to change the default values then change in the file location below:

$CATALINA_HOME/conf/server.xml

Example:

<Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
                maxPostSize="6291456" />

The size in bytes 6291456*(1024*1024)=6M

Note: Please make sure you restart the server after making the changes.

like image 58
Bhuwan Gautam Avatar answered Sep 29 '22 04:09

Bhuwan Gautam