为什么https.Agent在通过node-http-proxy进行代理时会抛出一个解析错误?

时间:2017-04-18 17:47:19

标签: javascript node.js ssl proxy node-http-proxy

我想在我的node-http-proxy应用中通过http.Agenthttps.Agent启用连接池。为此,我建立了一个安全代理,如下所示:

const secureAgent = new https.secureAgent({ keepAlive: true });

(为了简洁,我省略了补充的http代理。)

我创建了一个代理服务器,如下:

const proxy = httpProxy.createProxyServer({});

最后,我在Connect中间件中代理请求,如下所示:

proxy.web(req, res {
  agent: isSecure ? secureAgent : agent,
  target: ...,
  secure: false,
});

这似乎适用于大多数请求,但每隔几分钟我就会看到一个如下错误:

{
    "message": "Parse Error",
    "stack": "Error: Parse Error\n    at TLSSocket.socketOnData (_http_client.js:411:20)\n    at emitOne (events.js:96:13)\n    at TLSSocket.emit (events.js:191:7)\n    at readableAddChunk (_stream_readable.js:178:18)\n    at TLSSocket.Readable.push (_stream_readable.js:136:10)\n    at TLSWrap.onread (net.js:560:20)",
    "bytesParsed": 215,
    "code": "HPE_INVALID_CONSTANT",
    "__error_callsites": [
        {},
        {},
        {},
        {},
        {},
        {}
    ],
    "level": "error",
    "timestamp": "2017-04-18T17:34:09.735Z"
}

从一些粗略的阅读中,似乎HPE_INVALID_CONSTANT在反应不正确时显现出来。但是,在引入安全代理之前,这些响应都很好。

有没有人知道这里发生了什么或我如何解决它?

注意:通过FROM node:7.9在Docker容器中的Node v7.9.0上发生此错误。到目前为止,我只对HEAD个请求发现了这一点 - 必须有一些原因。

1 个答案:

答案 0 :(得分:0)

只需在此处添加评论即可。 我面临着类似的问题。 (正在使用节点请求库),在我的案例空间中请求参数中的一个值。另外,我向http://localhost(http)(nginx)发出请求,然后由python重定向到https://localhost(https)。这两者的结合对我来说是个问题。在我的情况下我注意到有一件事我可以使用请求体,但是Parse错误已经到来了。我做了一个小小的黑客来忽略错误,并发送响应。

    let formData = "some data";
    let headers = // some headers;
    headers["content-length"] = formData.length;
    headers["Accept-Encoding"] = "gzip, deflate";
    let gunzip;
    return new Promise(function (resolve, reject) {
        let buffer = [];
        let req = http.request({
            port: 80,
            method: "POST",
            path: url,
            headers: headers
        }, function (res) {
            let resHeaders = res.headers["content-encoding"];
            switch (resHeaders){
                case "gzip":
                    gunzip = zlib.createGunzip();
                    res.pipe(gunzip);
                    gunzip.on("data", function (data) {
                        buffer.push(data.toString());
                    }).on("end", function () {
                        resolve(buffer[0])
                    });
                    break;
                case "deflate":
                    gunzip = zlib.createDeflate();
                    res.pipe(gunzip);
                    gunzip.on("data", function (data) {
                        buffer.push(data.toString());
                    }).on("end", function () {
                        resolve(buffer[0])
                    });
                    break;
                default:
                    res.on("data", function (data) {
                        buffer.push(data.toString())
                    }).on("end", function () {
                        resolve(buffer[0])
                    })
            }
        });
        req.on('error', (e) => {
            // This is the Hack. If there is an error just ignore it.
            logger.debug("Error in getting requests, ", e)
        });
        req.write(formData);
        req.end();
    })
}

希望它有所帮助。如果你找出原因,请分享。我试图找出一天的原因,但不能。由于我们在生产中遇到了这个问题所以不得不做这个快速补丁。

根据我的想法,很可能是因为流中的HTTPS重定向。