HTTP中的多个响应合法吗? [英] Are multiple responses legal in HTTP?
问题描述
我对HTTP协议的细微差别感到生锈,我想知道它是否可以直接支持发布/订阅?
I'm a bit rusty on nuances of the HTTP protocol and I'm wondering if it can support publish/subscribe directly?
HTTP是请求响应协议.因此,客户端发送一个请求,服务器发送回一个响应. 在HTTP 1.0中,为每个请求建立了新的连接. 现在,HTTP 1.1通过允许客户端保持连接打开并发出多个请求来对HTTP 1.0进行了改进.
HTTP is a request reponse protocol. So client sends a request and the server sends back a response. In HTTP 1.0 a new connection was made for each request. Now HTTP 1.1 improved on HTTP 1.0 by allowing the client to keep the connection open and make multiple requests.
我意识到您可以将HTTP连接升级到websocket,以进行快速的2向通信.我很好奇的是,这是否绝对必要?
I realise you can upgrade an HTTP connection to a websocket for fast 2 way communications. What I'm curious about is whether this is strictly necessary?
例如,如果我请求资源" http://somewhere.com/fetch/me/slowly "
For example if I request a resource "http://somewhere.com/fetch/me/slowly"
服务器是否可以直接直接回复两次? 如先用202接受 然后在准备好内容后不久, 但没有客户端先发送其他请求?
Is the server free to reply directly twice? Such as first with a 202 accepted and then shortly later with the content when it is ready, but without the client sending an additional request first?
即
Client: GET http://somewhere.com/fetch/me/slowly
Server: 202 "please wait..."
Server: 200 "here's your document"
以这种方式实现发布/订阅服务是否正确? 例如:
Would it be correct to implement a publish/subscribe service this way? For example:
Client: http://somewhere.com/subscribe
Server: item 1
...
Server: item 2
我觉得这种可能"有效,因为客户端通常会有一个事件循环监视连接,但是在技术上是错误的(因为遵循该协议的客户端不需要那样实现).
I get the impression that this 'might' work because clients will typically have an event loop watching the connection but is technically wrong (because a client following the protocol need not be implemented that way).
但是,如果您使用分块传输编码,则可以.
However, if you use chunked transfer encoding this would work.
HTTP/2似乎也允许这样做,但是我不清楚是否进行了某些更改以使其成为可能.
HTTP/2 seems to allow this as well but I'm not clear whether something changed to make it possible.
关于发布/订阅,我还没有看到太多讨论,那么如果使用带有或不带有分块编码的纯HTTP/1.1有什么问题怎么办?
I haven't seen much discussion of this in relation to pub/sub so what if anything is wrong with using plain HTTP/1.1 with or without chunked encoding?
如果这行得通,为什么您需要RSS或ATOM之类的东西?
If this works why do you need things like RSS or ATOM?
推荐答案
一个HTTP请求可以具有多个响应",但是响应都具有1xx
范围内的状态码,例如
A HTTP request can have multiple 'responses', but the responses all have statuscodes in the 1xx
range, such as 102 Processing.
但是,这些响应只是标题,而不是正文.
However, these responses are only headers, never bodies.
HTTP/1.1(例如之前的1.0)是请求/响应协议.不允许发送未经请求的响应. HTTP/2是一种框架协议,它添加了服务器推送,该协议允许服务器建议报价额外的数据并并行处理多个请求,但不会更改其请求/响应性质.
HTTP/1.1 (like 1.0 before it) is a request/response protocol. Sending a response unsolicited is not allowed. HTTP/2 is a frames protocol which adds server push which allows the server to suggest offer extra data and handle multiple requests in parallel but doesn't change its request/response nature.
可以保持HTTP连接打开并继续发送更多数据.许多(音频,视频)流服务都将使用此功能.
It is possible to keep a HTTP connection open and keep sending more data though. Many (audio, video) streaming services will use this.
但是,这看起来像是一个连续不断的主体,可以持续进行流传输,而不是许多多个HTTP响应.
However, this just looks like a continuous body that keeps on streaming, rather than many multiple HTTP responses.
如果这行得通,为什么您需要RSS或ATOM之类的东西
If this works why do you need things like RSS or ATOM
因为打开TCP连接不是免费的.
Because keeping a TCP connection open is not free.
这篇关于HTTP中的多个响应合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!