Skip to content

ok_http websocket protocol issues when compression is not supported on the server #1782

@taylorbell

Description

@taylorbell

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions