ranm8 / requestify

Simplifies node HTTP request making.
http://ranm8.github.io/requestify
MIT License
223 stars 55 forks source link

client can hang forever #19

Open manish-alphonso-tv opened 10 years ago

manish-alphonso-tv commented 10 years ago

I have stepped on a situation in which a client can hang forever.

my HTTP client, is susceptible to getting blocked-forever, if the 3rd party server has buggy code, such as one written below.

require('http').createServer(                                                                                                   
    function (req, rsp)
    {
         console.log("rcvd request from client");
         rsp.writeHead(200, {"Content-Type": "text/plain"});
         rsp.write("Hello World 1");       /* server to send a data chunk */
         // rsp.end("Hello World 2\n");   /* server not to 'end' the response */
    }
).listen(24680);

the fix is trivial, in the Requestify.js

   ...

   httpRequest = http.request(options, function(res) {
          // clearTimeout(timeout);  // Fix-1: move this to Fix-2, while handling 'end' event.
          var response = new Response(res.statusCode, res.headers);

          res.setEncoding(responseEncoding);
          res.on('data', function(chunk) {
              response.setChunk(chunk);
          });

          res.on('end', function() {
              clearTimeout(timeout);  // Fix-2: this is when the timer should be cleared
              if (isSuccessful(response.code)) {
                  storeCache(request.getFullUrl(), response.getCode(), response.getHeaders(), response.body, request.cache);
                  defer.resolve(response);
                  return;
              }

              defer.reject(response);
          });                                                                                                                 
      });

...
manish-alphonso-tv commented 8 years ago

I have placed a pull request for this. Please consider accepting it.