[英]Node.js HEAD request returns HPE_INVALID_CONTENT_LENGTH error
使用請求模塊,我對一些縮短的301重定向URL的HEAD請求遇到以下錯誤:
{ [Error: Parse Error] bytesParsed: 123, code: 'HPE_INVALID_CONTENT_LENGTH' }
例如,我在http://cnb.cx/1vtyQyv上獲得了它。 非常容易復制(節點v0.10.29,請求v2.36.0):
var request = require('request');
request({ url:'http://cnb.cx/1vtyQyv', method: 'HEAD' }, function(err, res) {
console.log(err, res);
});
這是此URL上的curl
HEAD請求的結果:
$ curl -I http://cnb.cx/1vtyQyv
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 02 Jul 2014 18:16:05 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Cache-Control: private; max-age=90
Content-Length: 124
Location: http://www.cnbc.com/id/101793181
Mime-Version: 1.0
Set-Cookie: _bit=53b44c65-00194-0369a-281cf10a;domain=.cnb.cx;expires=Mon Dec 29 18:16:05 2014;path=/; HttpOnly
身體上的內容長度實際上是124,可以用curl http://cnb.cx/1vtyQyv | wc -c
驗證curl http://cnb.cx/1vtyQyv | wc -c
curl http://cnb.cx/1vtyQyv | wc -c
該錯誤是從Node.js的核心http解析器( https://github.com/mattn/http-server/blob/master/http_parser.c )中引發的,但是奇怪的是, request
能夠遵循此301重定向並成功進行GET請求時,返回目標頁面的內容( http://www.cnbc.com/id/101793181 )沒有錯誤,這表明該錯誤不是必需的:
var request = require('request');
request({ url:'http://cnb.cx/1vtyQyv', method: 'GET' }, function(err, res) {
console.log(err, res);
});
這是使用node-unshortener的問題,它會重復發送HEAD請求,直到找到完整的URL。
它適用於我的純節點v0.10.29:
var http = require('http');
http.request({
host: 'cnb.cx',
path: '/1vtyQyv',
method: 'HEAD'
}, function(res) {
console.dir(res);
res.resume();
}).end();
該錯誤是通過request
v2.36.0復制的。 您可能要提出有關此問題 。
更新:錯誤是用純節點重現的,問題不是縮短的URL,而是導致問題的重定向URL:
http.request({
host: 'www.cnbc.com',
path: '/id/101793181',
method: 'HEAD'
}, function(res) {
console.dir(res.statusCode);
console.dir(res.headers);
}).end();
// results in:
//
// events.js:72
// throw er; // Unhandled 'error' event
// ^
// Error: Parse Error
// at Socket.socketOnData (http.js:1583:20)
// at TCP.onread (net.js:527:27)
更新2:事實證明,重定向的URL返回Content-Length: -1
,這導致了錯誤。 curl -I http://www.cnbc.com/id/101793181
顯示:
HTTP/1.1 200 OK Date: Wed, 02 Jul 2014 22:23:49 GMT Server: Apache Vary: User-Agent Via: 1.1 aicache6 Content-Length: -1 X-Aicache-OS: 10.10.1.25:80 Connection: Keep-Alive Keep-Alive: max=20
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.