Skip to content

Commit

Permalink
Set Content-Length header for gzip bodies because of more strict …
Browse files Browse the repository at this point in the history
…proxies (#86)
  • Loading branch information
marandaneto authored Jan 19, 2024
1 parent 68662a9 commit eabbf07
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- Set `Content-Length` header for gzip bodies because of more strict proxies ([#86](https://github.com/PostHog/posthog-android/pull/86))

## 3.1.3 - 2024-01-17

- Do not capture console logs and network requests if session replay and session are not active ([#83](https://github.com/PostHog/posthog-android/pull/83))
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/PosthogBuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object PosthogBuildConfig {
// runtime
val LIFECYCLE = "2.6.2"
val GSON = "2.10.1"
val OKHTTP = "4.11.0"
val OKHTTP = "4.12.0"
val CURTAINS = "1.2.4"
val ANDROIDX_CORE = "1.5.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.Response
import okio.Buffer
import okio.BufferedSink
import okio.GzipSink
import okio.buffer
Expand All @@ -50,7 +51,7 @@ internal class GzipRequestInterceptor(private val config: PostHogConfig) : Inter
val compressedRequest = try {
originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method, gzip(body))
.method(originalRequest.method, forceContentLength(gzip(body)))
.build()
} catch (e: Throwable) {
config.logger.log("Failed to gzip the request body: $e.")
Expand Down Expand Up @@ -79,4 +80,26 @@ internal class GzipRequestInterceptor(private val config: PostHogConfig) : Inter
}
}
}

// https://github.com/square/okhttp/issues/350
@Throws(IOException::class)
private fun forceContentLength(body: RequestBody): RequestBody {
val buffer = Buffer()
body.writeTo(buffer)

return object : RequestBody() {
override fun contentType(): MediaType? {
return body.contentType()
}

override fun contentLength(): Long {
return buffer.size
}

@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
sink.write(buffer.snapshot())
}
}
}
}

0 comments on commit eabbf07

Please sign in to comment.