Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Content-Length header for gzip bodies because of more strict proxies #86

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}
}
}