Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel a file download/request on node.js http.Client based on file size

I made a function on node.js to start a file download but I want to create a rule where the function checks the file size before downloading the data.

I got the response headers and checked the size but I dont know how to cancel everything withou transferring the actual data/body. Maybe there is a way to transfer only the headers first and the, if match my rule, I can fire another request to do the download.

Here is a fragment of my code:

request.on('response', function(response) {
        var filesize = response.headers['content-length'];
        console.log("File size " + filename + ": " + filesize + " bytes.");
        response.pause();
        if (filesize >= 50000) {
            // WHAT TO PUT HERE TO CANCEL THE DOWNLOAD?
            console.log("Download cancelled. File too big.");
        } else {
            response.resume();
        }
        //Create file and write the data chunks to it

Thanks.

like image 489
Carlosedp Avatar asked Jan 28 '11 19:01

Carlosedp


1 Answers

According to HTTP protocol specifications 9.4 HEAD

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

If your server does not response properly to this I think you might be out of luck? Next simply just use google.request('HEAD' instead of google.request('GET'


Some code

I tested the following below. fake.js is just a fake server using express to test.

fake.js:

var HOST = 'localhost';
var PORT = 3000;
var connections = 0;
var express = require('express');
var app = module.exports = express.createServer();

if (process.argv[2] && process.argv[3]) {
    HOST = process.argv[2];
    PORT = process.argv[3];
}

app.use(express.staticProvider(__dirname + '/public'));

// to reconnect.
app.get('/small', function(req,  res) {
    console.log(req.method);
    if (req.method == 'HEAD') {
        console.log('here');
        res.send('');
    } else {
        connections++;
        res.send('small');    
    }
});

app.get('/count', function(req, res) {
    res.send('' + connections);
});

app.get('/reset', function(req, res) {
    connections = 0;
    res.send('reset');
});


if (!module.parent) {
    app.listen(PORT, HOST);
    console.log("Express server listening on port %d", app.address().port)
}

test.js is to test head from http-client. test.js:

var http = require('http');
var google = http.createClient(3000, 'localhost');
var request = google.request('HEAD', '/small',
  {'host': 'localhost'});
request.end();
request.on('response', function (response) {
  console.log('STATUS: ' + response.statusCode);
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.setEncoding('utf8');
});

alfred@alfred-laptop:~/node/stackoverflow/4832362$ curl http://localhost:3000/count
0

alfred@alfred-laptop:~/node/stackoverflow/4832362$ node test.js 
STATUS: 200
HEADERS: {"content-type":"text/html; charset=utf-8","content-length":"0","connection":"close"}

alfred@alfred-laptop:~/node/stackoverflow/4832362$ curl http://localhost:3000/count
0

As you can see still 0.

like image 159
Alfred Avatar answered Sep 27 '22 21:09

Alfred