Skip to content

Commit

Permalink
Read buffer prefix before potentially gunzipping it.
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Mar 6, 2020
1 parent 6e2406d commit 4751cf0
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ChuckerInterceptor internal constructor(
return processResponseMetadata(response, transaction) { file ->
val buffer = readResponseBuffer(file, response.isGzipped)
file.delete()
processResponseBody(buffer, response, transaction)
if (buffer != null) processResponseBody(buffer, response, transaction)
collector.onResponseReceived(transaction)
}
}
Expand Down Expand Up @@ -191,7 +191,6 @@ class ChuckerInterceptor internal constructor(
response: Response,
transaction: HttpTransaction
) {
if (responseBodyBuffer.readUtf8Line() != TeeSource.PREFIX_OK) return
val responseBody = response.body() ?: return

val contentType = responseBody.contentType()
Expand Down Expand Up @@ -225,11 +224,14 @@ class ChuckerInterceptor internal constructor(
return builder.build()
}

private fun readResponseBuffer(responseBody: File, isGizipped: Boolean): Buffer {
private fun readResponseBuffer(responseBody: File, isGizipped: Boolean): Buffer? {
val bufferedSource = Okio.buffer(Okio.source(responseBody))
if (bufferedSource.readUtf8Line() != TeeSource.PREFIX_OK) return null

val source = if (isGizipped) {
GzipSource(Okio.source(responseBody))
GzipSource(bufferedSource)
} else {
Okio.source(responseBody)
bufferedSource
}
return Buffer().apply {
writeAll(source)
Expand Down

0 comments on commit 4751cf0

Please sign in to comment.