-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add limiting source * Move request processing to a separate class - Limit content length with limiting source. - Do not assume that request has plain text body. * Simplify test name
- Loading branch information
Showing
12 changed files
with
230 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
library/src/main/java/com/chuckerteam/chucker/internal/support/IOUtils.kt
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
library/src/main/java/com/chuckerteam/chucker/internal/support/LimitingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.chuckerteam.chucker.internal.support | ||
|
||
import okio.Buffer | ||
import okio.ForwardingSource | ||
import okio.Source | ||
|
||
internal class LimitingSource( | ||
delegate: Source, | ||
private val bytesCountThreshold: Long, | ||
) : ForwardingSource(delegate) { | ||
private var bytesRead = 0L | ||
val isThresholdReached get() = bytesRead >= bytesCountThreshold | ||
|
||
override fun read(sink: Buffer, byteCount: Long) = if (!isThresholdReached) { | ||
super.read(sink, byteCount).also { bytesRead += it } | ||
} else { | ||
-1L | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
library/src/main/java/com/chuckerteam/chucker/internal/support/RequestProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.chuckerteam.chucker.internal.support | ||
|
||
import android.content.Context | ||
import com.chuckerteam.chucker.R | ||
import com.chuckerteam.chucker.api.ChuckerCollector | ||
import com.chuckerteam.chucker.internal.data.entity.HttpTransaction | ||
import okhttp3.Request | ||
import okio.Buffer | ||
import okio.IOException | ||
import kotlin.text.Charsets.UTF_8 | ||
|
||
internal class RequestProcessor( | ||
private val context: Context, | ||
private val collector: ChuckerCollector, | ||
private val maxContentLength: Long, | ||
) { | ||
fun process(request: Request, transaction: HttpTransaction): Request { | ||
processMetadata(request, transaction) | ||
processBody(request, transaction) | ||
collector.onRequestSent(transaction) | ||
return request | ||
} | ||
|
||
private fun processMetadata(request: Request, transaction: HttpTransaction) { | ||
transaction.apply { | ||
setRequestHeaders(request.headers) | ||
populateUrl(request.url) | ||
|
||
requestDate = System.currentTimeMillis() | ||
method = request.method | ||
requestContentType = request.body?.contentType()?.toString() | ||
requestPayloadSize = request.body?.contentLength() | ||
} | ||
} | ||
|
||
private fun processBody(request: Request, transaction: HttpTransaction) { | ||
val body = request.body ?: return | ||
|
||
val isEncodingSupported = request.headers.hasSupportedContentEncoding | ||
if (!isEncodingSupported) { | ||
return | ||
} | ||
|
||
val limitingSource = try { | ||
Buffer().apply { body.writeTo(this) } | ||
} catch (e: IOException) { | ||
Logger.error("Failed to read request payload", e) | ||
return | ||
}.uncompress(request.headers).let { LimitingSource(it, maxContentLength) } | ||
|
||
val contentBuffer = Buffer().apply { limitingSource.use { writeAll(it) } } | ||
if (!contentBuffer.isProbablyPlainText) { | ||
return | ||
} | ||
|
||
transaction.isRequestBodyPlainText = true | ||
try { | ||
transaction.requestBody = contentBuffer.readString(body.contentType()?.charset() ?: UTF_8) | ||
} catch (e: IOException) { | ||
Logger.error("Failed to process request payload", e) | ||
} | ||
if (limitingSource.isThresholdReached) { | ||
transaction.requestBody += context.getString(R.string.chucker_body_content_truncated) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.