视频请求中缺少范围标头? [英] Range header missing from video request?
问题描述
有时我的 Node.js 服务器上会出现错误,告诉我 req.headers.range
在用户请求 .mp4
时未定义(我没有尝试提供任何其他视频文件类型,不确定 mp4 是否相关).我不明白为什么缺少这个标题.
I sometimes get an error on my Node.js server telling me req.headers.range
is undefined when a user is requesting a .mp4
(I haven't tried serving any other video filetypes, not sure if mp4 is relevant). I don't understand why this header is missing.
视频的请求标头有时是否应该排除range
?
我像这样发送流:
var videoReqHandler = function(req, res, pathname) {
var file = dirPath + "/Client" + pathname;
var range = req.headers.range;
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
fs.stat(file, function(err, stats) {
if (err) {
throw err;
} else {
var total = stats.size;
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end - start) + 1;
res.writeHead(206, {
"Content-Range" : "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges" : "bytes",
"Content-Length" : chunksize,
"Content-Type" : "video/mp4"
});
var stream = fs.createReadStream(file, {
start : start,
end : end
}).on("open", function() {
stream.pipe(res);
}).on("error", function(err) {
res.end(err);
});
}
});
};
错误:
/video_stream.js:21
var positions = range.replace(/bytes=/, "").split("-");
TypeError: Cannot call method 'replace' of undefined
推荐答案
Range
标题是可选的:
使用条件或无条件 GET 方法的 HTTP 检索请求可以使用 Range 请求标头请求实体的一个或多个子范围,而不是整个实体(http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2)
HTTP retrieval requests using conditional or unconditional GET methods MAY request one or more sub-ranges of the entity, instead of the entire entity, using the Range request header (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2)
请求是否针对视频流并不重要.我假设浏览器(或视频播放器库)不会为初始请求发送 Range
标头,而仅在搜索过程中使用它.
It doesn't matter if the request is for a video stream or not. I would assume that a browser (or a video player library) doesn't send a Range
header for the initial request and only uses it during seeking.
这篇关于视频请求中缺少范围标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!