2011-06-03 113 views
8

该代码是否为有效的HTTP/1.1?Node.js:分块传输编码

var fs = require('fs') 
var http = require('http') 

var buf=function(res,fd,i,s,buffer){ 
if(i+buffer.length<s){ 
    fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){ 
    res.write(b.slice(0,l)) 
    //console.log(b.toString('utf8',0,l)) 
    i=i+buffer.length 
    buf(res,fd,i,s,buffer) 
    }) 
} 
else{ 
    fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){ 
    res.end(b.slice(0,l)) 
    fs.close(fd) 
    }) 
} 
} 

var app = function(req,res){ 
var head={'Content-Type':'text/html; charset=UTF-8'} 
switch(req.url.slice(-3)){ 
    case '.js':head={'Content-Type':'text/javascript'};break; 
    case 'css':head={'Content-Type':'text/css'};break; 
    case 'png':head={'Content-Type':'image/png'};break; 
    case 'ico':head={'Content-Type':'image/x-icon'};break; 
    case 'ogg':head={'Content-Type':'audio/ogg'};break; 
    case 'ebm':head={'Content-Type':'video/webm'};break; 
} 
head['Transfer-Encoding']='chunked' 
res.writeHead(200,head) 
fs.open('.'+req.url,'r',function(err,fd){ 
    fs.fstat(fd,function(err, stats){ 
    console.log('.'+req.url+' '+stats.size+' '+head['Content-Type']+' '+head['Transfer-Encoding']) 
    var buffer = new Buffer(100) 
    buf(res,fd,0,stats.size,buffer) 
    }) 
}) 
} 

http.createServer(app).listen(8000,"127.0.0.1") 
console.log('GET http://127.0.0.1:8000/appwsgi/www/index.htm') 

我想我在这里违反了HTTP/1.1?文本文件似乎工作正常,但这可能是巧合。我的标题是“200 OK”还是需要它为“100”?一个头是否足够?

回答

11

如果你正在做块传输编码,您实际需要设置标题:

Transfer-Encoding: chunked

您可以从由谷歌返回的头,它不转让分块的网页,并很可能会看到其他网页:

HTTP/1.1 200 OK 
Date: Sat, 04 Jun 2011 00:04:08 GMT 
Expires: -1 
Cache-Control: private, max-age=0 
Content-Type: text/html; charset=ISO-8859-1 
Set-Cookie: PREF=ID=f9c65f4927515ce7:FF=0:TM=1307145848:LM=1307145848:S=fB58RFtpI5YeXdU9; expires=Mon, 03-Jun-2013 00:04:08 GMT; path=/; domain=.google.com 
Set-Cookie: NID=47=UiPfl5ew2vCEte9JyBRkrFk4EhRQqy4dRuzG5Y-xeE---Q8AVvPDQq46GYbCy9VnOA8n7vxR8ETEAxKCh-b58r7elfURfiskmrOCgU706msiUx8L9qBpw-3OTPsY-6tl; expires=Sun, 04-Dec-2011 00:04:08 GMT; path=/; domain=.google.com; HttpOnly 
Server: gws 
X-XSS-Protection: 1; mode=block 
Transfer-Encoding: chunked 

编辑哎呀,那读的方式过于复杂:

var app = function(req,res){ 
var head={'Content-Type':'text/html'} 
switch(req.url.slice(-3)){ 
    case '.js':head={'Content-Type':'text/javascript'};break; 
    case 'css':head={'Content-Type':'text/css'};break; 
    case 'png':head={'Content-Type':'image/png'};break; 
    case 'ico':head={'Content-Type':'image/x-icon'};break; 
    case 'ogg':head={'Content-Type':'audio/ogg'};break; 
    case 'ebm':head={'Content-Type':'video/webm'};break; 
} 
res.writeHead(200,head) 
var file_stream = fs.createReadStream('.'+req.url); 
file_stream.on("error", function(exception) { 
    console.error("Error reading file: ", exception); 
}); 
file_stream.on("data", function(data) { 
    res.write(data); 
}); 
file_stream.on("close", function() { 
    res.end(); 
}); 
} 

你走了,一个漂亮的流缓冲区供你写。 这是我写的一篇博文,以不同的方式读取文件。我建议您仔细观察,以便您能够看到如何在节点的异步环境中更好地使用文件。

+0

这是令人困惑的,因为在发送分块软件包时,响应是否需要为200? – 2011-06-04 00:20:07

+0

@Gert从谷歌阅读标题我发布在我的回复中,其中有200 OK。当然,如果你正在重定向,或者“是的,我现在有这个文件在这里,然后将它发送给你”类型的情况之外,你应该使用适当的头。我建议看看所有的头文件和它们代表什么:http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html。如果你想继续像这样在一个低级别上使用node.js,这将对你有益。 – 2011-06-04 00:25:48

3

你为什么要手动执行所有fs操作?使用fs.createReadStream()函数可能会更好。

最重要的是,我的猜测是,Chrome期待您返回206响应代码。检查req.headers.range,并查看Chrome是否期待要返回媒体文件的“范围”。如果是这样,那么你只需要发回网络浏览器所请求的文件部分。

为什么要重新发明轮子?有大量的节点模块为你做这种事情。尝试Connect/Express'中间件'static。祝你好运!

+1

修复代码后,它只播放媒体一次。我想你对范围反应是正确的。 – 2011-06-05 01:44:56

9

由于Node.js的隐式设置“传输编码:分块”,所有我需要在头送了内容类型与字符集,如:

'Content-Type': 'text/html; charset=UTF-8' 

最初,它是:

'Content-Type': 'text/html' 

哪个没有工作。指定“charset = UTF-8”立即强制Chrome呈现分块响应。

+1

与我的代码的显式字符集工作,但我注意到,它不适用于文本/普通内容类型(在铬上测试)......奇怪的行为... – 2012-10-16 12:23:30

+0

我不确定你为什么有这种行为。我会测试这个和稍后回复。 – Eye 2012-10-22 12:33:48

+0

在我的测试中它可以在Firefox上使用,但不能在Chrome上使用(仅适用于文本/纯文本内容类型)... – 2012-10-22 17:11:07