Skip to content

Commit 4a90106

Browse files
committed
Obtain favicons directly from the website instead of a favicon service
1 parent a2c2ffb commit 4a90106

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.duckduckgo.app.global
1818

1919
import android.net.Uri
20+
import androidx.core.net.toUri
2021
import org.junit.Assert.*
2122
import org.junit.Test
2223

@@ -194,4 +195,33 @@ class UriExtensionTest {
194195
val absoluteString = Uri.parse("https://example.com/test?q=example/#1/anotherrandomcode").absoluteString
195196
assertEquals("https://example.com/test", absoluteString)
196197
}
198+
199+
@Test
200+
fun whenNullUrlThenNullFaviconUrl() {
201+
assertNull("".toUri().faviconLocation())
202+
}
203+
204+
@Test
205+
fun whenHttpRequestThenFaviconLocationAlsoHttp() {
206+
val favicon = "http://example.com".toUri().faviconLocation()
207+
assertTrue(favicon!!.isHttp)
208+
}
209+
210+
@Test
211+
fun whenHttpsRequestThenFaviconLocationAlsoHttps() {
212+
val favicon = "https://example.com".toUri().faviconLocation()
213+
assertTrue(favicon!!.isHttps)
214+
}
215+
216+
@Test
217+
fun whenUrlContainsASubdomainThenSubdomainReturnedInFavicon() {
218+
val favicon = "https://sub.example.com".toUri().faviconLocation()
219+
assertEquals("https://sub.example.com/favicon.ico", favicon.toString())
220+
}
221+
222+
@Test
223+
fun whenUrlIsIpAddressThenIpReturnedInFaviconUrl() {
224+
val favicon = "https://192.168.1.0".toUri().faviconLocation()
225+
assertEquals("https://192.168.1.0/favicon.ico", favicon.toString())
226+
}
197227
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ fun Uri.toDesktopUri(): Uri {
8080
return parse(newUrl)
8181
}
8282

83-
private const val faviconBaseUrlFormat = "https://proxy.duckduckgo.com/ip3/%s.ico"
83+
// to obtain a favicon for a website, we go directly to the site and look for /favicon.ico
84+
private const val faviconBaseUrlFormat = "%s://%s/favicon.ico"
8485

8586
fun Uri?.faviconLocation(): Uri? {
86-
val host = this?.host
87+
if (this == null) return null
88+
val host = this.host
8789
if (host.isNullOrBlank()) return null
88-
return parse(String.format(faviconBaseUrlFormat, host))
90+
val isHttps = this.isHttps
91+
return parse(String.format(faviconBaseUrlFormat, if (isHttps) "https" else "http", host))
8992
}

0 commit comments

Comments
 (0)