Skip to content

Commit

Permalink
[fix] Fix host parsing for file URLs (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca authored Jul 25, 2021
1 parent 201034b commit c798461
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var required = require('requires-port')
, qs = require('querystringify')
, slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//
, protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\\/]+)?([\S\s]*)/i
, windowsDriveLetter = /^[a-zA-Z]:/
, whitespace = '[\\x09\\x0A\\x0B\\x0C\\x0D\\x20\\xA0\\u1680\\u180E\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200A\\u202F\\u205F\\u3000\\u2028\\u2029\\uFEFF]'
, left = new RegExp('^'+ whitespace +'+');

Expand Down Expand Up @@ -279,7 +280,8 @@ function Url(address, location, parser) {
// component.
//
if (
url.protocol === 'file:' ||
extracted.protocol === 'file:' && (
extracted.slashesCount !== 2 || windowsDriveLetter.test(address)) ||
(!extracted.slashes &&
(extracted.protocol ||
extracted.slashesCount < 2 ||
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,13 @@ describe('url-parse', function () {
assume(data.pathname).equals('/c:/foo/bar/');
assume(data.href).equals('file:///c:/foo/bar/');

data = parse('file://host/file');
assume(data.protocol).equals('file:');
assume(data.host).equals('host');
assume(data.hostname).equals('host');
assume(data.pathname).equals('/file');
assume(data.href).equals('file://host/file');

data = parse('foo/bar', 'file:///baz');
assume(data.protocol).equals('file:');
assume(data.pathname).equals('/foo/bar');
Expand Down

0 comments on commit c798461

Please sign in to comment.