-
Notifications
You must be signed in to change notification settings - Fork 388
Description
In our application we have previously been using the default websocket implementation but are looking to swap it out with the OkHttpWebSocket
from the ok_http
package. When testing this transition I started noticing the socket being frequently closed with 1002 Protocol Errors. After inspecting traffic I noticed that okhttp was attempting to send compressed messages (and setting the corresponding reserved bit) despite the server not supporting (and not sending the permessage-deflate
header in the response) compression.
On digging a bit further I noticed the ok_http
package is manually adding the permessage-deflate
extension header to all responses regardless of the server response via the WebSocketInterceptor
:
class WebSocketInterceptor {
companion object {
fun addWSInterceptor(
clientBuilder: OkHttpClient.Builder
): OkHttpClient.Builder {
return clientBuilder.addInterceptor(Interceptor { chain ->
val request = chain.request()
val response = chain.proceed(request)
response.newBuilder()
// Removing this header to ensure that OkHttp does not fail due to unexpected values.
.removeHeader("sec-websocket-extensions")
// Adding the header to ensure successful parsing of the response.
.addHeader("sec-websocket-extensions", "permessage-deflate").build()
})
}
}
}
This interceptor is always added to the client on calling connect
and I didn't see a way to avoid it.
Disabling this locally by modifying the interceptor resolves the issue in testing but I'm trying to understand why this is done instead of checking the response headers and only adding the permessage-deflate
extension if it was present in the original server response. Is there a reason why this would always be needed?