javaagent icon indicating copy to clipboard operation
javaagent copied to clipboard

OkHTTP3 - fix response charset and adding support for gzip compression

Open thugrock7 opened this issue 1 year ago • 0 comments

Description

In OkHTTP Tracing interceptor, while capturing response body it is trying to attempt response().body().string(), the behaviour of string() function for non-BOM response is, if the response has a Content-Type header that specifies a charset, that is used else defaults to utf-8. For cases when original response is encoded in gzip this causes response parsing failure as the response that interceptor returns is one with modified type/charset and results in below error in application.

Caused by: java.io.IOException: ID1ID2: actual 0x00001fef != expected 0x00001f8b at okio.GzipSource.checkEqual(GzipSource.kt:197) at okio.GzipSource.consumeHeader(GzipSource.kt:110) at okio.GzipSource.read(GzipSource.kt:62) at okio.RealBufferedSource.select(RealBufferedSource.kt:232) at okhttp3.internal.Util.readBomAsCharset(Util.kt:265) at okhttp3.ResponseBody.string(ResponseBody.kt:187) at com.auth0.net.CustomRequest.readResponseBody(CustomRequest.java:58) at com.auth0.net.ExtendedBaseRequest.parseResponse(ExtendedBaseRequest.java:70) ... 2 more

ref: https://square.github.io/okhttp/3.x/okhttp/okhttp3/ResponseBody.html Other issue, is that in case of gzip encoded response, we need to decompress it read response in order to set as span attribute correctly.

Testing

Verified with response being of type gzip encoded and application/json. Verified for non-utf8 parsing

thugrock7 avatar Aug 05 '24 08:08 thugrock7