Skip to content

Commit caae871

Browse files
authored
Merge pull request #29 from bertbaron/master
Added option for configuration of the baseHref of the application
2 parents 6c86b7a + a945e97 commit caae871

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ Specify a path to serve from
5353
angular-http-server --path example
5454
```
5555

56+
Specify the base href of the application
57+
```sh
58+
angular-http-server --baseHref myapp
59+
```
60+
5661
Disable logging
5762
```sh
5863
angular-http-server --silent

lib/angular-http-server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if (argv.config) {
3636
// Pre-process arguments
3737
const useHttps = argv.ssl || argv.https;
3838
const basePath = argv.path ? path.resolve(argv.path) : process.cwd();
39+
const baseHref = argv.baseHref ? argv.baseHref : '';
3940
const port = getPort(argv.p);
4041

4142
// As a part of the startup - check to make sure we can access index.html
@@ -99,7 +100,7 @@ function requestListener(req, res) {
99100
}
100101
}
101102

102-
const safeFullFileName = getFilePathFromUrl(req.url, basePath);
103+
const safeFullFileName = getFilePathFromUrl(req.url, basePath, { baseHref });
103104

104105
fs.stat(safeFullFileName, function(err, stats) {
105106
var fileBuffer;

lib/get-file-path-from-url.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,29 @@ const path = require('path');
88
* @param {object} [pathLib=path] - path library, override for testing
99
* @return {string} - will return 'dummy' if the path is bad
1010
*/
11-
function getFilePathFromUrl(url, basePath, { pathLib = path } = {}) {
11+
function getFilePathFromUrl(url, basePath, { pathLib = path, baseHref = '' } = {}) {
1212
if (!basePath) {
1313
throw new Error('basePath must be specified');
1414
}
1515
if (!pathLib.isAbsolute(basePath)) {
1616
throw new Error(`${basePath} is invalid - must be absolute`);
1717
}
1818

19-
const relativePath = url.split('?')[0];
19+
let relativePath = url.split('?')[0];
2020

2121
if (relativePath.indexOf('../') > -1) {
2222
// any path attempting to traverse up the directory should be rejected
2323
return 'dummy';
2424
}
2525

26+
if (baseHref) {
27+
if (relativePath.startsWith('/' + baseHref)) {
28+
relativePath = relativePath.substr(baseHref.length + 1)
29+
} else {
30+
return 'dummy'
31+
}
32+
}
33+
2634
const absolutePath = pathLib.join(basePath, relativePath);
2735
if (
2836
!absolutePath.startsWith(basePath) || // if the path has broken out of the basePath, it should be rejected

lib/get-file-path-from-url.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,38 @@ describe('getFilePathFromUrl', () => {
6565
});
6666
}
6767

68+
function testWithBaseHref(pathLib, basePath) {
69+
const baseHref = 'myapp';
70+
context (`where baseHref = "${baseHref}" and basePath = "${basePath}"`, () => {
71+
function createTest(input, expected) {
72+
it(`should return "${expected}" given input "${input}"`, () => {
73+
expect(getFilePathFromUrl(input, basePath, { pathLib, baseHref})).to.equal(expected)
74+
});
75+
}
76+
function expectPath(input, expected) {
77+
createTest(input, pathLib.join(basePath, expected).split('?')[0]);
78+
}
79+
function expectDummy(input, expected) {
80+
createTest(input, 'dummy');
81+
}
82+
expectPath('/myapp/test.js', 'test.js');
83+
expectDummy('/test.js');
84+
});
85+
}
86+
6887
context('on a Windows system', () => {
6988
const pathLib = path.win32;
7089

7190
testInvalidBasePaths(pathLib, ['dist\\output', '.\\', '.', '.\\serve']);
7291
testValidBasePaths(pathLib, ['C:\\', 'C:\\project', 'C:\\project\\serve\\output']);
92+
testWithBaseHref(pathLib, 'C:\\project');
7393
});
7494

7595
context('on a POSIX system', () => {
7696
const pathLib = path.posix;
7797

7898
testInvalidBasePaths(pathLib, ['dist/output', './', '.', './serve']);
7999
testValidBasePaths(pathLib, ['/', '/home', '/home/user/project/serve/output']);
100+
testWithBaseHref(pathLib, '/home/user/project');
80101
});
81102
});

0 commit comments

Comments
 (0)