From ba1f029df4421bf3f22a302b45811835686f45f0 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Mon, 28 Oct 2024 14:38:24 +0100 Subject: [PATCH] KTOR-7606 Make Closable implement AutoCloseable (#4414) * Use stdlib `use` extension-function * Update type checks --- .../src/io/ktor/client/engine/cio/utils.kt | 6 ++--- .../jvmAndPosix/test/CIOEngineTest.kt | 7 ++--- .../jvmAndPosix/test/ConnectionFactoryTest.kt | 7 ++--- .../common/src/io/ktor/client/HttpClient.kt | 6 ++--- .../client/engine/HttpClientEngineBase.kt | 20 ++------------ .../plugins/cache/storage/FileCacheStorage.kt | 3 +-- .../engine/curl/test/CurlNativeTests.kt | 6 ++--- .../client/engine/curl/test/CurlProxyTest.kt | 5 ++-- .../tests/utils/CommonClientTestUtils.kt | 3 +-- .../winhttp/internal/WinHttpResponseData.kt | 4 +-- .../client/engine/winhttp/WinHttpProxyTest.kt | 5 ++-- ktor-io/api/ktor-io.klib.api | 2 +- .../src/io/ktor/utils/io/core/Closeable.kt | 26 ++++--------------- .../src/io/ktor/utils/io/core/Closeable.js.kt | 4 +-- .../io/ktor/utils/io/core/Closeable.posix.kt | 4 +-- .../io/ktor/utils/io/core/Closeable.wasmJs.kt | 4 +-- .../network/sockets/tests/TCPSocketTest.kt | 3 +-- .../ktor/network/sockets/tests/TestUtils.kt | 3 +-- .../network/sockets/tests/UDPSocketTest.kt | 3 ++- .../io/ktor/network/tls/TLSClientHandshake.kt | 5 ++-- .../network/tls/certificates/Certificates.kt | 3 ++- .../ktor/network/tls/certificates/builders.kt | 3 +-- ktor-server/ktor-server-cio/build.gradle.kts | 6 ++++- .../io/ktor/server/cio/backend/HttpServer.kt | 6 ++--- .../io/ktor/tests/server/cio/CIOEngineTest.kt | 5 ++-- .../server/application/ApplicationPlugin.kt | 7 +++-- .../io/ktor/tests/auth/SessionAuthTest.kt | 3 +-- .../base/EngineTestBase.jsAndWasmShared.kt | 1 - .../server/test/base/EngineTestBaseNix.kt | 1 - .../suites/HttpServerCommonTestSuite.kt | 3 ++- .../server/testing/suites/ContentTestSuite.kt | 5 ++-- .../testing/suites/HttpServerJvmTestSuite.kt | 3 ++- .../testing/suites/SustainabilityTestSuite.kt | 5 ++-- .../testing/suites/WebSocketEngineSuite.kt | 1 + .../jvm/src/io/ktor/util/cio/FileChannels.kt | 4 +-- 35 files changed, 70 insertions(+), 112 deletions(-) diff --git a/ktor-client/ktor-client-cio/jvmAndPosix/src/io/ktor/client/engine/cio/utils.kt b/ktor-client/ktor-client-cio/jvmAndPosix/src/io/ktor/client/engine/cio/utils.kt index 8ec2b097ed1..72a12c9bfea 100644 --- a/ktor-client/ktor-client-cio/jvmAndPosix/src/io/ktor/client/engine/cio/utils.kt +++ b/ktor-client/ktor-client-cio/jvmAndPosix/src/io/ktor/client/engine/cio/utils.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.client.engine.cio @@ -14,8 +14,6 @@ import io.ktor.http.content.* import io.ktor.util.date.* import io.ktor.utils.io.* import io.ktor.utils.io.CancellationException -import io.ktor.utils.io.core.* -import io.ktor.utils.io.errors.* import io.ktor.websocket.* import kotlinx.coroutines.* import kotlinx.io.IOException diff --git a/ktor-client/ktor-client-cio/jvmAndPosix/test/CIOEngineTest.kt b/ktor-client/ktor-client-cio/jvmAndPosix/test/CIOEngineTest.kt index f442d2f88df..8844cac9533 100644 --- a/ktor-client/ktor-client-cio/jvmAndPosix/test/CIOEngineTest.kt +++ b/ktor-client/ktor-client-cio/jvmAndPosix/test/CIOEngineTest.kt @@ -1,8 +1,10 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ + +package io.ktor.client.engine.cio + import io.ktor.client.* -import io.ktor.client.engine.cio.* import io.ktor.client.plugins.sse.* import io.ktor.client.plugins.websocket.* import io.ktor.client.request.* @@ -11,7 +13,6 @@ import io.ktor.http.* import io.ktor.network.selector.* import io.ktor.network.sockets.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* import io.ktor.websocket.* import kotlinx.coroutines.* import kotlinx.coroutines.flow.* diff --git a/ktor-client/ktor-client-cio/jvmAndPosix/test/ConnectionFactoryTest.kt b/ktor-client/ktor-client-cio/jvmAndPosix/test/ConnectionFactoryTest.kt index a4354f66eda..8c82620013f 100644 --- a/ktor-client/ktor-client-cio/jvmAndPosix/test/ConnectionFactoryTest.kt +++ b/ktor-client/ktor-client-cio/jvmAndPosix/test/ConnectionFactoryTest.kt @@ -1,10 +1,11 @@ /* - * Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -import io.ktor.client.engine.cio.* + +package io.ktor.client.engine.cio + import io.ktor.network.selector.* import io.ktor.network.sockets.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlin.test.* diff --git a/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt b/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt index ea78eb49947..bef615db902 100644 --- a/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt +++ b/ktor-client/ktor-client-core/common/src/io/ktor/client/HttpClient.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.client @@ -1361,7 +1361,7 @@ public class HttpClient( @Suppress("UNCHECKED_CAST") val plugin = installedFeatures[key as AttributeKey] - if (plugin is Closeable) { + if (plugin is AutoCloseable) { plugin.close() } } diff --git a/ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngineBase.kt b/ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngineBase.kt index 0ac6ebc76fb..8c7ec428928 100644 --- a/ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngineBase.kt +++ b/ktor-client/ktor-client-core/common/src/io/ktor/client/engine/HttpClientEngineBase.kt @@ -1,11 +1,10 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.client.engine import io.ktor.util.* -import io.ktor.utils.io.core.* import kotlinx.atomicfu.* import kotlinx.coroutines.* import kotlin.coroutines.* @@ -39,19 +38,4 @@ public abstract class HttpClientEngineBase(private val engineName: String) : Htt public class ClientEngineClosedException(override val cause: Throwable? = null) : IllegalStateException("Client already closed") -/** - * Closes [CoroutineDispatcher] if it's [CloseableCoroutineDispatcher] or [Closeable]. - */ -@OptIn(ExperimentalCoroutinesApi::class) -private fun CoroutineDispatcher.close() { - try { - when (this) { - is CloseableCoroutineDispatcher -> close() - is Closeable -> close() - } - } catch (ignore: Throwable) { - // Some closeable dispatchers like Dispatchers.IO can't be closed. - } -} - internal expect fun ioDispatcher(): CoroutineDispatcher diff --git a/ktor-client/ktor-client-core/jvm/src/io/ktor/client/plugins/cache/storage/FileCacheStorage.kt b/ktor-client/ktor-client-core/jvm/src/io/ktor/client/plugins/cache/storage/FileCacheStorage.kt index 40887edfb05..12fef1b75e6 100644 --- a/ktor-client/ktor-client-core/jvm/src/io/ktor/client/plugins/cache/storage/FileCacheStorage.kt +++ b/ktor-client/ktor-client-core/jvm/src/io/ktor/client/plugins/cache/storage/FileCacheStorage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.client.plugins.cache.storage @@ -10,7 +10,6 @@ import io.ktor.util.* import io.ktor.util.collections.* import io.ktor.util.date.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* import io.ktor.utils.io.jvm.javaio.* import kotlinx.coroutines.* import kotlinx.coroutines.sync.* diff --git a/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlNativeTests.kt b/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlNativeTests.kt index 6204db99a37..366ca4da5a7 100644 --- a/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlNativeTests.kt +++ b/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlNativeTests.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.client.engine.curl.test @@ -9,9 +9,7 @@ import io.ktor.client.call.* import io.ktor.client.engine.curl.* import io.ktor.client.request.* import io.ktor.client.statement.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* -import kotlin.native.concurrent.* import kotlin.test.* class CurlNativeTests { diff --git a/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlProxyTest.kt b/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlProxyTest.kt index 73b64693d69..e0d8e2b350e 100644 --- a/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlProxyTest.kt +++ b/ktor-client/ktor-client-curl/desktop/test/io/ktor/client/engine/curl/test/CurlProxyTest.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.client.engine.curl.test @@ -11,7 +11,6 @@ import io.ktor.client.engine.curl.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlin.test.* diff --git a/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt b/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt index 3efe5ea95c8..d2939619200 100644 --- a/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt +++ b/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ @file:Suppress("NO_EXPLICIT_RETURN_TYPE_IN_API_MODE_WARNING", "KDocMissingDocumentation") @@ -8,7 +8,6 @@ package io.ktor.client.tests.utils import io.ktor.client.* import io.ktor.client.engine.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlinx.coroutines.test.* import kotlin.time.Duration.Companion.milliseconds diff --git a/ktor-client/ktor-client-winhttp/windows/src/io/ktor/client/engine/winhttp/internal/WinHttpResponseData.kt b/ktor-client/ktor-client-winhttp/windows/src/io/ktor/client/engine/winhttp/internal/WinHttpResponseData.kt index 0271f8fb464..93df7931549 100644 --- a/ktor-client/ktor-client-winhttp/windows/src/io/ktor/client/engine/winhttp/internal/WinHttpResponseData.kt +++ b/ktor-client/ktor-client-winhttp/windows/src/io/ktor/client/engine/winhttp/internal/WinHttpResponseData.kt @@ -1,16 +1,14 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.client.engine.winhttp.internal -import io.ktor.client.plugins.sse.* import io.ktor.client.request.* import io.ktor.http.* import io.ktor.http.cio.* import io.ktor.util.date.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* import kotlin.coroutines.* internal class WinHttpResponseData( diff --git a/ktor-client/ktor-client-winhttp/windows/test/io/ktor/client/engine/winhttp/WinHttpProxyTest.kt b/ktor-client/ktor-client-winhttp/windows/test/io/ktor/client/engine/winhttp/WinHttpProxyTest.kt index bd977ceb70c..a14241ae849 100644 --- a/ktor-client/ktor-client-winhttp/windows/test/io/ktor/client/engine/winhttp/WinHttpProxyTest.kt +++ b/ktor-client/ktor-client-winhttp/windows/test/io/ktor/client/engine/winhttp/WinHttpProxyTest.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.client.engine.winhttp @@ -8,7 +8,6 @@ import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.engine.* import io.ktor.client.request.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlin.test.* diff --git a/ktor-io/api/ktor-io.klib.api b/ktor-io/api/ktor-io.klib.api index 9702bffedda..cba7dc34d45 100644 --- a/ktor-io/api/ktor-io.klib.api +++ b/ktor-io/api/ktor-io.klib.api @@ -53,7 +53,7 @@ abstract interface <#A: kotlin/Any> io.ktor.utils.io.pool/ObjectPool : kotlin/Au open fun close() // io.ktor.utils.io.pool/ObjectPool.close|close(){}[0] } -abstract interface io.ktor.utils.io.core/Closeable { // io.ktor.utils.io.core/Closeable|null[0] +abstract interface io.ktor.utils.io.core/Closeable : kotlin/AutoCloseable { // io.ktor.utils.io.core/Closeable|null[0] abstract fun close() // io.ktor.utils.io.core/Closeable.close|close(){}[0] } diff --git a/ktor-io/common/src/io/ktor/utils/io/core/Closeable.kt b/ktor-io/common/src/io/ktor/utils/io/core/Closeable.kt index 90567a0231f..043fab49be2 100644 --- a/ktor-io/common/src/io/ktor/utils/io/core/Closeable.kt +++ b/ktor-io/common/src/io/ktor/utils/io/core/Closeable.kt @@ -4,25 +4,9 @@ package io.ktor.utils.io.core -public expect interface Closeable { - public fun close() -} +import kotlin.use as stdlibUse -public inline fun T.use(block: (T) -> R): R { - var closed = false - try { - return block(this) - } catch (cause: Throwable) { - closed = true - try { - this?.close() - } catch (closeException: Throwable) { - cause.addSuppressed(closeException) - } - throw cause - } finally { - if (!closed) { - this?.close() - } - } -} +public expect interface Closeable : AutoCloseable + +@Deprecated("Use stdlib implementation instead. Remove import of this function", ReplaceWith("stdlibUse(block)")) +public inline fun T.use(block: (T) -> R): R = stdlibUse(block) diff --git a/ktor-io/js/src/io/ktor/utils/io/core/Closeable.js.kt b/ktor-io/js/src/io/ktor/utils/io/core/Closeable.js.kt index 09561abbac7..63ae1eeb0f4 100644 --- a/ktor-io/js/src/io/ktor/utils/io/core/Closeable.js.kt +++ b/ktor-io/js/src/io/ktor/utils/io/core/Closeable.js.kt @@ -4,6 +4,6 @@ package io.ktor.utils.io.core -public actual interface Closeable { - public actual fun close() +public actual interface Closeable : AutoCloseable { + override fun close() } diff --git a/ktor-io/posix/src/io/ktor/utils/io/core/Closeable.posix.kt b/ktor-io/posix/src/io/ktor/utils/io/core/Closeable.posix.kt index 09561abbac7..63ae1eeb0f4 100644 --- a/ktor-io/posix/src/io/ktor/utils/io/core/Closeable.posix.kt +++ b/ktor-io/posix/src/io/ktor/utils/io/core/Closeable.posix.kt @@ -4,6 +4,6 @@ package io.ktor.utils.io.core -public actual interface Closeable { - public actual fun close() +public actual interface Closeable : AutoCloseable { + override fun close() } diff --git a/ktor-io/wasmJs/src/io/ktor/utils/io/core/Closeable.wasmJs.kt b/ktor-io/wasmJs/src/io/ktor/utils/io/core/Closeable.wasmJs.kt index 09561abbac7..63ae1eeb0f4 100644 --- a/ktor-io/wasmJs/src/io/ktor/utils/io/core/Closeable.wasmJs.kt +++ b/ktor-io/wasmJs/src/io/ktor/utils/io/core/Closeable.wasmJs.kt @@ -4,6 +4,6 @@ package io.ktor.utils.io.core -public actual interface Closeable { - public actual fun close() +public actual interface Closeable : AutoCloseable { + override fun close() } diff --git a/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TCPSocketTest.kt b/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TCPSocketTest.kt index bece892c452..71f9543899b 100644 --- a/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TCPSocketTest.kt +++ b/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TCPSocketTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.network.sockets.tests @@ -7,7 +7,6 @@ package io.ktor.network.sockets.tests import io.ktor.network.sockets.* import io.ktor.utils.io.* import io.ktor.utils.io.CancellationException -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlinx.io.* import kotlin.test.* diff --git a/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TestUtils.kt b/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TestUtils.kt index 3e7f36121bb..1a255234492 100644 --- a/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TestUtils.kt +++ b/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/TestUtils.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.network.sockets.tests @@ -7,7 +7,6 @@ package io.ktor.network.sockets.tests import io.ktor.network.selector.* import io.ktor.test.dispatcher.* import io.ktor.util.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlin.time.* import kotlin.time.Duration.Companion.seconds diff --git a/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt b/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt index 5c9d784b5d7..1c947038cb6 100644 --- a/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt +++ b/ktor-network/jvmAndPosix/test/io/ktor/network/sockets/tests/UDPSocketTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.network.sockets.tests @@ -11,6 +11,7 @@ import kotlinx.coroutines.* import kotlinx.io.* import kotlin.random.* import kotlin.test.* +import kotlin.use class UDPSocketTest { diff --git a/ktor-network/ktor-network-tls/jvm/src/io/ktor/network/tls/TLSClientHandshake.kt b/ktor-network/ktor-network-tls/jvm/src/io/ktor/network/tls/TLSClientHandshake.kt index a10fe5caf7b..e47b551ca9b 100644 --- a/ktor-network/ktor-network-tls/jvm/src/io/ktor/network/tls/TLSClientHandshake.kt +++ b/ktor-network/ktor-network-tls/jvm/src/io/ktor/network/tls/TLSClientHandshake.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.network.tls @@ -21,6 +21,7 @@ import javax.crypto.* import javax.crypto.spec.* import javax.security.auth.x500.* import kotlin.coroutines.* +import kotlin.use internal class TLSClientHandshake( rawInput: ByteReadChannel, diff --git a/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/Certificates.kt b/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/Certificates.kt index 08d306311be..5c7c295d778 100644 --- a/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/Certificates.kt +++ b/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/Certificates.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.network.tls.certificates @@ -23,6 +23,7 @@ import javax.security.auth.x500.* import kotlin.time.* import kotlin.time.Duration import kotlin.time.Duration.Companion.days +import kotlin.use internal val DEFAULT_PRINCIPAL = X500Principal("CN=localhost, OU=Kotlin, O=JetBrains, C=RU") private val DEFAULT_CA_PRINCIPAL = X500Principal("CN=localhostCA, OU=Kotlin, O=JetBrains, C=RU") diff --git a/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/builders.kt b/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/builders.kt index b8895edcb4c..ebd9ddf7694 100644 --- a/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/builders.kt +++ b/ktor-network/ktor-network-tls/ktor-network-tls-certificates/jvm/src/io/ktor/network/tls/certificates/builders.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.network.tls.certificates @@ -7,7 +7,6 @@ package io.ktor.network.tls.certificates import io.ktor.network.tls.* import io.ktor.network.tls.extensions.* import io.ktor.util.* -import io.ktor.utils.io.core.* import java.io.* import java.net.* import java.security.* diff --git a/ktor-server/ktor-server-cio/build.gradle.kts b/ktor-server/ktor-server-cio/build.gradle.kts index 61445613e95..1629871d5d4 100644 --- a/ktor-server/ktor-server-cio/build.gradle.kts +++ b/ktor-server/ktor-server-cio/build.gradle.kts @@ -1,3 +1,7 @@ +/* + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + description = "" kotlin.sourceSets { @@ -14,11 +18,11 @@ kotlin.sourceSets { api(project(":ktor-server:ktor-server-core")) api(project(":ktor-client:ktor-client-cio")) api(project(":ktor-server:ktor-server-test-suites")) + api(project(":ktor-server:ktor-server-test-base")) } } jvmTest { dependencies { - api(project(":ktor-server:ktor-server-test-base")) api(project(":ktor-server:ktor-server-core", configuration = "testOutput")) api(libs.logback.classic) } diff --git a/ktor-server/ktor-server-cio/jvmAndPosix/src/io/ktor/server/cio/backend/HttpServer.kt b/ktor-server/ktor-server-cio/jvmAndPosix/src/io/ktor/server/cio/backend/HttpServer.kt index f24df8a15fd..3c811d9a72d 100644 --- a/ktor-server/ktor-server-cio/jvmAndPosix/src/io/ktor/server/cio/backend/HttpServer.kt +++ b/ktor-server/ktor-server-cio/jvmAndPosix/src/io/ktor/server/cio/backend/HttpServer.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.server.cio.backend @@ -11,8 +11,6 @@ import io.ktor.server.engine.* import io.ktor.server.engine.internal.* import io.ktor.util.logging.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* -import io.ktor.utils.io.errors.* import kotlinx.coroutines.* import kotlinx.io.IOException import kotlin.time.Duration.Companion.seconds diff --git a/ktor-server/ktor-server-cio/jvmAndPosix/test/io/ktor/tests/server/cio/CIOEngineTest.kt b/ktor-server/ktor-server-cio/jvmAndPosix/test/io/ktor/tests/server/cio/CIOEngineTest.kt index 32ee17291db..da08b330b05 100644 --- a/ktor-server/ktor-server-cio/jvmAndPosix/test/io/ktor/tests/server/cio/CIOEngineTest.kt +++ b/ktor-server/ktor-server-cio/jvmAndPosix/test/io/ktor/tests/server/cio/CIOEngineTest.kt @@ -1,8 +1,8 @@ -// ktlint-disable filename /* - * Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ +// ktlint-disable filename package io.ktor.tests.server.cio import io.ktor.client.statement.* @@ -16,7 +16,6 @@ import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.testing.suites.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* import kotlin.test.* class CIOHttpServerTest : HttpServerCommonTestSuite(CIO) { diff --git a/ktor-server/ktor-server-core/common/src/io/ktor/server/application/ApplicationPlugin.kt b/ktor-server/ktor-server-core/common/src/io/ktor/server/application/ApplicationPlugin.kt index f38ce7ea430..6f8012d4664 100644 --- a/ktor-server/ktor-server-core/common/src/io/ktor/server/application/ApplicationPlugin.kt +++ b/ktor-server/ktor-server-core/common/src/io/ktor/server/application/ApplicationPlugin.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.server.application @@ -8,7 +8,6 @@ import io.ktor.server.routing.* import io.ktor.util.* import io.ktor.util.internal.* import io.ktor.util.pipeline.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* /** @@ -230,7 +229,7 @@ public fun , B : Any, F : Any> A.uninstall( public fun , F : Any> A.uninstallPlugin(key: AttributeKey) { val registry = attributes.getOrNull(pluginRegistryKey) ?: return val instance = registry.getOrNull(key) ?: return - if (instance is Closeable) { + if (instance is AutoCloseable) { instance.close() } registry.remove(key) diff --git a/ktor-server/ktor-server-plugins/ktor-server-auth/common/test/io/ktor/tests/auth/SessionAuthTest.kt b/ktor-server/ktor-server-plugins/ktor-server-auth/common/test/io/ktor/tests/auth/SessionAuthTest.kt index 0fcdefca394..c565add44a4 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-auth/common/test/io/ktor/tests/auth/SessionAuthTest.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-auth/common/test/io/ktor/tests/auth/SessionAuthTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.tests.auth @@ -14,7 +14,6 @@ import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.sessions.* import io.ktor.server.testing.* -import io.ktor.utils.io.core.* import kotlinx.serialization.* import kotlin.test.* diff --git a/ktor-server/ktor-server-test-base/jsAndWasmShared/src/io/ktor/server/test/base/EngineTestBase.jsAndWasmShared.kt b/ktor-server/ktor-server-test-base/jsAndWasmShared/src/io/ktor/server/test/base/EngineTestBase.jsAndWasmShared.kt index fe9b632b017..7f176a56623 100644 --- a/ktor-server/ktor-server-test-base/jsAndWasmShared/src/io/ktor/server/test/base/EngineTestBase.jsAndWasmShared.kt +++ b/ktor-server/ktor-server-test-base/jsAndWasmShared/src/io/ktor/server/test/base/EngineTestBase.jsAndWasmShared.kt @@ -14,7 +14,6 @@ import io.ktor.server.engine.* import io.ktor.server.routing.* import io.ktor.server.testing.* import io.ktor.util.logging.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlin.coroutines.* import kotlin.time.Duration.Companion.seconds diff --git a/ktor-server/ktor-server-test-base/posix/src/io/ktor/server/test/base/EngineTestBaseNix.kt b/ktor-server/ktor-server-test-base/posix/src/io/ktor/server/test/base/EngineTestBaseNix.kt index b575aa151b8..a870cc2cb9e 100644 --- a/ktor-server/ktor-server-test-base/posix/src/io/ktor/server/test/base/EngineTestBaseNix.kt +++ b/ktor-server/ktor-server-test-base/posix/src/io/ktor/server/test/base/EngineTestBaseNix.kt @@ -17,7 +17,6 @@ import io.ktor.server.engine.* import io.ktor.server.routing.* import io.ktor.server.testing.* import io.ktor.util.logging.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import kotlin.coroutines.* import kotlin.time.Duration.Companion.seconds diff --git a/ktor-server/ktor-server-test-suites/common/src/io/ktor/server/testing/suites/HttpServerCommonTestSuite.kt b/ktor-server/ktor-server-test-suites/common/src/io/ktor/server/testing/suites/HttpServerCommonTestSuite.kt index 5b2a6c7a924..cf2ad35dacc 100644 --- a/ktor-server/ktor-server-test-suites/common/src/io/ktor/server/testing/suites/HttpServerCommonTestSuite.kt +++ b/ktor-server/ktor-server-test-suites/common/src/io/ktor/server/testing/suites/HttpServerCommonTestSuite.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.server.testing.suites @@ -31,6 +31,7 @@ import kotlinx.coroutines.* import kotlinx.io.* import kotlin.coroutines.* import kotlin.test.* +import kotlin.use abstract class HttpServerCommonTestSuite( hostFactory: ApplicationEngineFactory diff --git a/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/ContentTestSuite.kt b/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/ContentTestSuite.kt index 7be8a940ab6..960ea9817a2 100644 --- a/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/ContentTestSuite.kt +++ b/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/ContentTestSuite.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.server.testing.suites @@ -18,7 +18,6 @@ import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.server.test.base.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* import kotlinx.coroutines.* import org.junit.jupiter.api.extension.* import java.io.* diff --git a/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/HttpServerJvmTestSuite.kt b/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/HttpServerJvmTestSuite.kt index d7e1469c61d..5c78a07a6d8 100644 --- a/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/HttpServerJvmTestSuite.kt +++ b/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/HttpServerJvmTestSuite.kt @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.server.testing.suites @@ -25,6 +25,7 @@ import kotlin.coroutines.* import kotlin.test.* import kotlin.text.toByteArray import kotlin.time.Duration.Companion.seconds +import kotlin.use abstract class HttpServerJvmTestSuite( hostFactory: ApplicationEngineFactory diff --git a/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/SustainabilityTestSuite.kt b/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/SustainabilityTestSuite.kt index f50ebb82ed9..a361683dca1 100644 --- a/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/SustainabilityTestSuite.kt +++ b/ktor-server/ktor-server-test-suites/jvm/src/io/ktor/server/testing/suites/SustainabilityTestSuite.kt @@ -1,6 +1,6 @@ /* -* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. -*/ + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ package io.ktor.server.testing.suites @@ -37,6 +37,7 @@ import java.util.* import java.util.concurrent.* import java.util.concurrent.atomic.* import kotlin.test.* +import kotlin.use @ExtendWith(RetrySupport::class) abstract class SustainabilityTestSuite( diff --git a/ktor-server/ktor-server-test-suites/jvmAndPosix/src/io/ktor/server/testing/suites/WebSocketEngineSuite.kt b/ktor-server/ktor-server-test-suites/jvmAndPosix/src/io/ktor/server/testing/suites/WebSocketEngineSuite.kt index 920a0f382a7..f4e86cb77b3 100644 --- a/ktor-server/ktor-server-test-suites/jvmAndPosix/src/io/ktor/server/testing/suites/WebSocketEngineSuite.kt +++ b/ktor-server/ktor-server-test-suites/jvmAndPosix/src/io/ktor/server/testing/suites/WebSocketEngineSuite.kt @@ -26,6 +26,7 @@ import kotlin.test.* import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds +import kotlin.use @OptIn(InternalAPI::class) abstract class WebSocketEngineSuite( diff --git a/ktor-utils/jvm/src/io/ktor/util/cio/FileChannels.kt b/ktor-utils/jvm/src/io/ktor/util/cio/FileChannels.kt index e673f52f114..c6b72d89991 100644 --- a/ktor-utils/jvm/src/io/ktor/util/cio/FileChannels.kt +++ b/ktor-utils/jvm/src/io/ktor/util/cio/FileChannels.kt @@ -1,12 +1,10 @@ /* - * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + * Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ package io.ktor.util.cio -import io.ktor.util.* import io.ktor.utils.io.* -import io.ktor.utils.io.core.* import io.ktor.utils.io.jvm.nio.* import kotlinx.coroutines.* import java.io.*