From d0cb48924709cfb4002f7110799d616b52018086 Mon Sep 17 00:00:00 2001 From: Roxanne Skelly Date: Tue, 1 Oct 2019 15:34:39 -0700 Subject: [PATCH 1/3] DEV-1836 - handle urls in the form 'hifi://: --- libraries/networking/src/AddressManager.cpp | 47 ++++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/libraries/networking/src/AddressManager.cpp b/libraries/networking/src/AddressManager.cpp index bd3dc7c177..973d8a625b 100644 --- a/libraries/networking/src/AddressManager.cpp +++ b/libraries/networking/src/AddressManager.cpp @@ -234,14 +234,33 @@ const JSONCallbackParameters& AddressManager::apiCallbackParameters() { return callbackParams; } -bool AddressManager::handleUrl(const QUrl& lookupUrl, LookupTrigger trigger) { +bool AddressManager::handleUrl(const QUrl& lookupUrlIn, LookupTrigger trigger) { static QString URL_TYPE_USER = "user"; static QString URL_TYPE_DOMAIN_ID = "domain_id"; static QString URL_TYPE_PLACE = "place"; static QString URL_TYPE_NETWORK_ADDRESS = "network_address"; - if (lookupUrl.scheme() == URL_SCHEME_HIFI) { - qCDebug(networking) << "Trying to go to URL" << lookupUrl.toString(); + QUrl lookupUrl = lookupUrlIn; + + qCDebug(networking) << "Trying to go to URL" << lookupUrl.toString(); + + if (lookupUrl.scheme().isEmpty() && !lookupUrl.path().startsWith("/")) { + // 'urls' without schemes are taken as domain names, as opposed to + // simply a path portion of a url, so we need to set the scheme + lookupUrl.setScheme(URL_SCHEME_HIFI); + } + + // it should be noted that url's in the form + // somewhere: are not valid, as that + // would indicate that the scheme is 'somewhere' + // use hifi://somewhere: instead + + if (lookupUrl.scheme() == URL_SCHEME_HIFI) { + if (lookupUrl.host().isEmpty()) { + // this was in the form hifi:/somewhere or hifi:somewhere. Fix it by making it hifi://somewhere + const QRegExp HIFI_SCHEME_REGEX = QRegExp(URL_SCHEME_HIFI + ":\\/?", Qt::CaseInsensitive); + lookupUrl = QUrl(lookupUrl.toString().replace(HIFI_SCHEME_REGEX, URL_SCHEME_HIFI + "://")); + } DependencyManager::get()->flagTimeForConnectionStep(LimitedNodeList::ConnectionStep::LookupAddress); @@ -379,25 +398,11 @@ bool isPossiblePlaceName(QString possiblePlaceName) { } void AddressManager::handleLookupString(const QString& lookupString, bool fromSuggestions) { - if (!lookupString.isEmpty()) { + + QString sanitizedString = lookupString.trimmed(); + if (!sanitizedString.isEmpty()) { // make this a valid hifi URL and handle it off to handleUrl - QString sanitizedString = lookupString.trimmed(); - QUrl lookupURL; - - if (!lookupString.startsWith('/')) { - // sometimes we need to handle lookupStrings like hifi:/somewhere - const QRegExp HIFI_SCHEME_REGEX = QRegExp(URL_SCHEME_HIFI + ":\\/{1,2}", Qt::CaseInsensitive); - sanitizedString = sanitizedString.remove(HIFI_SCHEME_REGEX); - - lookupURL = QUrl(sanitizedString); - if (lookupURL.scheme().isEmpty() || lookupURL.scheme().toLower() == LOCALHOST) { - lookupURL = QUrl("hifi://" + sanitizedString); - } - } else { - lookupURL = QUrl(sanitizedString); - } - - handleUrl(lookupURL, fromSuggestions ? Suggestions : UserInput); + handleUrl(sanitizedString, fromSuggestions ? Suggestions : UserInput); } } From 9af7b904abb0df0633d091b423189152ca0fe0e7 Mon Sep 17 00:00:00 2001 From: Roxanne Skelly Date: Tue, 1 Oct 2019 17:04:07 -0700 Subject: [PATCH 2/3] CR related fixes --- libraries/networking/src/AddressManager.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/networking/src/AddressManager.cpp b/libraries/networking/src/AddressManager.cpp index 973d8a625b..828396d9d7 100644 --- a/libraries/networking/src/AddressManager.cpp +++ b/libraries/networking/src/AddressManager.cpp @@ -250,6 +250,11 @@ bool AddressManager::handleUrl(const QUrl& lookupUrlIn, LookupTrigger trigger) { lookupUrl.setScheme(URL_SCHEME_HIFI); } + static const QRegExp PORT_REGEX = QRegExp("\\d{1,5}"); + if(!lookupUrl.scheme().isEmpty() && lookupUrl.host().isEmpty() && PORT_REGEX.exactMatch(lookupUrl.path())) { + // this is in the form somewhere:, convert it to hifi://somewhere: + lookupUrl = QUrl(URL_SCHEME_HIFI + "://" + lookupUrl.toString()); + } // it should be noted that url's in the form // somewhere: are not valid, as that // would indicate that the scheme is 'somewhere' @@ -258,7 +263,7 @@ bool AddressManager::handleUrl(const QUrl& lookupUrlIn, LookupTrigger trigger) { if (lookupUrl.scheme() == URL_SCHEME_HIFI) { if (lookupUrl.host().isEmpty()) { // this was in the form hifi:/somewhere or hifi:somewhere. Fix it by making it hifi://somewhere - const QRegExp HIFI_SCHEME_REGEX = QRegExp(URL_SCHEME_HIFI + ":\\/?", Qt::CaseInsensitive); + static const QRegExp HIFI_SCHEME_REGEX = QRegExp(URL_SCHEME_HIFI + ":\\/?", Qt::CaseInsensitive); lookupUrl = QUrl(lookupUrl.toString().replace(HIFI_SCHEME_REGEX, URL_SCHEME_HIFI + "://")); } From fb8a0e3aee9f3f372d3d6784a3f52223890de938 Mon Sep 17 00:00:00 2001 From: Roxanne Skelly Date: Wed, 2 Oct 2019 16:40:27 -0700 Subject: [PATCH 3/3] handle urls in the form of :/path --- libraries/networking/src/AddressManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/AddressManager.cpp b/libraries/networking/src/AddressManager.cpp index 828396d9d7..50e2657622 100644 --- a/libraries/networking/src/AddressManager.cpp +++ b/libraries/networking/src/AddressManager.cpp @@ -250,7 +250,7 @@ bool AddressManager::handleUrl(const QUrl& lookupUrlIn, LookupTrigger trigger) { lookupUrl.setScheme(URL_SCHEME_HIFI); } - static const QRegExp PORT_REGEX = QRegExp("\\d{1,5}"); + static const QRegExp PORT_REGEX = QRegExp("\\d{1,5}(\\/.*)?"); if(!lookupUrl.scheme().isEmpty() && lookupUrl.host().isEmpty() && PORT_REGEX.exactMatch(lookupUrl.path())) { // this is in the form somewhere:, convert it to hifi://somewhere: lookupUrl = QUrl(URL_SCHEME_HIFI + "://" + lookupUrl.toString());