Ignore domains and paths from interceptor
VenomVendor opened this issue · 7 comments
⚠️ Is your feature request related to a problem? Please describe
- Similar to
skipPaths
#266 allow chucker to skip regex of paths and domains. - Our single Okhttp client handles API calls, image requests and blob content. I'd want to skip calls made to assets or other external, sub-domains.
💡 Describe the solution you'd like
- Provide
fun skipDomains(vararg pattern: kotlin.text.Regex)
to skip the domains user are not intereseted in tapping. - overload
fun skipPaths(vararg pattern: kotlin.text.Regex)
with Regex, this way user can skip parts of path that they are not interested.
📊 Describe alternatives you've considered
- No alternatives.
📄 Additional context
-
Provide
fun skipDomains(vararg pattern: kotlin.text.Regex)
to skip the domains user are not intereseted in tapping. -
Below are possible domains, subdomains that user is not interested.
assets.example.com
dam.example.com
notinterested.example.com
analytics.example.com
thirdparty.com
-
overload
fun skipPaths(vararg pattern: kotlin.text.Regex)
with Regex, this way user can skip parts of path that they are not interested.v1/not/very/interesting/path
v2/not/very/interesting/path/maybe
- Currently no way to exclude paths contaning
not/very/interesting
- Currently no way to exclude paths contaning
/important/but/i/dont/care/
/dynamic/path/from/dam/assets/my-image.jpg
/dynamic/path/from/dam/assets/my-image.png
- This is where it all started, couldn't find an api to ignore image requests.
Initially I considered String
over regex. But, regex gives more flexibility to the user to ignore more with less. ex: ".*(jpg|jpeg|png|gif|webp)$".toRegex()
in one. If this was string .jpg
, .jpeg
, .png
, .gif
, .webp
would need 5 loops.
* "not/very/important/in/here" // <-- Ingore
* "not/very/important/in/here/not/important/at/all", // <-- Ignore
* "not/very/important/in/here/but/important" // <-- DO NOT INGORE
* "not/very/important/in/here/also/but/many-be/important/req" // <-- DO NOT INGORE
Regex("""^not/very/important/in/here(?!/but.*important).*""")
🙋 Do you want to develop this feature yourself?
- Yes
- No
Provide fun skipDomains(vararg pattern: kotlin.text.Regex) to skip the domains user are not intereseted in tapping.
This sounds reasonable to add.
overload fun skipPaths(vararg pattern: kotlin.text.Regex) with Regex, this way user can skip parts of path that they are not interested.
This feels like an overkill to me and not something we probably want to maintain/test, etc.
Ignoring the path is more important for me than domian.
/dynamic/path/from/dam/assets/my-image.jpg
/dynamic/path/from/dam/assets/my-image.png
- This is where it all started, couldn't find an api to ignore image requests.
We would be receiving the Regex from App, hence we only call regex.matches
on path.
Did an initial draft, we're adding 5 lines in source for path check.
diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt
index 10a6e9c..7e0f03f 100644
--- a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt
+++ b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt
@@ -56,6 +56,7 @@ public class ChuckerInterceptor private constructor(
)
private val skipPaths = builder.skipPaths.toSet()
+ private val skipPathsRegex = builder.skipPathsRegex.toSet()
init {
if (builder.createShortcut) {
@@ -72,8 +73,9 @@ public class ChuckerInterceptor private constructor(
override fun intercept(chain: Interceptor.Chain): Response {
val transaction = HttpTransaction()
val request = chain.request()
- val shouldProcessTheRequest = !skipPaths.any { it == request.url.encodedPath }
- if (shouldProcessTheRequest) {
+ val shouldSkipPath = skipPaths.contains(request.url.encodedPath) || skipPathsRegex.any { it.matches(request.url.encodedPath) }
+
+ if (!shouldSkipPath) {
requestProcessor.process(request, transaction)
}
val response =
@@ -84,7 +86,7 @@ public class ChuckerInterceptor private constructor(
collector.onResponseReceived(transaction)
throw e
}
- return if (shouldProcessTheRequest) {
+ return if (!shouldSkipPath) {
responseProcessor.process(response, transaction)
} else {
response
@@ -105,7 +107,8 @@ public class ChuckerInterceptor private constructor(
internal var headersToRedact = emptySet<String>()
internal var decoders = emptyList<BodyDecoder>()
internal var createShortcut = true
- internal var skipPaths = mutableSetOf<String>()
+ internal val skipPaths = mutableSetOf<String>()
+ internal val skipPathsRegex = mutableSetOf<Regex>()
/**
* Sets the [ChuckerCollector] to customize data retention.
@@ -195,6 +198,11 @@ public class ChuckerInterceptor private constructor(
}
}
+ public fun skipPaths(vararg skipPaths: Regex): Builder =
+ apply {
+ this@Builder.skipPathsRegex.addAll(skipPaths.toSet())
+ }
+
/**
* Creates a new [ChuckerInterceptor] instance with values defined in this builder.
*/
Ignoring the path is more important for me than domian.
Can you give me some real world examples of paths you want to ignore with Regex just to understand your use case?
Some of the domain for these paths are well known, sometimes they are dynamically named by cloud provider. ex: CloudFront
Possible Paths for the images.
/assets/lob-type/module-name/year/feature-name/tile_type_tile_name-sizeX-image-name.jpg
/assets/lob-type/module-name/year/feature-name/tile_type_tile_name-sizeX-image-name.png
/assets/lob-type/module-name/year/feature-name/tile_type_tile_name-sizeX-image-name.gif
/content/dam/my-company/creative/type/feature/name_lang_mobile_2x.jpeg
/contentful/random/folder/name_lang_mobile_2x.webp
/b5912c198d289129248318eaeb9c4000ba42ac6e/2018/10/15/my-intro-image-1024x624.png
/lob/product-pages/tile_type_tile_name_01_image-name.b5912c198d289129248318eaeb9c4000ba42ac6e.png
Ignore the below info, it is not relavant.
"not/very/important/in/here" // <-- Ingore
"not/very/important/in/here/not/important/at/all", // <-- Ignore
"not/very/important/in/here/but/important" // <-- DO NOT INGORE
"not/very/important/in/here/also/but/many-be/important/req" // <-- DO NOT INGORE
Regex("""^not/very/important/in/here(?!/but.important).""")
Other than:
/b5912c198d289129248318eaeb9c4000ba42ac6e/2018/10/15/my-intro-image-1024x624.png
all the others can be easily achieved with skipPaths
, no?
What would be your skipPaths
regex then? Something like *.png
?
all the others can be easily achieved with skipPaths, no?
To confirm this statement, do you mean existing skipPaths(... String)
or proposed skipPaths(... Regex)
?
Regex for skipPaths(... Regex)
would be ".*(jpg|jpeg|png|gif|webp)$".toRegex()
To confirm this statement, do you mean existing
skipPaths(... String)
or proposedskipPaths(... Regex)
?
I meant the already existing skipPaths(... String)
Anyway, I think it's reasonable to add skipPaths(... Regex)
to handle scenarios like the one you presented 👍 Would you be up for sending a PR?