Repository: square/okhttp
File: okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.kt (Kotlin)

Code before:
  private var receivedPongCount = 0

  /** True if we have sent a ping that is still awaiting a reply. */
  private var awaitingPong = false

  init {
    require("GET" == originalRequest.method) {
      "Request must be GET: ${originalRequest.method}"
    }

    this.key = ByteArray(16).apply { random.nextBytes(this) }.toByteString().base64()
  }

  override fun request(): Request = originalRequest

  @Synchronized override fun queueSize(): Long = queueSize

  override fun cancel() {
    call!!.cancel()
  }

  fun connect(client: OkHttpClient) {
    val webSocketClient = client.newBuilder()
        .eventListener(EventListener.NONE)
        .protocols(ONLY_HTTP1)
        .build()
    val request = originalRequest.newBuilder()
        .header("Upgrade", "websocket")
        .header("Connection", "Upgrade")
        .header("Sec-WebSocket-Key", key)
        .header("Sec-WebSocket-Version", "13")
        .build()
    call = RealCall(webSocketClient, request, forWebSocket = true)
    call!!.enqueue(object : Callback {
      override fun onResponse(call: Call, response: Response) {
        val exchange = response.exchange
        val streams: Streams
        try {
          checkUpgradeSuccess(response, exchange)
          streams = exchange!!.newWebSocketStreams()
          // TODO(jwilson): use request & response headers to negotiate extensions.
          extensions = WebSocketExtensions()
        } catch (e: IOException) {
          exchange?.webSocketUpgradeFailed()
          failWebSocket(e, response)
          response.closeQuietly()
          return
        }

        // Process all web socket messages.
        try {

Review comment (bug):
@swankjesse I think this code is a little too restrictive to applications that want to specify their own extensions or other extensions that are not just the `permessage-deflate`.
I would rather merge the extensions provided by the application with the internal `permessage-deflate`, unless the application already specifies `permessage-deflate`. Thoughts?
FTR I have a CometD test that was testing this that fails with OkHttp 4.5.0. Thanks!


Code after:
  private var receivedPongCount = 0

  /** True if we have sent a ping that is still awaiting a reply. */
  private var awaitingPong = false

  init {
    require("GET" == originalRequest.method) {
      "Request must be GET: ${originalRequest.method}"
    }

    this.key = ByteArray(16).apply { random.nextBytes(this) }.toByteString().base64()
  }

  override fun request(): Request = originalRequest

  @Synchronized override fun queueSize(): Long = queueSize

  override fun cancel() {
    call!!.cancel()
  }

  fun connect(client: OkHttpClient) {
    if (originalRequest.header("Sec-WebSocket-Extensions") != null) {
      failWebSocket(ProtocolException(
          "Request header not permitted: 'Sec-WebSocket-Extensions'"), null)
      return
    }

    val webSocketClient = client.newBuilder()
        .eventListener(EventListener.NONE)
        .protocols(ONLY_HTTP1)
        .build()
    val request = originalRequest.newBuilder()
        .header("Upgrade", "websocket")
        .header("Connection", "Upgrade")
        .header("Sec-WebSocket-Key", key)
        .header("Sec-WebSocket-Version", "13")
        .header("Sec-WebSocket-Extensions", "permessage-deflate")
        .build()
    call = RealCall(webSocketClient, request, forWebSocket = true)
    call!!.enqueue(object : Callback {
      override fun onResponse(call: Call, response: Response) {
        val exchange = response.exchange
        val streams: Streams
        try {
          checkUpgradeSuccess(response, exchange)
          streams = exchange!!.newWebSocketStreams()
        } catch (e: IOException) {
          exchange?.webSocketUpgradeFailed()
          failWebSocket(e, response)
          response.closeQuietly()