brandonscript brandonscript - 4 months ago 11
Node.js Question

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);

Answer

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.

Comments