Skip to content

Commit 4501fb0

Browse files
committed
fix: normalize bare-domain URLs before request interception
Append trailing slash to bare-domain HTTP(S) URLs (e.g. https://example.comhttps://example.com/) in WebViewNavigator before the interceptor runs, matching standard browser normalization behavior across all platforms.
1 parent eabd7f3 commit 4501fb0

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

webview-compose/src/commonMain/kotlin/io/github/kdroidfilter/webview/web/WebViewNavigator.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ class WebViewNavigator(
6060
NavigationEvent.Reload -> reload()
6161
NavigationEvent.StopLoading -> stopLoading()
6262
is NavigationEvent.LoadUrl -> {
63+
val normalizedUrl = normalizeHttpUrl(event.url)
6364
val interceptor = requestInterceptor
6465
if (interceptor == null) {
65-
loadUrl(event.url, event.additionalHttpHeaders)
66+
loadUrl(normalizedUrl, event.additionalHttpHeaders)
6667
} else {
6768
val request =
6869
io.github.kdroidfilter.webview.request.WebRequest(
69-
url = event.url,
70+
url = normalizedUrl,
7071
headers = event.additionalHttpHeaders.toMutableMap(),
7172
isForMainFrame = true,
7273
method = "GET",
@@ -157,6 +158,22 @@ class WebViewNavigator(
157158
}
158159
}
159160

161+
/**
162+
* Normalize bare-domain HTTP(S) URLs by appending a trailing slash,
163+
* matching browser behavior (e.g. https://example.com → https://example.com/).
164+
*/
165+
private fun normalizeHttpUrl(url: String): String {
166+
if (!url.startsWith("http://") && !url.startsWith("https://")) return url
167+
val schemeEnd = url.indexOf("://") + 3
168+
if (url.indexOf('/', schemeEnd) == -1 &&
169+
url.indexOf('?', schemeEnd) == -1 &&
170+
url.indexOf('#', schemeEnd) == -1
171+
) {
172+
return "$url/"
173+
}
174+
return url
175+
}
176+
160177
@Composable
161178
fun rememberWebViewNavigator(
162179
coroutineScope: CoroutineScope = rememberCoroutineScope(),

0 commit comments

Comments
 (0)