Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should not set "Content-Encoding" for a direct request of a compressed file #161

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions packages/sirv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function toAssume(uri, extns) {

let arr=[], tmp=`${uri}/index`;
for (; i < extns.length; i++) {
x = extns[i] ? `.${extns[i]}` : '';
if (uri) arr.push(uri + x);
arr.push(tmp + x);
x = extns[i].ext ? `.${extns[i].ext}` : '';
if (uri) arr.push({ name: uri + x, encoded: extns[i].encoded });
arr.push({ name: tmp + x, encoded: extns[i].encoded });
}

return arr;
Expand All @@ -31,21 +31,23 @@ function toAssume(uri, extns) {
function viaCache(cache, uri, extns) {
let i=0, data, arr=toAssume(uri, extns);
for (; i < arr.length; i++) {
if (data = cache[arr[i]]) return data;
if (data = cache[arr[i].name]) {
return { ...data, ...arr[i] };
}
}
}

function viaLocal(dir, isEtag, uri, extns) {
let i=0, arr=toAssume(uri, extns);
let abs, stats, name, headers;
for (; i < arr.length; i++) {
abs = normalize(join(dir, name=arr[i]));
abs = normalize(join(dir, name=arr[i].name));
if (abs.startsWith(dir) && fs.existsSync(abs)) {
stats = fs.statSync(abs);
if (stats.isDirectory()) continue;
headers = toHeaders(name, stats, isEtag);
headers = toHeaders(stats, isEtag);
headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
return { abs, stats, headers };
return { abs, stats, headers, ...arr[i] };
}
}
}
Expand All @@ -54,10 +56,17 @@ function is404(req, res) {
return (res.statusCode=404,res.end());
}

function send(req, res, file, stats, headers) {
function send(req, res, file, stats, headers, name, encoded) {
let code=200, tmp, opts={};
headers = { ...headers };

let enc = encoded ? ENCODING[name.slice(-3)] : undefined;
if (enc) headers['Content-Encoding'] = enc;

let ctype = lookup(name.slice(0, enc && -3)) || '';
if (ctype === 'text/html') ctype += ';charset=utf-8';
headers['Content-Type'] = ctype;

for (let key in headers) {
tmp = res.getHeader(key);
if (tmp) headers[key] = tmp;
Expand Down Expand Up @@ -97,19 +106,12 @@ const ENCODING = {
'.gz': 'gzip',
};

function toHeaders(name, stats, isEtag) {
let enc = ENCODING[name.slice(-3)];

let ctype = lookup(name.slice(0, enc && -3)) || '';
if (ctype === 'text/html') ctype += ';charset=utf-8';

function toHeaders(stats, isEtag) {
let headers = {
'Content-Length': stats.size,
'Content-Type': ctype,
'Last-Modified': stats.mtime.toUTCString(),
};

if (enc) headers['Content-Encoding'] = enc;
if (isEtag) headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;

return headers;
Expand Down Expand Up @@ -154,7 +156,7 @@ export default function (dir, opts={}) {
if (/\.well-known[\\+\/]/.test(name)) {} // keep
else if (!opts.dotfiles && /(^\.|[\\+|\/+]\.)/.test(name)) return;

let headers = toHeaders(name, stats, isEtag);
let headers = toHeaders(stats, isEtag);
if (cc) headers['Cache-Control'] = cc;

FILES['/' + name.normalize().replace(/\\+/g, '/')] = { abs, stats, headers };
Expand All @@ -164,12 +166,12 @@ export default function (dir, opts={}) {
let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);

return function (req, res, next) {
let extns = [''];
let extns = [{ ext: '', encoded: false }];
let pathname = parse(req).pathname;
let val = req.headers['accept-encoding'] || '';
if (gzips && val.includes('gzip')) extns.unshift(...gzips);
if (brots && /(br|brotli)/i.test(val)) extns.unshift(...brots);
extns.push(...extensions); // [...br, ...gz, orig, ...exts]
if (gzips && val.includes('gzip')) extns.unshift(...gzips.map(ext => ({ ext, encoded: true })));
if (brots && /(br|brotli)/i.test(val)) extns.unshift(...brots.map(ext => ({ ext, encoded: true })));
extns.push(...extensions.map(ext => ({ ext, encoded: false }))); // [...br, ...gz, orig, ...exts]

if (pathname.indexOf('%') !== -1) {
try { pathname = decodeURI(pathname) }
Expand All @@ -189,6 +191,6 @@ export default function (dir, opts={}) {
}

setHeaders(res, pathname, data.stats);
send(req, res, data.abs, data.stats, data.headers);
send(req, res, data.abs, data.stats, data.headers, data.name, data.encoded);
};
}
1 change: 1 addition & 0 deletions tests/public/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.csv
1 change: 1 addition & 0 deletions tests/public/test.csv.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.csv.gz
1 change: 1 addition & 0 deletions tests/public/test.csv.gz.gz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.csv.gz.gz
70 changes: 70 additions & 0 deletions tests/sirv.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,76 @@ gzip('should defer to brotli when "Accept-Encoding" allows both', async () => {
}
});

gzip('should not set "Content-Encoding" for a direct request of a copressed file (dev: true)', async () => {
let server = utils.http({ dev: true, gzip: true });
try {
await testGzipDirectRequest(server);
} finally {
server.close();
}
});

gzip('should not set "Content-Encoding" for a direct request of a copressed file (dev: false)', async () => {
let server = utils.http({ dev: false, gzip: true });
try {
await testGzipDirectRequest(server);
} finally {
server.close();
}
});

async function testGzipDirectRequest(server) {
let headers = { 'Accept-Encoding': 'gzip' };

{
let res = await server.send('GET', '/test.csv.gz.gz', { headers });
assert.is(res.headers['content-type'], 'application/gzip');
assert.is(res.headers['content-encoding'], undefined);
assert.is(res.data, 'test.csv.gz.gz\n');
assert.is(res.statusCode, 200);
}

{
let res = await server.send('GET', '/test.csv.gz', { headers });
assert.is(res.headers['content-type'], 'application/gzip');
assert.is(res.headers['content-encoding'], 'gzip');
assert.is(res.data, 'test.csv.gz.gz\n');
assert.is(res.statusCode, 200);
}

{
let res = await server.send('GET', '/test.csv', { headers });
assert.is(res.headers['content-type'], 'text/csv');
assert.is(res.headers['content-encoding'], 'gzip');
assert.is(res.data, 'test.csv.gz\n');
assert.is(res.statusCode, 200);
}

{
let res = await server.send('GET', '/test.csv.gz.gz');
assert.is(res.headers['content-type'], 'application/gzip');
assert.is(res.headers['content-encoding'], undefined);
assert.is(res.data, 'test.csv.gz.gz\n');
assert.is(res.statusCode, 200);
}

{
let res = await server.send('GET', '/test.csv.gz');
assert.is(res.headers['content-type'], 'application/gzip');
assert.is(res.headers['content-encoding'], undefined);
assert.is(res.data, 'test.csv.gz\n');
assert.is(res.statusCode, 200);
}

{
let res = await server.send('GET', '/test.csv');
assert.is(res.headers['content-type'], 'text/csv');
assert.is(res.headers['content-encoding'], undefined);
assert.is(res.data, 'test.csv\n');
assert.is(res.statusCode, 200);
}
}

gzip.run();

// ---
Expand Down