Repository: square/okhttp
File: okhttp/src/jvmMain/kotlin/okhttp3/internal/connection/ConnectPlan.kt (Kotlin)

Code before:
    check(!isReady) { "already connected" }

    val connectionSpecs = route.address.connectionSpecs
    var retryTlsConnection: ConnectPlan? = null
    var success = false

    // Tell the call about the connecting call so async cancels work.
    call.plansToCancel += this
    try {
      if (tunnelRequest != null) {
        val tunnelResult = connectTunnel()

        // Tunnel didn't work. Start it all again.
        if (tunnelResult.nextPlan != null || tunnelResult.throwable != null) {
          return tunnelResult
        }
      }

      if (route.address.sslSocketFactory != null) {
        eventListener.secureConnectStart(call)

        // Create the wrapper over the connected socket.
        val sslSocket = route.address.sslSocketFactory.createSocket(
          rawSocket,
          route.address.url.host,
          route.address.url.port,
          true /* autoClose */
        ) as SSLSocket

        val tlsEquipPlan = planWithCurrentOrInitialConnectionSpec(connectionSpecs, sslSocket)
        val connectionSpec = connectionSpecs[tlsEquipPlan.connectionSpecIndex]

        // Figure out the next connection spec in case we need a retry.
        retryTlsConnection = tlsEquipPlan.nextConnectionSpec(connectionSpecs, sslSocket)

        connectionSpec.apply(sslSocket, isFallback = tlsEquipPlan.isTlsFallback)
        connectTls(sslSocket, connectionSpec)
        eventListener.secureConnectEnd(call, handshake)
      } else {
        socket = rawSocket
        protocol = when {
          Protocol.H2_PRIOR_KNOWLEDGE in route.address.protocols -> Protocol.H2_PRIOR_KNOWLEDGE
          else -> Protocol.HTTP_1_1
        }
      }

      val connection = RealConnection(
        taskRunner = client.taskRunner,
        connectionPool = client.connectionPool.delegate,
        route = route,
        rawSocket = rawSocket,

Review comment (refactor):
FYI @yschimke I had to move this check to get tests to stop flaking. In TLS handshakes the server doesn’t send until it receives data from the client. In HTTP/2 the server and client can both send immediately!

Code after:
    check(!isReady) { "already connected" }

    val connectionSpecs = route.address.connectionSpecs
    var retryTlsConnection: ConnectPlan? = null
    var success = false

    // Tell the call about the connecting call so async cancels work.
    call.plansToCancel += this
    try {
      if (tunnelRequest != null) {
        val tunnelResult = connectTunnel()

        // Tunnel didn't work. Start it all again.
        if (tunnelResult.nextPlan != null || tunnelResult.throwable != null) {
          return tunnelResult
        }
      }

      if (route.address.sslSocketFactory != null) {
        // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If
        // that happens, then we will have buffered bytes that are needed by the SSLSocket!
        // This check is imperfect: it doesn't tell us whether a handshake will succeed, just
        // that it will almost certainly fail because the proxy has sent unexpected data.
        if (source?.buffer?.exhausted() == false || sink?.buffer?.exhausted() == false) {
          throw IOException("TLS tunnel buffered too many bytes!")
        }

        eventListener.secureConnectStart(call)

        // Create the wrapper over the connected socket.
        val sslSocket = route.address.sslSocketFactory.createSocket(
          rawSocket,
          route.address.url.host,
          route.address.url.port,
          true /* autoClose */
        ) as SSLSocket

        val tlsEquipPlan = planWithCurrentOrInitialConnectionSpec(connectionSpecs, sslSocket)
        val connectionSpec = connectionSpecs[tlsEquipPlan.connectionSpecIndex]

        // Figure out the next connection spec in case we need a retry.
        retryTlsConnection = tlsEquipPlan.nextConnectionSpec(connectionSpecs, sslSocket)

        connectionSpec.apply(sslSocket, isFallback = tlsEquipPlan.isTlsFallback)
        connectTls(sslSocket, connectionSpec)
        eventListener.secureConnectEnd(call, handshake)
      } else {
        socket = rawSocket
        protocol = when {
          Protocol.H2_PRIOR_KNOWLEDGE in route.address.protocols -> Protocol.H2_PRIOR_KNOWLEDGE
          else -> Protocol.HTTP_1_1