Skip to content
This repository was archived by the owner on Aug 10, 2023. It is now read-only.

Commit 82c2ed0

Browse files
rundefmicahr
authored andcommitted
(serveStatic plugin) Add an option to not append req.path() to the file path (#41)
1 parent 5d36c2c commit 82c2ed0

2 files changed

Lines changed: 94 additions & 3 deletions

File tree

lib/plugins/static.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ var ResourceNotFoundError = errors.ResourceNotFoundError;
3232
*/
3333
function serveStatic(options) {
3434
var opts = options || {};
35+
36+
if (typeof opts.appendRequestPath === 'undefined') {
37+
opts.appendRequestPath = true;
38+
}
39+
3540
assert.object(opts, 'options');
3641
assert.string(opts.directory, 'options.directory');
3742
assert.optionalNumber(opts.maxAge, 'options.maxAge');
3843
assert.optionalObject(opts.match, 'options.match');
3944
assert.optionalString(opts.charSet, 'options.charSet');
4045
assert.optionalString(opts.file, 'options.file');
46+
assert.bool(opts.appendRequestPath, 'options.appendRequestPath');
4147

4248
var p = path.normalize(opts.directory).replace(/\\/g, '/');
4349
var re = new RegExp('^' + escapeRE(p) + '/?.*');
@@ -114,8 +120,24 @@ function serveStatic(options) {
114120
file = path.join(opts.directory,
115121
decodeURIComponent(opts.file));
116122
} else {
117-
file = path.join(opts.directory,
118-
decodeURIComponent(req.path()));
123+
if (opts.appendRequestPath) {
124+
file = path.join(opts.directory,
125+
decodeURIComponent(req.path()));
126+
}
127+
else {
128+
var dirBasename = path.basename(opts.directory);
129+
var reqpathBasename = path.basename(req.path());
130+
131+
if (path.extname(req.path()) === '' &&
132+
dirBasename === reqpathBasename) {
133+
134+
file = opts.directory;
135+
}
136+
else {
137+
file = path.join(opts.directory,
138+
decodeURIComponent(path.basename(req.path())));
139+
}
140+
}
119141
}
120142

121143
if (req.method !== 'GET' && req.method !== 'HEAD') {

test/static.test.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ describe('static resource plugin', function () {
100100
}
101101

102102
if (testDefault) {
103-
opts.defaultFile = testFileName;
103+
p = '/' + testDir + '/';
104+
opts.default = testFileName;
104105
routeName += ' with default';
105106
}
106107
var re = regex ||
@@ -121,7 +122,61 @@ describe('static resource plugin', function () {
121122
});
122123
});
123124
});
125+
}
126+
127+
function testNoAppendPath(done, testDefault, tmpDir, regex, staticFile) {
128+
var staticContent = '{"content": "abcdefg"}';
129+
var staticObj = JSON.parse(staticContent);
130+
var testDir = 'public';
131+
var testFileName = 'index.json';
132+
var routeName = 'GET wildcard';
133+
var tmpPath = path.join(__dirname, '../', tmpDir);
134+
135+
mkdirp(tmpPath, function (err) {
136+
assert.ifError(err);
137+
DIRS_TO_DELETE.push(tmpPath);
138+
var folderPath = path.join(tmpPath, testDir);
139+
140+
mkdirp(folderPath, function (err2) {
141+
assert.ifError(err2);
142+
143+
DIRS_TO_DELETE.push(folderPath);
144+
var file = path.join(folderPath, testFileName);
145+
146+
fs.writeFile(file, staticContent, function (err3) {
147+
assert.ifError(err3);
148+
FILES_TO_DELETE.push(file);
149+
var p = '/' + testDir + '/' + testFileName;
150+
var opts = { directory: folderPath };
151+
opts.appendRequestPath = false;
152+
153+
if (staticFile) {
154+
opts.file = testFileName;
155+
}
124156

157+
if (testDefault) {
158+
p = '/' + testDir + '/';
159+
opts.default = testFileName;
160+
routeName += ' with default';
161+
}
162+
var re = regex ||
163+
new RegExp('/' + testDir + '/?.*');
164+
165+
SERVER.get({
166+
path: re,
167+
name: routeName
168+
}, plugins.serveStatic(opts));
169+
170+
CLIENT.get(p, function (err4, req, res, obj) {
171+
assert.ifError(err4);
172+
assert.equal(res.headers['cache-control'],
173+
'public, max-age=3600');
174+
assert.deepEqual(obj, staticObj);
175+
done();
176+
});
177+
});
178+
});
179+
});
125180
}
126181

127182
it('static serves static files', function (done) {
@@ -162,4 +217,18 @@ describe('static resource plugin', function () {
162217
serveStaticTest(done, false, '.tmp', null, true);
163218
});
164219

220+
it('static serves static file with appendRequestPath = false',
221+
function (done) {
222+
testNoAppendPath(done, false, '.tmp');
223+
});
224+
225+
it('static serves default file with appendRequestPath = false',
226+
function (done) {
227+
testNoAppendPath(done, true, '.tmp');
228+
});
229+
230+
it('restify serve a specific static file with appendRequestPath = false',
231+
function (done) {
232+
testNoAppendPath(done, false, '.tmp', null, true);
233+
});
165234
});

0 commit comments

Comments
 (0)