-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from hotwired/cross-origin-redirect
Detect cross-origin redirects during visits
- Loading branch information
Showing
10 changed files
with
284 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
core/src/main/kotlin/dev/hotwire/core/turbo/http/HttpRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dev.hotwire.core.turbo.http | ||
|
||
import android.webkit.CookieManager | ||
import dev.hotwire.core.logging.logError | ||
import dev.hotwire.core.turbo.util.dispatcherProvider | ||
import kotlinx.coroutines.withContext | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
|
||
internal class HttpRepository { | ||
private val cookieManager = CookieManager.getInstance() | ||
|
||
data class HttpRequestResult( | ||
val response: Response, | ||
val redirect: HttpRedirect? | ||
) | ||
|
||
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() | ||
|
||
HttpRequestResult( | ||
response = response, | ||
redirect = if (!isRedirect) null else HttpRedirect( | ||
location = responseUrl.toString(), | ||
isCrossOrigin = location.toHttpUrl().host != responseUrl.host | ||
) | ||
) | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
|
||
private fun issueRequest(location: String): Response? { | ||
return try { | ||
val request = buildRequest(location) | ||
HotwireHttpClient.instance.newCall(request).execute() | ||
} catch (e: Exception) { | ||
logError("httpRequestError", e) | ||
null | ||
} | ||
} | ||
|
||
private fun buildRequest(location: String): Request { | ||
val builder = Request.Builder().url(location) | ||
|
||
cookieManager.getCookie(location)?.let { | ||
builder.header("Cookie", it) | ||
} | ||
|
||
return builder.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.