-
Notifications
You must be signed in to change notification settings - Fork 50
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 #186 from hotwired/offline-caching-redirects
When using offline caching, don't mask HTTP redirects
- Loading branch information
Showing
3 changed files
with
79 additions
and
26 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
60 changes: 60 additions & 0 deletions
60
turbo/src/main/kotlin/dev/hotwire/turbo/http/TurboWebViewRequestInterceptor.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,60 @@ | ||
package dev.hotwire.turbo.http | ||
|
||
import android.webkit.WebResourceRequest | ||
import android.webkit.WebResourceResponse | ||
import dev.hotwire.turbo.session.TurboSession | ||
import dev.hotwire.turbo.util.logEvent | ||
|
||
internal class TurboWebViewRequestInterceptor(val session: TurboSession) { | ||
private val offlineRequestHandler get() = session.offlineRequestHandler | ||
private val httpRepository get() = session.httpRepository | ||
private val currentVisit get() = session.currentVisit | ||
|
||
fun interceptRequest(request: WebResourceRequest): WebResourceResponse? { | ||
val requestHandler = offlineRequestHandler ?: return null | ||
|
||
if (!shouldInterceptRequest(request)) { | ||
return null | ||
} | ||
|
||
val url = request.url.toString() | ||
val isCurrentVisitRequest = url == currentVisit?.location | ||
val result = httpRepository.fetch(requestHandler, request) | ||
|
||
return if (isCurrentVisitRequest) { | ||
logCurrentVisitResult(url, result) | ||
currentVisit?.completedOffline = result.offline | ||
|
||
// If the request resulted in a redirect, don't return the response. This | ||
// lets the WebView handle the request/response and Turbo can see the redirect, | ||
// so a redirect "replace" visit can be proposed. | ||
when (result.redirectToLocation) { | ||
null -> result.response | ||
else -> null | ||
} | ||
} else { | ||
result.response | ||
} | ||
} | ||
|
||
private fun shouldInterceptRequest(request: WebResourceRequest): Boolean { | ||
return request.method.equals("GET", ignoreCase = true) && | ||
request.url.scheme?.startsWith("HTTP", ignoreCase = true) == true | ||
} | ||
|
||
private fun logCurrentVisitResult(url: String, result: TurboHttpRepository.Result) { | ||
logEvent( | ||
"location" to url, | ||
"redirectToLocation" to result.redirectToLocation.toString(), | ||
"statusCode" to (result.response?.statusCode ?: "<none>"), | ||
"completedOffline" to result.offline | ||
) | ||
} | ||
|
||
private fun logEvent(vararg params: Pair<String, Any>) { | ||
val attributes = params.toMutableList().apply { | ||
add(0, "session" to session.sessionName) | ||
} | ||
logEvent("interceptRequest", attributes) | ||
} | ||
} |
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