简体   繁体   English

持久性HTTP客户端一次可以发送多个请求吗?

[英]Can an persistent HTTP client send more than one request at a time?

I am writing a HTTP proxy server and I noticed that many clients use the "Connection: Keep-Alive" header to keep a persistent connection. 我正在编写一个HTTP代理服务器,我注意到许多客户端使用“Connection:Keep-Alive”标头来保持持久连接。 Is it possible that the client sends another HTTP request before the server processes the first? 客户端是否可能在服务器处理第一个HTTP请求之前发送另一个HTTP请求?

For example, the client sends "GET / HTTP/1.1" but before the server has a chance to respond, the client sends "GET /favicon.ico HTTP/1.1". 例如,客户端发送“GET / HTTP / 1.1”,但在服务器有机会响应之前,客户端发送“GET /favicon.ico HTTP / 1.1”。 Is that possible? 那可能吗? Or will the client pause for the response before sending the second request? 或者客户端会在发送第二个请求之前暂停响应吗?

Also, when using a persistent connection, is it safe to assume all requests through that connection will have the same "Host: " header? 此外,在使用持久连接时,是否可以安全地假设通过该连接的所有请求都具有相同的“Host:”标头?

"Also, when using a persistent connection, is it safe to assume all requests through that connection will have the same "Host: " header?" “另外,当使用持久连接时,假设通过该连接的所有请求都具有相同的”Host:“标题是否安全?”

I don't think so, see HTTPbis P1, Section 2.2 : 我不这么认为,请参阅HTTPbis P1,第2.2节

Recipients MUST consider every message in a connection in isolation; 收件人必须孤立地考虑连接中的每条消息; because HTTP is a stateless protocol, it cannot be assumed that two requests on the same connection are from the same client or share any other common attributes. 因为HTTP是无状态协议,所以不能假设同一连接上的两个请求来自同一客户端或共享任何其他公共属性。 In particular, intermediaries might mix requests from different clients into a single server connection. 特别是,中介可能会将来自不同客户端的请求混合到一个服务器连接中。 Note that some existing HTTP extensions (eg, [RFC4559]) violate this requirement, thereby potentially causing interoperability and security problems. 请注意,某些现有的HTTP扩展(例如,[RFC4559])违反了此要求,从而可能导致互操作性和安全性问题。

Yes, it is possible for the client to pipeline requests. 是的,客户端可以管道请求。 (See http://en.wikipedia.org/wiki/HTTP_pipelining ). (见http://en.wikipedia.org/wiki/HTTP_pipelining )。

Turning your last question around... it would not be safe for a client to assume that requests to multiple hosts would be served by a single pipeline. 转动你的最后一个问题......对于一个客户来说,假设对一个管道提供多个主机的请求是不安全的。 There may be no specs that directly address your question on the Host: header, but it's a safe bet they'll be the same. 可能没有直接在Host:标题上解决您的问题的规范,但可以肯定它们将是相同的。

Regarding the first question: 关于第一个问题:

Is it possible that the client sends another HTTP request before the server processes the first? 客户端是否可能在服务器处理第一个HTTP请求之前发送另一个HTTP请求?

I believe that yes, it can be possible (perhaps I am wrong, I remembered having read that a couple of years ago; the definitive answer is in the HTTP protocol specifications). 我相信是的,它是可能的(也许我错了,我记得几年前已经阅读过;最终的答案是在HTTP协议规范中)。 But I don't understand why you are asking. 但我不明白你为什么这么问。 Also, the client can open several TCP connections at once to the same HTTP server. 此外,客户端可以同时打开几个TCP连接到同一个HTTP服务器。 And of course you have many simultaneous clients. 当然,你有许多同时发生的客户。

About the second question 关于第二个问题

Also, when using a persistent connection, is it safe to assume all requests through that connection will have the same "Host: " header? 此外,在使用持久连接时,是否可以安全地假设通过该连接的所有请求都具有相同的“Host:”标头?

I believe it is usually the case, but I won't assume that to be certain. 我相信情况通常如此,但我不认为这是肯定的。 I could imagine that some clever HTTP clients, recognizing that two URL with different Host: headers share the same IP, could re-use the same connection. 我可以想象一些聪明的HTTP客户端,认识到具有不同Host:头的两个URL共享相同的IP,可以重用相同的连接。

But I don't understand why you are asking. 但我不明白你为什么这么问。 Persistent HTTP connections have been invented to minimize the TCP connections which are costly, and the two questions you are asking are an extreme point on that. 已经发明了持久的HTTP连接以最小化TCP连接,这是非常昂贵的,并且您要问的两个问题是极端的。 Perhaps few HTTP clients are doing what you describe today. 也许很少有HTTP客户端正在执行您今天所描述的内容

And you should be strict on what you send (wrt standard conformance), but flexible on what you accept receiving. 你应该严格对待你发送的内容(符合标准的一致性),但要接受你接受的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM