From 49a299c2297113e14a7052c36295a73a7f3760fe Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Wed, 8 Jan 2025 14:07:58 -0500 Subject: [PATCH] Rename the request result class and move the redirect properties to its own data class --- .../hotwire/core/turbo/http/HttpRepository.kt | 21 ++++++++++++------- .../dev/hotwire/core/turbo/session/Session.kt | 4 ++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt index 239fa14..c1310c0 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt @@ -11,13 +11,17 @@ import okhttp3.Response internal class HttpRepository { private val cookieManager = CookieManager.getInstance() - data class Result( + data class HttpRequestResult( val response: Response, - val redirectToLocation: String?, - val redirectIsCrossOrigin: Boolean + val redirect: HttpRedirect? ) - suspend fun fetch(location: String): Result? { + data class HttpRedirect( + val location: String, + val isCrossOrigin: Boolean + ) + + suspend fun fetch(location: String): HttpRequestResult? { return withContext(dispatcherProvider.io) { val response = issueRequest(location) @@ -25,12 +29,13 @@ internal class HttpRepository { // Determine if there was a redirect, based on the final response's request url val responseUrl = response.request.url val isRedirect = location != responseUrl.toString() - val redirectIsCrossOrigin = isRedirect && location.toHttpUrl().host != responseUrl.host - Result( + HttpRequestResult( response = response, - redirectToLocation = if (isRedirect) responseUrl.toString() else null, - redirectIsCrossOrigin = redirectIsCrossOrigin + redirect = if (!isRedirect) null else HttpRedirect( + location = responseUrl.toString(), + isCrossOrigin = location.toHttpUrl().host != responseUrl.host + ) ) } else { null diff --git a/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt b/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt index afbcafa..6cf8bd1 100644 --- a/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt +++ b/core/src/main/kotlin/dev/hotwire/core/turbo/session/Session.kt @@ -353,10 +353,10 @@ class Session( val result = httpRepository.fetch(location) if (result != null && result.response.isSuccessful && - result.redirectToLocation != null && result.redirectIsCrossOrigin) { + result.redirect?.isCrossOrigin == true) { visitProposedToCrossOriginRedirect( location = location, - redirectLocation = result.redirectToLocation, + redirectLocation = result.redirect.location, visitIdentifier = visitIdentifier ) } else {