Skip to content

Commit

Permalink
Fix for webUrl detection for local named computers (#2704)
Browse files Browse the repository at this point in the history
<!--
Note: This checklist is a reminder of our shared engineering
expectations.
The items in Bold are required
If your PR involves UI changes:
1. Upload screenshots or screencasts that illustrate the changes before
/ after
2. Add them under the UI changes section (feel free to add more columns
if needed)
    3. Make sure these changes are tested in API 23 and API 26
If your PR does not involve UI changes, you can remove the **UI
changes** section
-->

Task/Issue URL:
https://app.asana.com/0/488551667048375/1203568844801448/f

### Description
Tweaks the logic used to determine if an inputted query is a web URL or
not; better handling for a query like `http://raspberrypi` or
`http://raspberry:8080` (has a scheme, does not have a TLD) to navigate
rather than search.

### Steps to test this PR

- [ ] Input `http://raspberry` in the omnibar; verify it tries to
**navigate** to this
- [ ] Input `http://raspberry:8080` in the omnibar; verify it tries to
**navigate** to this
- [ ] Input `https://raspberry:8080` in the omnibar; verify it tries to
**navigate** to this
- [ ] Input `192.168.0.100`; verify it tries to **navigate**
- [ ] Input `http://192.168.0.100`; verify it tries to **navigate**
- [ ] Input `192.168.0.100`; verify it tries to **navigate**
- [ ] Input `raspberry` in the omnibar; verify it performs a **search**
- [ ] Input `ftp://raspberrypi`; verify it says it can't handle this
type of link

- [ ] Repeat any of the above tests you can from an external app link.
e.g., write a text message containing the link and open the link in our
browser; verifying the behaviour is still correct when coming from an
external app
  • Loading branch information
CDRussell committed Jan 11, 2023
1 parent 04613ed commit 9b13f63
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
25 changes: 25 additions & 0 deletions app/src/test/java/com/duckduckgo/app/global/UriStringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,29 @@ class UriStringTest {
fun whenGivenNumberThenIsWebUrlIsFalse() {
assertFalse(isWebUrl("33"))
}

@Test
fun whenNamedLocalMachineWithSchemeAndPortThenIsTrue() {
assertTrue(isWebUrl("http://raspberrypi:8080"))
}

@Test
fun whenNamedLocalMachineWithNoSchemeAndPortThenIsFalse() {
assertFalse(isWebUrl("raspberrypi:8080"))
}

@Test
fun whenNamedLocalMachineWithSchemeNoPortThenIsTrue() {
assertTrue(isWebUrl("http://raspberrypi"))
}

@Test
fun whenStartsWithSiteSpecificSearchThenIsFalse() {
assertFalse(isWebUrl("site:example.com"))
}

@Test
fun whenSchemeIsValidFtpButNotHttpThenNot() {
assertFalse(isWebUrl("ftp://example.com"))
}
}
31 changes: 27 additions & 4 deletions common/src/main/java/com/duckduckgo/app/global/UriString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package com.duckduckgo.app.global

import android.net.Uri
import androidx.core.util.PatternsCompat
import java.lang.IllegalArgumentException
import okhttp3.HttpUrl.Companion.toHttpUrl
import timber.log.Timber

class UriString {

Expand All @@ -43,19 +46,39 @@ class UriString {

fun isWebUrl(inputQuery: String): Boolean {
if (inputQuery.contains(space)) return false
val uri = Uri.parse(inputQuery).withScheme()
if (uri.scheme != UrlScheme.http && uri.scheme != UrlScheme.https) return false
val rawUri = Uri.parse(inputQuery)

val uri = rawUri.withScheme()
if (!uri.hasWebScheme()) return false
if (uri.userInfo != null) return false

val host = uri.host ?: return false
if (host == localhost) return true
if (host.contains(space)) return false
if (host.contains("!")) return false
return (webUrlRegex.containsMatchIn(host))

if (webUrlRegex.containsMatchIn(host)) return true

return try {
// this will throw an exception if OkHttp thinks it's not a well-formed HTTP or HTTPS URL
uri.toString().toHttpUrl()

// it didn't match the regex and OkHttp thinks it looks good
// we might have prepended a scheme to let okHttp check, so only consider it valid at this point if the scheme was manually provided
// e.g., this means "http://raspberrypi" will be considered a webUrl, but "raspberrypi" will not
rawUri.hasWebScheme()
} catch (e: IllegalArgumentException) {
Timber.i("Failed to parse %s as a web url; assuming it isn't", inputQuery)
false
}
}

fun isValidDomain(domain: String): Boolean {
return domainRegex.matches(domain)
}

private fun Uri.hasWebScheme(): Boolean {
val normalized = normalizeScheme()
return normalized.scheme == UrlScheme.http || normalized.scheme == UrlScheme.https
}
}
}

0 comments on commit 9b13f63

Please sign in to comment.