[http]未指定content-length时如何识别流结束?

[http]How to identify end of stream when content-length is not specified?

首先让我展示一下我的代码。

http=require("http");
fs=require("fs");
var server=http.createServer(function(req,res){
    var readStream=fs.createReadStream("1.jpg");
    readStream.on("data",function(data){
        res.write(data);
    });
    readStream.on("end",function(data){
        res.write("this string seems not to be sent","utf8");
        res.end("end","utf8");
    });
});

server.listen(3000);

我创建了图片的readStream 1.jpg 然后发送了数据流。 "end" 事件触发后,我发送了一个字符串 "this string seems not to be sent"。我没有在 headers 中指定 content-length。

在客户端,我实际上 1.jpg 正确。但是我没有收到字符串。我想一定有什么东西标志着流的结束。如果有,标记是什么?它是如何工作的?

我知道用 "chunked" 分配 transfer-encoding 是一种发送长度不确定的数据的方法,但我的 safari 显示响应 headers 是:

连接keep-alive
Transfer-Encoding身份

On the client side, I actually got 1.jpg correctly. But I didn't receive the string.

实际上,字符串发送的。确认这一点:

$ echo '(Contents of a JPEG file.)' >1.jpg

$ curl -i http://localhost:3000/
HTTP/1.1 200 OK
Date: Sat, 23 May 2015 08:01:48 GMT
Connection: keep-alive
Transfer-Encoding: chunked

(Contents of a JPEG file.)
this string seems not to be sentend

您的浏览器(或图像查看器)理解 JPEG 格式,因此它会忽略末尾的额外字符串。然而,它被发送了。

I guess there must be something that marks the end of stream.

是的。数据由 chunked transfer encoding 标记分隔。 curl 默认情况下不显示它们,但它们存在。要在回复中查看它们:

$ curl -i --raw http://localhost:3000/
HTTP/1.1 200 OK
Date: Sat, 23 May 2015 08:23:02 GMT
Connection: keep-alive
Transfer-Encoding: chunked

1b
(Contents of a JPEG file.)

20
this string seems not to be sent
3
end
0