Skip to content

Instantly share code, notes, and snippets.

@StrixG
Created April 25, 2024 15:24
Show Gist options
  • Save StrixG/9366b3d7797db8015791da320a96ca1f to your computer and use it in GitHub Desktop.
Save StrixG/9366b3d7797db8015791da320a96ca1f to your computer and use it in GitHub Desktop.
class DeflateInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
return inflate(response)
}
private fun inflate(response: Response): Response {
val body = response.body ?: return response
val contentEncoding = response.headers["Content-Encoding"]
return if (contentEncoding != null && contentEncoding == "deflate") {
val contentLength = body.contentLength()
val inflaterSource = body.source().inflate(Inflater(true))
val responseBody = RealResponseBody(
body.contentType().toString(),
contentLength,
inflaterSource.buffer()
)
response.newBuilder()
.body(responseBody)
.build()
} else response
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment