Why does https.Agent throw a parsing error when proxing through node-http-proxy?

I would like to enable the use of the connection pool through http.Agentand https.Agentin my node-http-proxy application . To do this, I created a secure agent, for example:

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

(I briefly explained the optional http agent for short.)

I am creating a proxy server as such:

const proxy = httpProxy.createProxyServer({});

Finally, I proxy the request inside Connect middleware like this:

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

This seems to work for most queries, but every few minutes I see an error that looks like this:

{
    "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"
}

From some cursory reading, it seems that it HPE_INVALID_CONSTANTappears when the answer is distorted. However, these responses are excellent prior to administration of the protected agent.

Does anyone know what is going on here or how can I fix it?

. Node v7.9.0 Docker FROM node:7.9. HEAD - - .

+4
1

. . ( node), . 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();
    })
}

, . , . , . "Production", .

, , , , - HTTPS .

0

Source: https://habr.com/ru/post/1675072/


All Articles