Nodejs proxy script, does not work with mod_deflate

I created a small proxy node script that looks for request.url and either passes the request to my apache server, or uses node for the process / response to this request. So far I have been successful, everything works fine, but when I turn on mod_deflate for apache, "weird things will happen."

It appears that node simply β€œcancels” or β€œstops” the early response method. I am listening to the data event from my request, and at some point node just decides that the answer has ended (which is wrong) and fires the end event.

Code snippet:

var apache = http.createClient(82, 'localhost');

function pass_to_apache(req, res){
    var request = apache.request(req.method, req.url, req.headers);

    req.addListener('end', function() {
        request.end();
    });

    req.addListener('data', function(chunk) {
        request.write(chunk);
        sys.puts('writting chunk\n');
    });

    request.addListener('response', function(response) {
        res.writeHead(response.statusCode, response.headers);
        response.addListener('data', function(chunk) {
            sys.puts('writting data..\n');
            res.write(chunk);
        });
        response.addListener('end', function() {
            sys.puts('end of request');
            res.end();
        });
    });
}

var MainServer = http.createServer(function(request, response) {
    sys.puts('received '+request.method+' '+request.url + "\n"+JSON.stringify(request.headers));
    if(/^\/node/.test(request.url)) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.end("Hi, it node =)\n");
    }
    else if(/^\/exit/.test(request.url)) {
        sys.puts('closing..\n');
        MainServer.close();
        throw new Error('forced');
    }
    else {
        pass_to_apache(request, response);
    }
});

MainServer.listen(80, 'typeofnan.com');

"" www.typeofnan.com && & && & && www.typeofnan.com/node/anything
: nodejs.

, , gzip/deflate . "" , .

- ? - ? relase (0.2.0).

, () -?

+3
1

. www.typeofnan.com. , , . , proxy apache.org, gzipped! GET "/" :

{"date":"Mon, 13 Sep 2010 11:03:45 GMT","server":"Apache/2.3.8 (Unix) mod_ssl/2.3.8 OpenSSL/1.0.0a","last-modified":"Sat, 11 Sep 2010 19:38:09 GMT","etag":"\"c9489a-4ada-4900100c32a40-gzip\"","accept-ranges":"bytes","vary":"Accept-Encoding","content-encoding":"gzip","cache-control":"max-age=86400","expires":"Tue, 14 Sep 2010 11:03:45 GMT","content-length":"5359","keep-alive":"timeout=5, max=100","connection":"Keep-Alive","content-type":"text/html"}

... gzip, ? , " ", ? , , , "":)

, accept-encoding, apache . apache apache :

req.headers['accept-encoding'] = '*;q=1,gzip=0';
0
source

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


All Articles