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

Issue 1604: Fix 'appendRequestPath: false' scenarios for Restify.plugins.serverStatic #1860

Open
wants to merge 1 commit 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
23 changes: 12 additions & 11 deletions lib/plugins/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function serveStatic(options) {
assert.optionalObject(opts.match, 'options.match');
assert.optionalString(opts.charSet, 'options.charSet');
assert.optionalString(opts.file, 'options.file');
assert.optionalString(opts.file, 'options.default');
assert.bool(opts.appendRequestPath, 'options.appendRequestPath');

var p = path.normalize(opts.directory).replace(/\\/g, '/');
Expand Down Expand Up @@ -170,19 +171,19 @@ function serveStatic(options) {
} else if (opts.appendRequestPath) {
file = path.join(opts.directory, decodeURIComponent(req.path()));
} else {
var dirBasename = path.basename(opts.directory);
var reqpathBasename = path.basename(req.path());

if (
path.extname(req.path()) === '' &&
dirBasename === reqpathBasename
) {
if (path.extname(req.path()) === '') {
file = opts.directory;
} else {
file = path.join(
opts.directory,
decodeURIComponent(path.basename(req.path()))
);
var fileName = decodeURIComponent(path.basename(req.path()));
// test for requested file, if it doesn't exist we send the directory as path since appendRequestPath = false
if (!fs.existsSync(path.join(opts.directory, fileName))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: mind breaking these nested function invocations up into multiple lines w/ variables indicating what each path is supposed to represent?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Author

@jmbeltran07 jmbeltran07 Oct 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! let me work on it and I'll update the PR! great catch & suggestion!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I noticed the Issue #1604 is still open and has gotten some activity recently from people saying the issue is still unfixed. Are there any plans to submit this PR, or would you prefer if someone else creates a clone of this PR and submits that? - though that seems like unnecessary duplicate effort

file = opts.directory;
} else {
file = path.join(
opts.directory,
decodeURIComponent(path.basename(req.path()))
);
}
}
}

Expand Down
124 changes: 96 additions & 28 deletions test/plugins/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,22 @@ describe('static resource plugin', function() {
});
}

function testNoAppendPath(done, testDefault, tmpDir, regex, staticFile) {
function testNoAppendPath(
done,
testDefault,
tmpDir,
regex,
staticFile,
requestDirectFile,
customRoute
) {
var staticContent = '{"content": "abcdefg"}';
var staticContent2 = '{"content2": "hijklmn"}';
var staticObj = JSON.parse(staticContent);
var staticObj2 = JSON.parse(staticContent2);
var testDir = 'public';
var testFileName = 'index.json';
var testFileName2 = 'specific.json';
var routeName = 'GET wildcard';
var tmpPath = path.join(__dirname, '../', tmpDir);

Expand All @@ -140,40 +151,61 @@ describe('static resource plugin', function() {

DIRS_TO_DELETE.push(folderPath);
var file = path.join(folderPath, testFileName);
var file2 = path.join(folderPath, testFileName2);

fs.writeFile(file, staticContent, function(err3) {
assert.ifError(err3);
FILES_TO_DELETE.push(file);
var p = '/' + testDir + '/' + testFileName;
var opts = { directory: folderPath };
opts.appendRequestPath = false;

if (staticFile) {
opts.file = testFileName;
}

if (testDefault) {
p = '/' + testDir + '/';
opts.default = testFileName;
routeName += ' with default';
}

SERVER.get(
{
path: '/' + testDir + '/*',
name: routeName
},
restify.plugins.serveStatic(opts)
);

CLIENT.get(p, function(err4, req, res, obj) {
fs.writeFile(file2, staticContent2, function(err4) {
assert.ifError(err4);
assert.equal(
res.headers['cache-control'],
'public, max-age=3600'
FILES_TO_DELETE.push(file2);

var p = '/' + testDir + '/' + testFileName;
var opts = { directory: folderPath };
opts.appendRequestPath = false;

if (staticFile) {
opts.file = testFileName;
}

if (testDefault) {
p =
'/' +
testDir +
'/other/route/it/should/serve/default/anyway';
opts.default = testFileName;
routeName += ' with default';
}

if (requestDirectFile) {
p = '/' + testDir + '/specific.json';
}

if (customRoute) {
p = '/' + testDir + '/' + customRoute;
}

SERVER.get(
{
path: '/' + testDir + '/*',
name: routeName
},
restify.plugins.serveStatic(opts)
);
assert.deepEqual(obj, staticObj);
done();

CLIENT.get(p, function(err5, req, res, obj) {
assert.ifError(err5);
assert.equal(
res.headers['cache-control'],
'public, max-age=3600'
);
var ojbToCompare = requestDirectFile
? staticObj2
: staticObj;
assert.deepEqual(obj, ojbToCompare);
done();
});
});
});
});
Expand Down Expand Up @@ -227,6 +259,42 @@ describe('static resource plugin', function() {
testNoAppendPath(done, false, '.tmp', null, true);
});

// eslint-disable-next-line
it('static serves and request an specific file with appendRequestPath = false', function(done) {
testNoAppendPath(done, true, '.tmp', null, null, true);
});

// eslint-disable-next-line
it('static serves and request an specific file with appendRequestPath = false, with no default file specified', function(done) {
testNoAppendPath(done, false, '.tmp', null, null, true);
});

// eslint-disable-next-line
it('static serves and request an inexistent specific file with appendRequestPath = false, with default file specified', function(done) {
testNoAppendPath(
done,
true,
'.tmp',
null,
null,
false,
'doesNotExists.json'
);
});

// eslint-disable-next-line
it('static serves and request an inexistent specific route with appendRequestPath = false, with default file specified', function(done) {
testNoAppendPath(
done,
true,
'.tmp',
null,
null,
false,
'doesNotExists'
);
});

it('static responds 404 for missing file', function(done) {
var p = '/public/no-such-file.json';
var tmpPath = path.join(process.cwd(), '.tmp');
Expand Down