From c7984617e235892cc22e0f47bb5ff1c012e6e39f Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 26 Jul 2021 00:16:21 +0200 Subject: [PATCH] [fix] Fix host parsing for file URLs (#210) Fixes: https://github.com/unshiftio/url-parse/pull/209#issuecomment-886235848 --- index.js | 4 +++- test/test.js | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 903fce2..c6052d5 100644 --- a/index.js +++ b/index.js @@ -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 +'+'); @@ -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 || diff --git a/test/test.js b/test/test.js index 1893891..8b34f7a 100644 --- a/test/test.js +++ b/test/test.js @@ -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');