http.request中的nodeJS最大标头大小

pts*_*ts4 7 http-headers node.js

对于nodeJS v0.10.28,http请求中标头内容的大小/长度是否有限制?

让我解释:

我需要使用第三方提供商提供的休息服务.返回给我的数据是在请求的标题中,正文大部分是空的(120左右的字符).标题中的数据量从几个字符到几百字节不等.

var https = require('https');

var httpHeaders = {
    Authorization: 'Basic ' + new Buffer(user + ':' + psw).toString('base64'),
    accept: '*/*',
    'Content-Type': 'text/plain; charset=utf-8'
};
var options = {
    host: "www.website.com",
    port: 8080,            
    path: "/" ,   
    method: 'GET',
    headers: httpHeaders,
    rejectUnauthorized: false,
    requestCert: true,
    agent: false
};

https.request(options, function(res) {
    res.setEncoding('utf8');
    if (res.statusCode == 200) {
        var json = res.headers["someHeaderInfo"];
        callback(null,{ "result" : JSON.parse(json) });
    } else {
        callback({ "error" : res.statusCode  });                            
    }
}).on('data', function (chunk) {
    console.log('BODY: ' + chunk);
}).on('error', function(e, res) {
    console.log("  Got error: " + e.message);
    callback({ "error" : e.message });
}).end();
Run Code Online (Sandbox Code Playgroud)

上面的代码适用于较小尺寸的标题,但在on('error',在较大的标题上带有"Parse Error"消息时失败.

删除on error子句会引发此异常:

Error: Parse Error
    at CleartextStream.socketOnData (http.js:1583:20)
    at CleartextStream.read [as _read] (tls.js:511:12)
    at CleartextStream.Readable.read (_stream_readable.js:320:10)
    at EncryptedStream.write [as _write] (tls.js:366:25)
    at doWrite (_stream_writable.js:226:10)
    at writeOrBuffer (_stream_writable.js:216:5)
    at EncryptedStream.Writable.write (_stream_writable.js:183:11)
    at write (_stream_readable.js:582:24)
    at flow (_stream_readable.js:591:7)
    at Socket.pipeOnReadable (_stream_readable.js:623:5)
Run Code Online (Sandbox Code Playgroud)

标题大小是否有限制,我可以改变吗?我有什么解决方案?

谢谢

log*_*yth 16

Node使用的HTTP协议解析器似乎是硬编码的,最大标头大小为80KB.相关常数.由于这是一个编译时常量,因此您必须使用自定义编译的Node版本来设置更大的常量.

听起来你正在使用的服务通过将大量数据放在标题中而犯了一个错误.标头用于有关请求正文的元数据.如果他们要返回那么多数据,他们应该将它包含在请求体中.

您可以使用像http-parser-js这样的备用HTTP解析器进行探索,因为它似乎没有限制.

  • 重要信息:最近,默认的最大标头大小已从80K更改为8K。请参阅此处的讨论https://github.com/nodejs/node/issues/24692要配置为有可用的参数,--max-http-header-size (7认同)

Gro*_*ppe 11

Node.js 最近的更新中,默认允许的最大标头大小最近从更改80KB8KB

The total size of HTTP headers received by Node.js now must not exceed 8192 bytes.
Run Code Online (Sandbox Code Playgroud)

在我们的例子中,我们更新了 Node 的版本,突然间开始400 Bad Request从我们的 express 服务器获取's ,这至少可以说是令人困惑的。

有一个问题可以将此响应更改431 Request Header Fields Too Large为更具描述性和帮助性的 a。

它也可以配置,因为它对某些人来说是一个突破性的变化

--max-http-header-size=size#
Added in: v11.6.0
Specify the maximum size, in bytes, of HTTP headers. Defaults to 8KB.
Run Code Online (Sandbox Code Playgroud)


Chr*_*idi 6

使用--max-http-header-size该节点的命令行来接受更大的头。如果出现“节点:错误的选项:-max-http-header-size”,请升级到节点v10.15.0或更高版本。相关变更日志

node --max-http-header-size 15000 client.js
Run Code Online (Sandbox Code Playgroud)

归功于@murad。