Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: how to disable chunked transfer-encoding?

I'm missing a content-length header on my response from a Node server that I'm piping a .zip file from another location. I've injected a content-length header via the code below, but still it seems the transfer-encoding: chunked is overwriting it somehow.

Response Headers

HTTP/1.1 200 OK
access-control-allow-origin: *
connection: close
content-type: application/zip
date: Mon, 14 Jul 2014 03:47:00 GMT
etag: "\"eb939974703e14ee9f578642972ed984\""
last-modified: Sat, 12 Jul 2014 02:15:52 GMT
server: Apache-Coyote/1.1
set-cookie: rememberMe=deleteMe; Path=/; Max-Age=0; Expires=Sun, 13-Jul-2014 03:47:00 GMT
transfer-encoding: chunked
X-Powered-By: Express

Code

var request = require('request');
var express = require('express');
var async = require('async');

var app = express();

app.get('/:bundle_id?', function(req, res) {
    var bundle_id = req.params.bundle_id;
    bundle_id = bundle_id.replace(/\.zip$/, '');

    var url = "https://url....../bundles/" + bundle_id;

    async.waterfall([

        function(callback) {
            request.get(url, function(req, res, data) {
                callback(null, JSON.parse(data).entities[0]['file-metadata']['content-length']);
            });
        }
    ], function(err, contentLength) {

        request.get({
            url: url,
            headers: {
                "Accept": "application/zip"
            }
        }).pipe(res);

        res.oldWriteHead = res.writeHead;
        res.writeHead = function(statusCode, reasonPhrase, headers) {
            res.header('Content-Length', contentLength);
            res.oldWriteHead(statusCode, reasonPhrase, headers);
        }
    });
});

app.listen(9000);
like image 684
brandonscript Avatar asked Jul 14 '14 03:07

brandonscript


People also ask

How do I stop transfer encoding chunked?

Try adding "&headers=false" to your request. That should shorten it up and cause the response to be less likely to be chunked. Also, are you sending a HTTP/1.1 or HTTP/1.0 request? Try sending a HTTP/1.0 if your device cannot handle a HTTP/1.1 request.

What means transfer encoding chunked?

Chunked transfer encoding is a streaming data transfer mechanism available in version 1.1 of the Hypertext Transfer Protocol (HTTP). In chunked transfer encoding, the data stream is divided into a series of non-overlapping "chunks". The chunks are sent out and received independently of one another.

What is chunked message?

Chunked transfer-coding, also known as chunking, involves transferring the body of a message as a series of chunks, each with its own chunk size header. The end of the message is indicated by a chunk with zero length and an empty line.


1 Answers

Turns out this was actually a rather simple fix: setting the transfer-encoding header to an empty string in the response solved the problem:

...
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, reasonPhrase, headers) {
    res.header('Content-Length', contentLength);
    res.header('transfer-encoding', ''); // <-- add this line
    res.oldWriteHead(statusCode, reasonPhrase, headers);
}
...

The reason this works, is because after doing some digging, it appears the transfer-encoding header replaces content-length (since both can't co-exist). It just so happens that the clients I was using to test were choosing chunked transfer encoding over content length.

like image 179
brandonscript Avatar answered Nov 23 '22 08:11

brandonscript