Skip to content

Commit

Permalink
feat(android): also make gitlab requests work
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Dec 14, 2024
1 parent e4d95cb commit 6bd36f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class CapacitorMainActivity : BridgeActivity() {
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
Log.v("TW", "Regular Request Intercepting request: ${request?.url}")
val interceptedResponse = webViewRequestHandler.interceptWebRequest(request)
return interceptedResponse ?: super.shouldInterceptRequest(view, request)
}
Expand All @@ -86,6 +87,7 @@ class CapacitorMainActivity : BridgeActivity() {
swController.setServiceWorkerClient(
object : ServiceWorkerClient() {
override fun shouldInterceptRequest(request: WebResourceRequest): WebResourceResponse? {
Log.v("TW", "SW Intercepting request: ${request.url}")
val interceptedResponse = webViewRequestHandler.interceptWebRequest(request)
return interceptedResponse ?: bridge.webViewClient.shouldInterceptRequest(bridge.webView, request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OptionsAllowResponse {
"Access-Control-Allow-Methods" to "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS",
"Access-Control-Max-Age" to "7200",
"Access-Control-Allow-Credentials" to "true",
"Access-Control-Allow-Headers" to "accept, authorization, Content-Type",
"Access-Control-Allow-Headers" to "*",
// "Via" to "1.1 vegur"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,29 @@ class WebViewRequestHandler(private val activity: Activity, private val serviceH

for ((key, value) in request.requestHeaders) {
Log.v("TW", "interceptRequest header:${key}${value}")
if(key == "User-Agent" || key == "Origin" || key == "Referer") {
if (key == "User-Agent" || key == "Origin" || key == "Referer" || key == "Sec-Fetch-Mode") {
continue
}
newRequestBuilder.addHeader(key, value)
}

newRequestBuilder.header("User-Agent", "curl/7.64.1")
val newRequest = newRequestBuilder.build()

if (request.method.uppercase() == "OPTIONS") {
Log.v("TW", "OPTIONS request triggered")
client.newCall(newRequest).execute().use { response ->
Log.v(
"TW",
"OPTIONS original response: ${response.code} ${response.message} ${response.body?.string()}"
)
if (response.code != 200) {
Log.v("TW", "OPTIONS overwrite")
return OptionsAllowResponse.build()
}
}
return OptionsAllowResponse.build()
// to execute actual OPTIONS request uncomment the following lines
// client.newCall(newRequest).execute().use { response ->
// Log.v(
// "TW",
// "OPTIONS original response: ${response.code} ${response.message} ${response.body?.string()}"
// )
// if (response.code != 200) {
// Log.v("TW", "OPTIONS overwrite")
// return OptionsAllowResponse.build()
// }
// }
}
//-------------

Expand All @@ -111,7 +114,7 @@ class WebViewRequestHandler(private val activity: Activity, private val serviceH

val contentType = response.header("Content-Type", "text/plain")
val contentEncoding = response.header("Content-Encoding", "utf-8")
// TODO check if this needs to be adjusted for HEAD requests

val inputStream = ByteArrayInputStream(response.body?.bytes())
val reasonPhrase =
response.message.ifEmpty { "OK" } // provide a default value if the message is null or empty
Expand Down

0 comments on commit 6bd36f0

Please sign in to comment.