Skip to content

Commit e2108fd

Browse files
committed
fix(demo): normalize bare-domain URLs and fix interceptor matching
normalizeUrl now appends trailing slash to bare-domain URLs (e.g. https://example.comhttps://example.com/) to match browser behavior. DemoRequestInterceptor uses trimEnd('/') for consistent matching with or without trailing slash.
1 parent eabd7f3 commit e2108fd

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

demo-shared/src/commonMain/kotlin/io/github/kdroidfilter/webview/demo/DemoRequestInterceptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class DemoRequestInterceptor(
2121
return WebRequestInterceptResult.Reject
2222
}
2323

24-
if (url == "https://example.com" || url == "https://www.example.com") {
24+
if (url.trimEnd('/') == "https://example.com" || url.trimEnd('/') == "https://www.example.com") {
2525
val rewritten = "https://httpbin.org/anything?from=interceptor&original=" + uriEncodeComponent(url)
2626
onLog("interceptor: rewrite $url -> $rewritten")
2727
val modified =

demo-shared/src/commonMain/kotlin/io/github/kdroidfilter/webview/demo/DemoUtils.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@ package io.github.kdroidfilter.webview.demo
33
internal fun normalizeUrl(raw: String): String {
44
val trimmed = raw.trim()
55
if (trimmed.isEmpty()) return "about:blank"
6-
if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) return trimmed
76
if (trimmed.startsWith("file://")) return trimmed
8-
return "https://$trimmed"
7+
8+
val url = if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
9+
trimmed
10+
} else {
11+
"https://$trimmed"
12+
}
13+
14+
// Add trailing slash for bare-domain URLs (e.g. https://example.com → https://example.com/)
15+
// to match browser normalization behavior
16+
val schemeEnd = url.indexOf("://") + 3
17+
val pathStart = url.indexOf('/', schemeEnd)
18+
if (pathStart == -1 && url.indexOf('?', schemeEnd) == -1 && url.indexOf('#', schemeEnd) == -1) {
19+
return "$url/"
20+
}
21+
return url
922
}
1023

1124
internal fun hostFromUrl(url: String): String? {

0 commit comments

Comments
 (0)