Repository: square/okhttp
File: mockwebserver/src/main/kotlin/okhttp3/mockwebserver/MockWebServer.kt (Kotlin)

Code before:
        if (name == Header.TARGET_METHOD_UTF8) {
          method = value
        } else if (name == Header.TARGET_PATH_UTF8) {
          path = value
        } else if (protocol === Protocol.HTTP_2 || protocol === Protocol.H2_PRIOR_KNOWLEDGE) {
          httpHeaders.add(name, value)
        } else {
          throw IllegalStateException()
        }
        if (name == "expect" && value.equals("100-continue", ignoreCase = true)) {
          // Don't read the body unless we've invited the client to send it.
          readBody = false
        }
      }
      val headers = httpHeaders.build()

      val peek = dispatcher.peek()
      if (!readBody && peek.socketPolicy === EXPECT_CONTINUE) {
        val continueHeaders =
            listOf(Header(Header.RESPONSE_STATUS, "100 Continue".encodeUtf8()))
        stream.writeHeaders(continueHeaders, outFinished = false, flushHeaders = true)
        stream.connection.flush()
        readBody = true
      }

      val body = Buffer()
      val requestLine = "$method $path HTTP/1.1"
      var exception: IOException? = null
      if (readBody && !peek.isDuplex && peek.socketPolicy !== DO_NOT_READ_REQUEST_BODY) {
        try {
          val contentLengthString = headers["content-length"]
          val byteCount = contentLengthString?.toLong() ?: Long.MAX_VALUE
          throttledTransfer(peek, socket, stream.getSource().buffer(),
              body, byteCount, true)
        } catch (e: IOException) {
          exception = e
        }
      }

      return RecordedRequest(requestLine, headers, emptyList(), body.size, body,
          sequenceNumber.getAndIncrement(), socket, exception)
    }

    @Throws(IOException::class)
    private fun writeResponse(
      stream: Http2Stream,
      request: RecordedRequest,
      response: MockResponse
    ) {
      val settings = response.settings
      stream.connection.setSettings(settings)

Review comment (bug):
FWIW, I dislike `forEach` as an alternative to `for(...)`. It’s needlessly complex to step-debug!

Code after:
        if (name == Header.TARGET_METHOD_UTF8) {
          method = value
        } else if (name == Header.TARGET_PATH_UTF8) {
          path = value
        } else if (protocol === Protocol.HTTP_2 || protocol === Protocol.H2_PRIOR_KNOWLEDGE) {
          httpHeaders.add(name, value)
        } else {
          throw IllegalStateException()
        }
        if (name == "expect" && value.equals("100-continue", ignoreCase = true)) {
          // Don't read the body unless we've invited the client to send it.
          readBody = false
        }
      }
      val headers = httpHeaders.build()

      val peek = dispatcher.peek()
      if (!readBody && peek.socketPolicy === EXPECT_CONTINUE) {
        val continueHeaders =
            listOf(Header(Header.RESPONSE_STATUS, "100 Continue".encodeUtf8()))
        stream.writeHeaders(continueHeaders, outFinished = false, flushHeaders = true)
        stream.connection.flush()
        readBody = true
      }

      peek.informationalResponses.forEach {
        val informationalHeader =
          listOf(Header(Header.RESPONSE_STATUS, it.status.replace("HTTP/1.1 ", "")))
        stream.writeHeaders(informationalHeader, outFinished = false, flushHeaders = true)
        stream.connection.flush()
      }

      val body = Buffer()
      val requestLine = "$method $path HTTP/1.1"
      var exception: IOException? = null
      if (readBody && !peek.isDuplex && peek.socketPolicy !== DO_NOT_READ_REQUEST_BODY) {
        try {
          val contentLengthString = headers["content-length"]
          val byteCount = contentLengthString?.toLong() ?: Long.MAX_VALUE
          throttledTransfer(peek, socket, stream.getSource().buffer(),
              body, byteCount, true)
        } catch (e: IOException) {
          exception = e
        }
      }

      return RecordedRequest(requestLine, headers, emptyList(), body.size, body,
          sequenceNumber.getAndIncrement(), socket, exception)
    }

    @Throws(IOException::class)