常见http协议实现的资源限制

586 阅读2分钟

1 url和http请求行长度限制

http协议本身并没有限制url的最大长度,但是浏览器和服务器实现http协议时对url长度分别有不同程度的限制。本人搜集的信息如下表所示:

URLRequest LineURL过长的结果
HTTP/1.1 标准推荐服务端至少支持8000字节
HTTP/2 标准无限制
URL、URI 标准无限制
Chromium2MB打开失败
Firefox1MB打开失败
Safari无限制
IE8~2083字节打开失败
IE9-IE11无限制
Node.js >= v13.13.016KB抛出Header overflow异常
Node.js < v13.13.08KB抛出Header overflow异常
nginx8KB返回 4xx 错误
httpd8190字节返回 4xx 错误

2 spring支持http协议时的一些常见资源限制

参数默认值含义
server.max-http-header-size8KBhttp头部最大长度
server.servlet.session.timeout30mhttp session超时时间,默认单位为秒
server.tomcat.accept-count100tomcat已连接tcp队列的最大长度
server.tomcat.connection-timeouttomcat tcp连接建立超时时间
server.tomcat.keep-alive-timeouttomcat 空闲tcp连接保活时间,超过该时间没有接收到新请求,将关闭连接
server.tomcat.max-connections8192tomcat最大tcp连接数
server.tomcat.max-http-form-post-size2MBtomcat http post请求表单body的最大大小
server.tomcat.max-http-response-header-size8KBtomcat http响应头最大大小
server.tomcat.max-keep-alive-requests100tomcat每条tcp连接的http请求队列的最大长度
server.tomcat.max-swallow-size2MBtomcat请求body和响应body的最大大小
server.tomcat.threads.max200tomcat处理请求的最大线程数
server.tomcat.threads.min-spare10tomcat处理请求的最小线程数

更多详细的spring参数配置请参考官方文档:Common Application Properties

3 http协议中关于http消息长度超出限制的响应码定义

  1. 413(Payload Too Large)

The 413 (Payload Too Large) status code indicates that the server is refusing to process a request because the request payload is larger than the server is willing or able to process. The server MAY close the connection to prevent the client from continuing the request.

If the condition is temporary, the server SHOULD generate a Retry-After header field to indicate that it is temporary and after what time the client MAY try again.

  1. 414(URI Too Long)

The 414 (URI Too Long) status code indicates that the server is refusing to service the request because the request-target (Section 5.3 of [RFC7230]) is longer than the server is willing to interpret. This rare condition is only likely to occur when a client has improperly converted a POST request to a GET request with long query information, when the client has descended into a "black hole" of redirection (e.g., a redirected URI prefix that points to a suffix of itself) or when the server is under attack by a client attempting to exploit potential security holes.

A 414 response is cacheable by default; i.e., unless otherwise indicated by the method definition or explicit cache controls (see Section 4.2.2 of [RFC7234]).

  1. 431(Request Header Fields Too Large)

The 431 status code indicates that the server is unwilling to process the request because its header fields are too large. The request MAY be resubmitted after reducing the size of the request header fields.

It can be used both when the set of request header fields in total is too large, and when a single header field is at fault. In the latter case, the response representation SHOULD specify which header field was too large.

4 参考资料

  1. URL最大长度限制探究