diff --git a/index.js b/index.js index ae7fe1e..6282629 100644 --- a/index.js +++ b/index.js @@ -47,19 +47,22 @@ module.exports = src => { const $tds = $(tr).find('td'); const getCol = label => fieldCols[label] === undefined ? null : $tds.eq(fieldCols[label]); const getColText = label => getCol(label) && getCol(label).text().trim(); - - const path = getCol('Name').children().eq(0).attr('href'); const name = getColText('Name'); // Ignore 'Parent Directory' row if (name === 'Parent Directory' || !name) return; + let path = getCol('Name').children().eq(0).attr('href'); + if (!path.startsWith('http://') && !path.startsWith('https://')) { + path = join(dir, path); + } + files.push({ type: path.endsWith('/') ? 'directory' : 'file', name: name, - path: join(dir, path), + path: path, lastModified: getCol('Last modified') && new Date(getColText('Last modified')), size: getCol('Size') && bytes(getColText('Size')), description: getColText('Description') diff --git a/test/fixture/apache-index-absolute-and-relative-urls.html b/test/fixture/apache-index-absolute-and-relative-urls.html new file mode 100644 index 0000000..dc5d39f --- /dev/null +++ b/test/fixture/apache-index-absolute-and-relative-urls.html @@ -0,0 +1,48 @@ + + + Index of /foo/bar + + + +

Index of /foo/bar

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameLast modifiedSizeDescription
Parent Directory
subdir-absolute/Wed Jun 30 22:24:50 UTC 2021
subdir-relative/Wed Jun 30 22:24:50 UTC 2021
subfile-absolute.xmlSun Jul 04 16:22:15 UTC 2021 + 25 +
subfile-relative.xmlSun Jul 04 16:22:15 UTC 2021 + 25 +
+ + diff --git a/test/index.js b/test/index.js index 230c3ea..347197f 100644 --- a/test/index.js +++ b/test/index.js @@ -131,3 +131,46 @@ test('should parse apache directory index files (alt format)', t => { }); t.end(); }); + +test('Should parse both absolute and relative URLs', t => { + const index = fs.readFileSync(join(__dirname, '/fixture/apache-index-absolute-and-relative-urls.html'), 'utf8'); + + t.same(parse(index), { + dir: '/foo/bar', + files: [ + { + name: 'subdir-absolute/', + type: 'directory', + path: 'https://www.example.org/subdir-absolute/', + description: '', + size: null, + lastModified: new Date('2021-06-30T22:24:50.000Z') + }, + { + name: 'subdir-relative/', + type: 'directory', + path: '/foo/bar/subdir-relative/', + size: null, + lastModified: new Date('2021-06-30T22:24:50.000Z'), + description: '' + }, + { + name: 'subfile-absolute.xml', + type: 'file', + path: 'https://www.example.org/subfile-absolute.xml', + size: 25, + lastModified: new Date('2021-07-04T16:22:15.000Z'), + description: '' + }, + { + name: 'subfile-relative.xml', + type: 'file', + path: '/foo/bar/subfile-relative.xml', + size: 25, + lastModified: new Date('2021-07-04T16:22:15.000Z'), + description: '' + } + ] + }); + t.end(); +});