8000 Accept IP:PORT query as a URL (#601) · androiddev2019/Android@1af6ca7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1af6ca7

Browse files
shamilatesoglusubsymbolic
authored andcommitted
Accept IP:PORT query as a URL (duckduckgo#601)
1 parent e30cb68 commit 1af6ca7

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

app/src/androidTest/java/com/duckduckgo/app/global/UriExtensionTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ class UriExtensionTest {
125125
assertTrue(Uri.parse("54.229.105.203/something").hasIpHost)
126126
}
127127

128+
@Test
129+
fun whenIpWithPortUriThenHasIpHostIsTrue() {
130+
assertTrue(Uri.parse("https://54.229.105.203:999/something").hasIpHost)
131+
assertTrue(Uri.parse("54.229.105.203:999/something").hasIpHost)
132+
}
133+
134+
@Test
135+
fun whenIpWithPortUriThenPortNumberParsedSuccessfully() {
136+
assertEquals(999, Uri.parse("https://54.229.105.203:999/something").port)
137+
}
138+
139+
@Test
140+
fun whenValidIpAddressWithPortParsedWithSchemeThenPortNumberParsedSuccessfully(){
141+
assertEquals(999, Uri.parse("121.33.2.11:999").withScheme().port)
142+
}
143+
128144
@Test
129145
fun whenStandardUriThenHasIpHostIsFalse() {
130146
assertFalse(Uri.parse("http://example.com").hasIpHost)

app/src/androidTest/java/com/duckduckgo/app/global/UriStringTest.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.duckduckgo.app.global
1818

19+
import android.net.Uri
1920
import com.duckduckgo.app.global.UriString.Companion.isWebUrl
2021
import com.duckduckgo.app.global.UriString.Companion.sameOrSubdomain
21-
import org.junit.Assert.assertFalse
22-
import org.junit.Assert.assertTrue
22+
import org.junit.Assert.*
2323
import org.junit.Test
2424

2525
class UriStringTest {
@@ -71,6 +71,11 @@ class UriStringTest {
7171
assertTrue(isWebUrl("121.33.2.11"))
7272
}
7373

74+
@Test
75+
fun whenHostIsValidIpAddressWithPortThenIsWebUrlIsTrue() {
76+
assertTrue(isWebUrl("121.33.2.11:999"))
77+
}
78+
7479
@Test
7580
fun whenHostIsLocalhostThenIsWebUrlIsTrue() {
7681
assertTrue(isWebUrl("localhost"))
@@ -106,6 +111,11 @@ class UriStringTest {
106111
assertTrue(isWebUrl("http://121.33.2.11"))
107112
}
108113

114+
@Test
115+
fun whenSchemeIsValidIpAddressWithPortThenIsWebUrlIsTrue() {
116+
assertTrue(isWebUrl("http://121.33.2.11:999"))
117+
}
118+
109119
@Test
110120
fun whenSchemeIsValidLocalhostUrlThenIsWebUrlIsTrue() {
111121
assertTrue(isWebUrl("http://localhost"))
@@ -166,6 +176,11 @@ class UriStringTest {
166176
assertTrue(isWebUrl("http://121.33.2.11/path"))
167177
}
168178

179+
@Test
180+
fun whenPathIsValidIpAddressWithPortThenIsWebUrlIsTrue() {
181+
assertTrue(isWebUrl("http://121.33.2.11:999/path"))
182+
}
183+
169184
@Test
170185
fun whenPathIsValidLocalhostThenIsWebUrlIsTrue() {
171186
assertTrue(isWebUrl("http://localhost/path"))

app/src/main/java/com/duckduckgo/app/global/UriExtension.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ import android.net.Uri
1919
import android.net.Uri.parse
2020
import com.duckduckgo.app.global.UrlScheme.Companion.http
2121

22+
val IP_REGEX = Regex("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:[0-9]+)?$")
23+
2224
fun Uri.withScheme(): Uri {
23-
if (scheme == null) {
25+
// Uri.parse function falsely parses IP:PORT string.
26+
// For example if input is "255.255.255.255:9999", it falsely flags 255.255.255.255 as the scheme.
27+
// Therefore in the withScheme method, we need to parse it after manually inserting "http".
28+
if (scheme == null || scheme!!.matches(IP_REGEX)) {
2429
return parse("$http://${toString()}")
2530
}
2631

@@ -45,8 +50,7 @@ val Uri.toHttps: Uri
4550

4651
val Uri.hasIpHost: Boolean
4752
get() {
48-
val ipRegex = Regex("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
49-
return baseHost?.matches(ipRegex) ?: false
53+
return baseHost?.matches(IP_REGEX) ?: false
5054
}
5155

5256
fun Uri.isHttpsVersionOfUri(other: Uri): Boolean {

0 commit comments

Comments
 (0)
0