From 20ab946f34b1d9727ff08c733b2006e84fd79349 Mon Sep 17 00:00:00 2001 From: jeswinsimon Date: Wed, 11 Sep 2019 05:05:18 -0700 Subject: [PATCH] Support bare hosts. Add missing / between url (#26050) Summary: Fix for https://github.com/facebook/react-native/issues/26019 URL cannot handle "localhost" domain for base url. Also noticed another issue where `/` is not added between base URL and path if it is missing. Added fix for that too. ## Changelog [Javascript] [Fixed] - `URL`: Bare Hosts are now supported. Pull Request resolved: https://github.com/facebook/react-native/pull/26050 Test Plan: * `new URL('home', 'http://localhost')` now returns `http://localhost/home` instead of throwing an error. * `new URL('en-US/docs', 'https://developer.mozilla.org')` now returns `https://developer.mozilla.org/en-US/docs` and not `https://developer.mozilla.orgen-US/docs`. Differential Revision: D17314137 Pulled By: cpojer fbshipit-source-id: ef56c4f4032187a7efee32b28e2b3c935b6a2599 --- Libraries/Blob/URL.js | 7 +++++-- Libraries/Blob/__tests__/URL-test.js | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Libraries/Blob/URL.js b/Libraries/Blob/URL.js index 9869dcbeade..f2b6f7f277c 100644 --- a/Libraries/Blob/URL.js +++ b/Libraries/Blob/URL.js @@ -107,7 +107,7 @@ export class URLSearchParams { function validateBaseUrl(url: string) { // from this MIT-licensed gist: https://gist.github.com/dperini/729294 - return /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( + return /^(?:(?:(?:https?|ftp):)?\/\/)(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( url, ); } @@ -144,9 +144,12 @@ export class URL { } else if (typeof base === 'object') { baseUrl = base.toString(); } - if (baseUrl.endsWith('/') && url.startsWith('/')) { + if (baseUrl.endsWith('/')) { baseUrl = baseUrl.slice(0, baseUrl.length - 1); } + if (!url.startsWith('/')) { + url = `/${url}`; + } if (baseUrl.endsWith(url)) { url = ''; } diff --git a/Libraries/Blob/__tests__/URL-test.js b/Libraries/Blob/__tests__/URL-test.js index 281649ce782..87b8558049b 100644 --- a/Libraries/Blob/__tests__/URL-test.js +++ b/Libraries/Blob/__tests__/URL-test.js @@ -33,5 +33,11 @@ describe('URL', function() { expect(h.href).toBe('https://developer.mozilla.org/en-US/docs'); const i = new URL('http://github.com', 'http://google.com'); expect(i.href).toBe('http://github.com/'); + // Support Bare Hosts + const j = new URL('home', 'http://localhost'); + expect(j.href).toBe('http://localhost/home'); + // Insert / between Base and Path if missing + const k = new URL('en-US/docs', 'https://developer.mozilla.org'); + expect(k.href).toBe('https://developer.mozilla.org/en-US/docs'); }); });