How to check the actual content length in the Content-Length header?

The user can send the document to our web service. We spend it elsewhere. But, at the end of streaming, we need to be sure that they are not lying about their Content-Length.

I assume that if headerContentLength > realContentLength, the request will just wait until they send the rest, they will eventually fail. So maybe OK.

What if headerContentLength < realContentLength? That is, what if they continue to send data after they said they were made?

Does it make you have Node.js? If not, what can I check? I suppose I could just count the bytes within a certain event listeners data--- ie The, req.on("data", function (chunk) { totalBytes += chunk.length; }). It seems like a scrap, though.

+5
source share
1 answer

To check the actual length of the request, you must add it yourself. Pieces data Buffer, and they have a property .lengththat you can add.

If you specify an encoding with request.setEncoding(), your fragments datawill be String. In this case, call Buffer.byteLength(chunk)to get the length. ( Bufferis the global object in node.)

Add a total for each of your pieces and you will know how much data has been sent. Here is an example (untested):

https.createServer(function(req, res) {
    var expected_length = req.headers['content-length']; // I think this is a string ;)
    var actual_length = 0;
    req.on('data', function (chunk) {
        actual_length += chunk.length;
    });
    req.on('end', function() {
        console.log('expected: ' + expected_length + ', actual: ' + actual_length);
    });
});

. length Buffer, . , , chunk . , .

+2

All Articles