Skip to content

Commit

Permalink
Rename the request result class and move the redirect properties to i…
Browse files Browse the repository at this point in the history
…ts own data class
  • Loading branch information
jayohms committed Jan 8, 2025
1 parent 9e17f8c commit 49a299c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@ 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)

if (response != null) {
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 49a299c

Please sign in to comment.