@@ -29,6 +29,7 @@ import androidx.annotation.RequiresApi
2929import androidx.core.net.toUri
3030import androidx.core.os.bundleOf
3131import androidx.fragment.app.Fragment
32+ import androidx.lifecycle.lifecycleScope
3233import be.mygod.reactmap.App.Companion.app
3334import be.mygod.reactmap.BuildConfig
3435import be.mygod.reactmap.R
@@ -37,6 +38,7 @@ import be.mygod.reactmap.util.UnblockCentral
3738import be.mygod.reactmap.util.findErrorStream
3839import com.google.android.material.snackbar.Snackbar
3940import com.google.firebase.analytics.FirebaseAnalytics
41+ import kotlinx.coroutines.launch
4042import org.json.JSONArray
4143import org.json.JSONException
4244import org.json.JSONObject
@@ -123,6 +125,7 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
123125
124126 protected lateinit var web: WebView
125127 protected lateinit var glocation: Glocation
128+ private lateinit var postInterceptor: PostInterceptor
126129 protected lateinit var hostname: String
127130
128131 private var loginText: String? = null
@@ -152,6 +155,7 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
152155 javaScriptEnabled = true
153156 }
154157 glocation = Glocation (this , this @BaseReactMapFragment)
158+ postInterceptor = PostInterceptor (this )
155159 webChromeClient = object : WebChromeClient () {
156160 @Suppress(" KotlinConstantConditions" )
157161 override fun onConsoleMessage (consoleMessage : ConsoleMessage ) = consoleMessage.run {
@@ -194,11 +198,15 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
194198
195199 override fun onPageStarted (view : WebView , url : String , favicon : Bitmap ? ) {
196200 glocation.clear()
201+ postInterceptor.clear()
197202 val uri = url.toUri()
198203 if (! BuildConfig .DEBUG && " http" .equals(uri.scheme, true )) {
199204 web.loadUrl(uri.buildUpon().scheme(" https" ).build().toString())
200205 }
201- if (uri.host == hostname) glocation.setupGeolocation()
206+ if (uri.host == hostname) {
207+ glocation.setupGeolocation()
208+ postInterceptor.setup()
209+ }
202210 onPageStarted()
203211 }
204212
@@ -233,9 +241,7 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
233241 return handleTranslation(request)
234242 }
235243 if (vendorJsMatcher.matchEntire(path) != null ) return handleVendorJs(request)
236- if (path == " /graphql" && request.method == " POST" ) {
237- request.requestHeaders.remove(" _interceptedBody" )?.let { return handleGraphql(request, it) }
238- }
244+ postInterceptor.extractBody(request)?.let { return handleGraphql(request, it) }
239245 }
240246 if (ReactMapHttpEngine .isCronet && (path.substringAfterLast(' .' ).lowercase(Locale .ENGLISH )
241247 in mediaExtensions || request.requestHeaders.any { (key, value) ->
@@ -374,7 +380,12 @@ abstract class BaseReactMapFragment : Fragment(), DownloadListener {
374380 setupConnection(request, conn)
375381 ReactMapHttpEngine .writeCompressed(conn, body)
376382 }
377- createResponse(conn) { _ -> conn.findErrorStream }
383+ if (conn.responseCode == 302 ) {
384+ ReactMapHttpEngine .detectBrotliError(conn)?.let {
385+ lifecycleScope.launch { Snackbar .make(web, it, Snackbar .LENGTH_LONG ).show() }
386+ }
387+ null
388+ } else createResponse(conn) { _ -> conn.findErrorStream }
378389 } catch (e: IOException ) {
379390 Timber .d(e)
380391 null
0 commit comments