Created
April 25, 2024 15:24
-
-
Save StrixG/9366b3d7797db8015791da320a96ca1f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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