Skip to content

Commit

Permalink
Check content length before marking an unpacked dir as unpacked.
Browse files Browse the repository at this point in the history
  • Loading branch information
yhahn committed Nov 7, 2015
1 parent 9aa6de5 commit b647757
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
9 changes: 8 additions & 1 deletion index.js
Expand Up @@ -493,6 +493,9 @@ function tm2z(uri, callback) {
// Package unpacked but no project.xml. Call load to error our.
if (unpacked) return load();

// Callback already called with an error.
if (once) return;

todo.push(function(next) { fs.writeFile(base + '/.unpacked', '', next); });
var next = function(err) {
if (err && err.code !== 'EEXIST') return error(err);
Expand Down Expand Up @@ -520,7 +523,11 @@ function tm2z(uri, callback) {
break;
case 'tm2z+http:':
uri.protocol = 'http:';
stream = request({ uri: uri })
stream = request({ uri: uri, encoding:null }, function(err, res, body) {
if (res.headers['content-length'] && parseInt(res.headers['content-length'],10) !== body.length) {
error(new Error('Content-Length does not match response body length'));
}
})
.on('data', chunked)
.pipe(gunzip)
.pipe(parser)
Expand Down
42 changes: 22 additions & 20 deletions test/tm2z.js
Expand Up @@ -28,27 +28,29 @@ tilelive.protocols['mapbox:'] = function Source(uri, callback) {
// Register font
Vector.mapnik.register_fonts(path.join(__dirname, 'fonts', 'source-sans-pro'));

test('tm2z+http content length error', function(assert) {
var server = require('http').createServer(function(req, res) {
var buffer = fs.readFileSync(path.join(fixtureDir, 'patternstyle.tm2z'));
res.setHeader('content-length', buffer.length);
res.writeHead(200);
res.write(buffer.slice(0,250e3));
req.socket.destroy();
});
server.listen(9191, afterListen);
function afterListen(err) {
assert.ifError(err);
tilelive.load('tm2z+http://127.0.0.1:9191/patternstyle.tm2z', function(err, source) {
assert.ok(err, 'has error');
assert.equal(err.code, undefined, 'not a mapnik error');
server.close(afterClose);
['cold', 'warm'].forEach(function(label) {
test(label + ': tm2z+http content length error', function(assert) {
var server = require('http').createServer(function(req, res) {
var buffer = fs.readFileSync(path.join(fixtureDir, 'patternstyle.tm2z'));
res.setHeader('content-length', buffer.length);
res.writeHead(200);
res.write(buffer.slice(0,250e3));
req.socket.destroy();
});
}
function afterClose(err) {
assert.ifError(err);
assert.end();
}
server.listen(9191, afterListen);
function afterListen(err) {
assert.ifError(err);
tilelive.load('tm2z+http://127.0.0.1:9191/patternstyle.tm2z', function(err, source) {
assert.ok(err, 'has error');
assert.equal(err.code, undefined, 'not a mapnik error');
server.close(afterClose);
});
}
function afterClose(err) {
assert.ifError(err);
assert.end();
}
});
});

test('exposes the mapnik binding', function(t) {
Expand Down

0 comments on commit b647757

Please sign in to comment.