Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

no 'end' callback for very large responses. #77

Closed
rnewson opened this issue Mar 19, 2010 · 2 comments
Closed

no 'end' callback for very large responses. #77

rnewson opened this issue Mar 19, 2010 · 2 comments

Comments

@rnewson
Copy link

rnewson commented Mar 19, 2010

Sufficiently large responses cause node.js some internal confusion that makes it never emit an 'end' event.

The following node.js code reproduces the problem. It appears to be chunked mode only, if you change 'res.writeHead(200, {"transfer-encoding":"chunked"});' to 'res.writeHead(200, {"content-length": len*chunk.length});' it exits successfully.

var sys = require("sys"),
fs = require("fs"),
http = require("http"),
url = require("url");

// Produce a very large response.
var chargen = http.createServer(function (req, res) {
    var chunk = '01234567890123456789';
    var len = req.headers['x-len'];
    res.writeHead(200, {"transfer-encoding":"chunked"});
    for (var i=0; i<len; i++) {
  res.write(chunk);
    }
    res.close();
});
chargen.listen(9000);

// Proxy to the chargen server.
var proxy = http.createServer(function (req, res) {
    var proxy_req = http.createClient(9000, 'localhost')
  .request(req.method, req.url, req.headers);
    proxy_req.addListener('response', function(proxy_res) {
  res.writeHead(proxy_res.statusCode, proxy_res.headers);
  proxy_res.addListener('data', function(chunk) {
      res.write(chunk);
  });
  proxy_res.addListener('end', function() {
      res.close();
  });
    });
    proxy_req.close();
});
proxy.listen(9001);

function call_chargen(list) {
  if (list.length > 0) {
    sys.debug("calling chargen for " + list[0] + " chunks.");
    var req = http.createClient(9001, 'localhost').request('/', {'x-len': list[0]});
    req.addListener('response', function(res) {
      res.addListener('end', function() {
        sys.debug("end for " + list[0] + " chunks.");
        list.shift();
        call_chargen(list);
      });
    });
    req.close();
  }
  else {
    sys.puts("End of list.");
      proxy.close();
      chargen.close();
  }
}

call_chargen([ 100, 1000, 10000, 100000, 1000000 ]);
@mikeal
Copy link

mikeal commented Apr 28, 2010

@ry
Copy link

ry commented May 10, 2010

fixed in f919216

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants