Skip to content

Commit

Permalink
Add Regex skipPath/skipDomain to Builder, fixes #1236 (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
VenomVendor committed Jul 16, 2024
1 parent 6c9b24a commit 5261314
Show file tree
Hide file tree
Showing 10 changed files with 517 additions and 91 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Please add your entries according to this format.

### Added
* Added _save as text_ and _save as .har file_ options to save all transactions [#1214]
* Added `skipPaths(paths Regex)`, `skipDomains(domains Regex)` and `skipDomains(domains... String)` to skip paths/domains from chucker [#1236]

### Fixed
* Change GSON `TypeToken` creation to allow using Chucker in builds optimized by R8 [#1166]
Expand Down
1 change: 1 addition & 0 deletions detekt-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ complexity:
active: true
ignoreDefaultParameters: true
TooManyFunctions:
thresholdInClasses: 13
active: true
ignoreOverridden: true

Expand Down
3 changes: 3 additions & 0 deletions library-no-op/api/library-no-op.api
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public final class com/chuckerteam/chucker/api/ChuckerInterceptor$Builder {
public final fun maxContentLength (J)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun redactHeaders (Ljava/lang/Iterable;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun redactHeaders ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipDomains (Lkotlin/text/Regex;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipDomains ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipPaths (Lkotlin/text/Regex;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipPaths ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public class ChuckerInterceptor private constructor(

public fun skipPaths(vararg paths: String): Builder = this

public fun skipPaths(paths: Regex): Builder = this

public fun skipDomains(vararg domains: String): Builder = this

public fun skipDomains(domains: Regex): Builder = this

public fun build(): ChuckerInterceptor = ChuckerInterceptor(this)
}
}
3 changes: 3 additions & 0 deletions library/api/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public final class com/chuckerteam/chucker/api/ChuckerInterceptor$Builder {
public final fun maxContentLength (J)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun redactHeaders (Ljava/lang/Iterable;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun redactHeaders ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipDomains (Lkotlin/text/Regex;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipDomains ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipPaths (Lkotlin/text/Regex;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
public final fun skipPaths ([Ljava/lang/String;)Lcom/chuckerteam/chucker/api/ChuckerInterceptor$Builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class ChuckerInterceptor private constructor(
)

private val skipPaths = builder.skipPaths.toSet()
private val skipPathsRegex = builder.skipPathsRegex.toSet()
private val skipDomains = builder.skipDomains.toSet()
private val skipDomainRegex = builder.skipDomainRegex.toSet()

init {
if (builder.createShortcut) {
Expand All @@ -72,7 +75,14 @@ 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 }
val path = request.url.encodedPath
val host = request.url.host
val shouldSkipPath = skipPaths.contains(path) || skipPathsRegex.any { it.matches(path) }
// Skip evaluation of domain if the path is already skipped
val shouldSkipDomain =
shouldSkipPath || skipDomains.contains(host) || skipDomainRegex.any { it.matches(host) }
val shouldProcessTheRequest = !(shouldSkipPath || shouldSkipDomain)

if (shouldProcessTheRequest) {
requestProcessor.process(request, transaction)
}
Expand Down Expand Up @@ -105,7 +115,10 @@ 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>()
internal val skipDomains = mutableSetOf<String>()
internal val skipDomainRegex = mutableSetOf<Regex>()

/**
* Sets the [ChuckerCollector] to customize data retention.
Expand Down Expand Up @@ -183,9 +196,13 @@ public class ChuckerInterceptor private constructor(
this.cacheDirectoryProvider = provider
}

public fun skipPaths(vararg skipPaths: String): Builder =
/**
* Sets a list of [String] to skip paths. When any of the [String] matches
* a request path, the request will be skipped.
*/
public fun skipPaths(vararg paths: String): Builder =
apply {
skipPaths.forEach { candidatePath ->
paths.forEach { candidatePath ->
val httpUrl =
HttpUrl.Builder()
.scheme("https")
Expand All @@ -196,7 +213,56 @@ public class ChuckerInterceptor private constructor(
}

/**
* Creates a new [ChuckerInterceptor] instance with values defined in this builder.
* Sets a list of [Regex] to skip paths. When any of the [Regex] matches a
* request path, the request will be skipped. Include [RegexOption] where
* necessary.
*
* ```
* ".*(jpg|jpeg|png|gif|webp|svg|bmp|ico)$".toRegex(), // Ignore all image requests
* ".*iGnOrE.*"toRegex(RegexOption.IGNORE_CASE), // Case insensitive
* ".*path/to/skip.*".toRegex(),
* ".*path/ends/with/dev$".toRegex(),
* ```
*/
public fun skipPaths(paths: Regex): Builder =
apply {
this.skipPathsRegex.add(paths)
}

/**
* Sets a list of [String] to skip domains. Domain names are evaluated in
* lowercase format. When any of the [String] matches a request domain, the
* request will be skipped.
*
* ```
* example.com, subdomain.example.com, exam-ple.com, eXaMpLe.CoM, example.co.uk
* ```
*/
public fun skipDomains(vararg domains: String): Builder =
apply {
this@Builder.skipDomains.addAll(domains.map { it.lowercase() })
}

/**
* Sets a list of [Regex] to skip domains. Domain names are evaluated in
* lowercase format. When any of the [Regex] matches a request domain,
* the request will be skipped. Include [RegexOption] where necessary.
*
* ```
* ".*iGnOrE.*"toRegex(RegexOption.IGNORE_CASE),
* "ignoresubdomain.*".toRegex(),
* "domainname.*".toRegex(),
* ".*.dev$".toRegex(),
* ```
*/
public fun skipDomains(domains: Regex): Builder =
apply {
this.skipDomainRegex.add(domains)
}

/**
* Creates a new [ChuckerInterceptor] instance with values defined in this
* builder.
*/
public fun build(): ChuckerInterceptor = ChuckerInterceptor(this)
}
Expand Down
Loading

0 comments on commit 5261314

Please sign in to comment.