6

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?

Khaled Ahmed Sobhy
  • 353
  • 2
  • 5
  • 16
  • Are you sure you are testing with POST request ? (and not a GET) – Thomas Lehoux Feb 26 '17 at 16:26
  • yes, I'm sure as tomcat logs show: 127.0.0.1 - - [27/Feb/2017:10:23:25 +0200] "POST /link/Link HTTP/1.1" 200 210 So it is definitely post. I'm sending very large to make sure it exceeds 1KB also. – Khaled Ahmed Sobhy Feb 27 '17 at 08:24
  • Did you try with a very low configuration : `maxPostSize="1"` for example, to see if you're configuration is taken into account ? – Thomas Lehoux Feb 27 '17 at 08:50
  • I tried with maxPostSize="1" now and the request still passed. I removed the whole configuration connector line to make sure I'm editting in the right place. The link gave me connection refused so I'm definitely editing in the right place. – Khaled Ahmed Sobhy Feb 27 '17 at 09:10
  • Are you sure the request you're sending has `application/x-www-form-urlencoded`as Content-Type ? – Thomas Lehoux Feb 27 '17 at 09:15
  • Here's the header data from the client side: POST http://localhost:8080/link/Link HTTP/1.1 Accept-Encoding: gzip,deflate Content-Type: application/x-www-form-urlencoded Content-Length: 44503 Host: localhost:8080 Connection: Keep-Alive User-Agent: Apache-HttpClient/4.1.1 (java 1.5) I was able to get these info as I used soapui to make the post. I also try from browser but no good. I tried both application/x-www-form-urlencoded & text/xml;charset=UTF-8 – Khaled Ahmed Sobhy Feb 27 '17 at 09:29
  • I also tried with tomcat 7 but I had the same problem. This is so weird. – Khaled Ahmed Sobhy Feb 27 '17 at 09:40

2 Answers2

5

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.

Bhuwan Gautam
  • 1,229
  • 1
  • 11
  • 24
0

By default, HTTP containers use inputstream of HTTP request object and process the data as bytes. If the request has any encoding , it will use character based reader object .
https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletRequest.html#getInputStream()

While sending response also, the content length are set in bytes. You set the content length as bytes and either write as character or bytes.

Servers will handle the data as bytes