http.request中的nodeJS最大标头大小 [英] nodeJS max header size in http.request

查看:2002
本文介绍了http.request中的nodeJS最大标头大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

With nodeJS v0.10.28, is there a limit in the size/length of the header content in an http request?

让我解释一下:

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

I need to consume rest services provided by a 3rd party provider. The data returned to me is in the header of the request, the body is mostly empty (120 or so chars). The amount of data in the header varies from a few chars to several 100kb.

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();

上面的代码适用于较小尺寸的标题但在on上失败('error',with更大标题上的分析错误消息。

The code above works fine for smaller size headers but fails on the on('error', with "Parse Error" message on larger headers.

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

Removing the on error clause throws this exception:

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)

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

Is there a limit on the header size, can it me changed? what solution do I have?

谢谢

推荐答案

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

The HTTP protocol parser that Node uses appears to be hard-coded with a maximum header size of 80KB. Relevant constant. Since that is a compile-time constant, you would have to use a custom-compiled version of Node to set that constant larger.

这听起来像是你的服务尽管将尽可能多的数据放入标题中,使用却犯了一个错误。标头用于有关请求正文的元数据。如果他们要返回那么多数据,他们应该将它包含在请求体中。

It really sounds like the service you are using has made a mistake by putting that much data in a header though. Headers are meant for metadata about the request body. If they have that much data to return, they should probably be including it in the request body.

您可以使用替代HTTP解析器进行探索,例如 http-parser-js ,因为它似乎没有限制。

You could explore using an alternate HTTP parser like http-parser-js, since it doesn't appear to have a limit.

这篇关于http.request中的nodeJS最大标头大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆