From 5758100f43c72efb5d6ad15617cd6c3c7c2abfc8 Mon Sep 17 00:00:00 2001 From: chenchao Date: Tue, 5 Nov 2019 10:37:17 +0800 Subject: [PATCH 1/3] support url started with localhost --- src/shared/urls.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/urls.ts b/src/shared/urls.ts index 64ea4f2..b256b20 100644 --- a/src/shared/urls.ts +++ b/src/shared/urls.ts @@ -5,7 +5,7 @@ const trimStart = (str: string): string => { return str.replace(/^\s+/, ''); }; -const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:']; +const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:', 'localhost:']; const searchUrl = (keywords: string, search: Search): string => { try { From f3ce1c300a79fab2f87c38cd853477e7756edb55 Mon Sep 17 00:00:00 2001 From: chenchao Date: Wed, 6 Nov 2019 18:13:23 +0800 Subject: [PATCH 2/3] more localhost use cases --- src/shared/urls.ts | 14 ++++++++++++-- test/shared/urls.test.ts | 14 +++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/shared/urls.ts b/src/shared/urls.ts index b256b20..ab01c49 100644 --- a/src/shared/urls.ts +++ b/src/shared/urls.ts @@ -5,7 +5,16 @@ const trimStart = (str: string): string => { return str.replace(/^\s+/, ''); }; -const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:', 'localhost:']; +const SUPPORTED_PROTOCOLS = ['http:', 'https:', 'ftp:', 'mailto:', 'about:']; + +const isLocalhost = (url: string): boolean => { + if (url === 'localhost') { + return true; + } + + let [host, port] = url.split(':', 2); + return host === 'localhost' && !isNaN(Number(port)); +}; const searchUrl = (keywords: string, search: Search): string => { try { @@ -16,7 +25,8 @@ const searchUrl = (keywords: string, search: Search): string => { } catch (e) { // fallthrough } - if (keywords.includes('.') && !keywords.includes(' ')) { + if (isLocalhost(keywords) || + (keywords.includes('.') && !keywords.includes(' '))) { return 'http://' + keywords; } let template = search.engines[search.defaultEngine]; diff --git a/test/shared/urls.test.ts b/test/shared/urls.test.ts index 3a3eea6..d6f0550 100644 --- a/test/shared/urls.test.ts +++ b/test/shared/urls.test.ts @@ -36,6 +36,19 @@ describe("shared/commands/parsers", () => { expect(parsers.searchUrl('std::vector', config)) .to.equal('https://google.com/search?q=std%3A%3Avector'); }); + + it('localhost urls', () => { + expect(parsers.searchUrl('localhost', config)) + .to.equal('http://localhost'); + expect(parsers.searchUrl('http://localhost', config)) + .to.equal('http://localhost'); + expect(parsers.searchUrl('localhost:8080', config)) + .to.equal('http://locahost:8080'); + expect(parsers.searchUrl('localhost:80nan', config)) + .to.equal('https://google.com/search?q=localhost:80nan'); + expect(parsers.searchUrl('localhost 8080', config)) + .to.equal('https://google.com/search?q=localhost%208080'); + }) }); describe('#normalizeUrl', () => { @@ -47,4 +60,3 @@ describe("shared/commands/parsers", () => { }); }); }); - From ddd3b08a4a3b393375b6e9716697b9442fcfa66d Mon Sep 17 00:00:00 2001 From: chenchao Date: Thu, 7 Nov 2019 11:14:03 +0800 Subject: [PATCH 3/3] handle localhost urls with path --- src/shared/urls.ts | 19 +++++++++++++++++-- test/shared/urls.test.ts | 8 +++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/shared/urls.ts b/src/shared/urls.ts index ab01c49..8fa9a49 100644 --- a/src/shared/urls.ts +++ b/src/shared/urls.ts @@ -16,6 +16,20 @@ const isLocalhost = (url: string): boolean => { return host === 'localhost' && !isNaN(Number(port)); }; +const isMissingHttp = (keywords: string): boolean => { + if (keywords.includes('.') && !keywords.includes(' ')) { + return true; + } + + try { + let u = new URL('http://' + keywords); + return isLocalhost(u.host) + } catch (e) { + // fallthrough + } + return false; +}; + const searchUrl = (keywords: string, search: Search): string => { try { let u = new URL(keywords); @@ -25,10 +39,11 @@ const searchUrl = (keywords: string, search: Search): string => { } catch (e) { // fallthrough } - if (isLocalhost(keywords) || - (keywords.includes('.') && !keywords.includes(' '))) { + + if (isMissingHttp(keywords)) { return 'http://' + keywords; } + let template = search.engines[search.defaultEngine]; let query = keywords; diff --git a/test/shared/urls.test.ts b/test/shared/urls.test.ts index d6f0550..6206d03 100644 --- a/test/shared/urls.test.ts +++ b/test/shared/urls.test.ts @@ -41,13 +41,15 @@ describe("shared/commands/parsers", () => { expect(parsers.searchUrl('localhost', config)) .to.equal('http://localhost'); expect(parsers.searchUrl('http://localhost', config)) - .to.equal('http://localhost'); + .to.equal('http://localhost/'); expect(parsers.searchUrl('localhost:8080', config)) - .to.equal('http://locahost:8080'); + .to.equal('http://localhost:8080'); expect(parsers.searchUrl('localhost:80nan', config)) - .to.equal('https://google.com/search?q=localhost:80nan'); + .to.equal('https://google.com/search?q=localhost%3A80nan'); expect(parsers.searchUrl('localhost 8080', config)) .to.equal('https://google.com/search?q=localhost%208080'); + expect(parsers.searchUrl('localhost:80/build', config)) + .to.equal('http://localhost:80/build') }) });