Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore domains and paths from interceptor #1236

Closed
2 tasks done
VenomVendor opened this issue Jun 21, 2024 · 7 comments · Fixed by #1242
Closed
2 tasks done

Ignore domains and paths from interceptor #1236

VenomVendor opened this issue Jun 21, 2024 · 7 comments · Fixed by #1242
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@VenomVendor
Copy link
Contributor

VenomVendor commented Jun 21, 2024

⚠️ Is your feature request related to a problem? Please describe

💡 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
    • /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
@cortinico
Copy link
Member

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.

@VenomVendor
Copy link
Contributor Author

VenomVendor commented Jun 22, 2024

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.
          */

regex-path-domain.patch

@cortinico
Copy link
Member

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?

@VenomVendor
Copy link
Contributor Author

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).""")

@cortinico
Copy link
Member

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?

@VenomVendor
Copy link
Contributor Author

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()

@cortinico
Copy link
Member

To confirm this statement, do you mean existing skipPaths(... String) or proposed skipPaths(... 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants