Skip to content

Commit

Permalink
KTOR-6036 Deprecate old IO API (#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l authored Jul 31, 2023
1 parent cb94df2 commit 273fc7d
Show file tree
Hide file tree
Showing 145 changed files with 728 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class AndroidClientEngine(override val config: AndroidEngineConfig) : Htt
}

@OptIn(DelicateCoroutinesApi::class)
@Suppress("BlockingMethodInNonBlockingContext")
@Suppress("BlockingMethodInNonBlockingContext", "DEPRECATION")
internal suspend fun OutgoingContent.writeTo(
stream: OutputStream,
callContext: CoroutineContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.apache.http.client.methods.*
import org.apache.http.client.utils.*
import org.apache.http.entity.*
import org.apache.http.nio.*
import org.apache.http.nio.ContentEncoder
import org.apache.http.nio.protocol.*
import org.apache.http.protocol.*
import java.nio.*
Expand All @@ -44,6 +43,7 @@ internal class ApacheRequestProducer(
private val producerJob = Job()
override val coroutineContext: CoroutineContext = callContext + producerJob

@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
private val channel: ByteReadChannel = when (val body = requestData.body) {
is OutgoingContent.ByteArrayContent -> ByteReadChannel(body.bytes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ internal class ApacheResponseConsumer(
override val coroutineContext: CoroutineContext = parentContext + consumerJob

private val waiting = atomic(false)

@Suppress("DEPRECATION")
private val channel = ByteChannel().also {
it.attachJob(consumerJob)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package io.ktor.client.engine.apache5

import io.ktor.client.call.*
import io.ktor.client.engine.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.HttpHeaders
Expand All @@ -16,17 +15,10 @@ import io.ktor.utils.io.*
import kotlinx.atomicfu.*
import kotlinx.coroutines.*
import org.apache.hc.client5.http.async.methods.*
import org.apache.hc.client5.http.classic.methods.*
import org.apache.hc.client5.http.config.*
import org.apache.hc.client5.http.utils.*
import org.apache.hc.core5.http.*
import org.apache.hc.core5.http.HttpRequest
import org.apache.hc.core5.http.io.entity.*
import org.apache.hc.core5.http.message.*
import org.apache.hc.core5.http.nio.*
import org.apache.hc.core5.http.nio.entity.*
import org.apache.hc.core5.http.nio.support.*
import org.apache.hc.core5.http.protocol.*
import java.nio.*
import java.util.concurrent.*
import kotlin.coroutines.*
Expand Down Expand Up @@ -98,6 +90,7 @@ internal class ApacheRequestEntityProducer(
private val producerJob = Job()
override val coroutineContext: CoroutineContext = callContext + producerJob

@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
private val channel: ByteReadChannel = when (val body = requestData.body) {
is OutgoingContent.ByteArrayContent -> ByteReadChannel(body.bytes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ internal class ApacheResponseConsumer(
private val consumerJob = Job(parentContext[Job])
override val coroutineContext: CoroutineContext = parentContext + consumerJob

@Suppress("DEPRECATION")
private val channel = ByteChannel().also {
it.attachJob(consumerJob)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ internal suspend fun writeHeaders(
}
}

@Suppress("TYPEALIAS_EXPANSION_DEPRECATION")
internal suspend fun writeBody(
request: HttpRequestData,
output: ByteWriteChannel,
Expand Down Expand Up @@ -149,6 +150,7 @@ internal suspend fun writeBody(
}
}

@Suppress("DEPRECATION")
internal suspend fun readResponse(
requestTime: GMTDate,
request: HttpRequestData,
Expand Down Expand Up @@ -255,6 +257,7 @@ internal fun HttpStatusCode.isInformational(): Boolean = (value / 100) == 1
/**
* Wrap channel so that [ByteWriteChannel.close] of the resulting channel doesn't lead to closing of the base channel.
*/
@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
internal fun ByteWriteChannel.withoutClosePropagation(
coroutineContext: CoroutineContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public fun CoroutineScope.mapEngineExceptions(input: ByteReadChannel, request: H

val replacementChannel = ByteChannelWithMappedExceptions(request)

@Suppress("DEPRECATION")
writer(channel = replacementChannel) {
try {
input.copyAndClose(replacementChannel)
Expand All @@ -56,6 +57,7 @@ public fun CoroutineScope.mapEngineExceptions(output: ByteWriteChannel, request:

val replacementChannel = ByteChannelWithMappedExceptions(request)

@Suppress("DEPRECATION")
writer(channel = replacementChannel) {
try {
replacementChannel.copyAndClose(output)
Expand All @@ -71,4 +73,5 @@ public fun CoroutineScope.mapEngineExceptions(output: ByteWriteChannel, request:
* Creates [ByteChannel] that maps close exceptions (close the channel with [SocketTimeoutException] if asked to
* close it with [SocketTimeoutException]).
*/
@Suppress("DEPRECATION")
internal expect fun ByteChannelWithMappedExceptions(request: HttpRequestData): ByteChannel
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public fun HttpClient.defaultTransformers() {
}

ByteReadPacket::class,
@Suppress("DEPRECATION")
Input::class -> {
proceedWith(HttpResponseContainer(info, body.readRemaining()))
}
Expand All @@ -91,7 +92,7 @@ public fun HttpClient.defaultTransformers() {

ByteReadChannel::class -> {
// the response job could be already completed, so the job holder
// could be cancelled immediately, but it doesn't matter
// could be canceled immediately, but it doesn't matter
// since the copying job is running under the client job
val responseJobHolder = Job(response.coroutineContext[Job])
val channel: ByteReadChannel = writer(response.coroutineContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public class HttpPlainText internal constructor(
return TextContent(content, contentType.withCharset(charset))
}

@Suppress("DEPRECATION")
internal fun read(call: HttpClientCall, body: Input): String {
val actualCharset = call.response.charset() ?: responseCharsetFallback
LOGGER.trace("Reading response body for ${call.request.url} as String with charset $actualCharset")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.utils.io.*

@Suppress("KDocMissingDocumentation")
@Suppress("KDocMissingDocumentation", "DEPRECATION")
@InternalAPI
public abstract class ClientUpgradeContent : OutgoingContent.NoContent() {
private val content: ByteChannel by lazy { ByteChannel() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public class MultiPartFormDataContent(
}
}

override val contentLength: Long?
override var contentLength: Long? = null
private set

init {
var rawLength: Long? = 0
Expand Down Expand Up @@ -146,6 +147,7 @@ private fun generateBoundary(): String = buildString {
}.take(70)

private sealed class PreparedPart(val headers: ByteArray, val size: Long?) {
@Suppress("DEPRECATION")
class InputPart(headers: ByteArray, val provider: () -> Input, size: Long?) : PreparedPart(headers, size)
class ChannelPart(
headers: ByteArray,
Expand All @@ -154,6 +156,7 @@ private sealed class PreparedPart(val headers: ByteArray, val size: Long?) {
) : PreparedPart(headers, size)
}

@Suppress("DEPRECATION")
private suspend fun Input.copyTo(channel: ByteWriteChannel) {
if (this is ByteReadPacket) {
channel.writePacket(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public data class FormPart<T : Any>(val key: String, val value: T, val headers:
*
* Example: [Upload a file](https://ktor.io/docs/request.html#upload_file).
*/
@Suppress("DEPRECATION")
public fun formData(vararg values: FormPart<*>): List<PartData> {
val result = mutableListOf<PartData>()

Expand Down Expand Up @@ -128,6 +129,7 @@ public class FormBuilder internal constructor() {
/**
* Appends a pair [key]:[InputProvider(block)] with optional [headers].
*/
@Suppress("DEPRECATION")
public fun appendInput(key: String, headers: Headers = Headers.Empty, size: Long? = null, block: () -> Input) {
parts += FormPart(key, InputProvider(size, block), headers)
}
Expand Down Expand Up @@ -197,6 +199,7 @@ public inline fun FormBuilder.append(
* @property size estimate for data produced by the block or `null` if no size estimation known
* @param block: content generator
*/
@Suppress("DEPRECATION")
public class InputProvider(public val size: Long? = null, public val block: () -> Input)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal fun HttpResponse.complete() {
public suspend fun HttpResponse.bodyAsText(fallbackCharset: Charset = Charsets.UTF_8): String {
val originCharset = charset() ?: fallbackCharset
val decoder = originCharset.newDecoder()
val input = body<Input>()
val input = body<ByteReadPacket>()

return decoder.decode(input)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public actual class SocketTimeoutException actual constructor(
* close it with [SocketTimeoutException]).
*/
@OptIn(InternalAPI::class)
@Suppress("DEPRECATION")
internal actual fun ByteChannelWithMappedExceptions(request: HttpRequestData): ByteChannel = ByteChannel { cause ->
when (cause?.rootCause) {
is java.net.SocketTimeoutException -> SocketTimeoutException(request, cause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private class FileCacheStorage(
}
}

@Suppress("DEPRECATION")
private suspend fun writeCache(channel: ByteChannel, cache: CachedResponseData) {
channel.writeStringUtf8(cache.url.toString() + "\n")
channel.writeInt(cache.statusCode.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal fun HttpRequestData.convertToHttpRequest(callContext: CoroutineContext)
return builder.build()
}

@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
internal fun OutgoingContent.convertToHttpRequestBody(
callContext: CoroutineContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal class JavaHttpResponseBodyHandler(
return JavaHttpResponseBodySubscriber(coroutineContext, responseInfo, requestTime)
}

@Suppress("DEPRECATION")
private class JavaHttpResponseBodySubscriber(
callContext: CoroutineContext,
response: HttpResponse.ResponseInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private fun HttpRequestData.prepareHeadersFrame(): HeadersFrame {
return HeadersFrame(meta, null, body is OutgoingContent.NoContent)
}

@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
private fun sendRequestBody(request: JettyHttp2Request, content: OutgoingContent, callContext: CoroutineContext) {
when (content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private fun HttpRequestData.prepareHeadersFrame(): HeadersFrame {
return HeadersFrame(meta, null, body is OutgoingContent.NoContent)
}

@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
private fun sendRequestBody(request: JettyHttp2Request, content: OutgoingContent, callContext: CoroutineContext) {
when (content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public interface JsonSerializer {
/**
* Read content from response using information specified in [type].
*/
@Suppress("DEPRECATION")
public fun read(type: TypeInfo, body: Input): Any
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal suspend fun OutgoingContent.observe(log: ByteWriteChannel): OutgoingCon
}
}

@Suppress("DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
private fun OutgoingContent.WriteChannelContent.toReadChannel(): ByteReadChannel =
GlobalScope.writer(Dispatchers.Default) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fun List<PartData>.makeString(): String = buildString {
}
}

@Suppress("DEPRECATION")
private fun filenameContentTypeAndContentString(provider: () -> Input, headers: Headers): String {
val dispositionHeader: String = headers.getAll(HttpHeaders.ContentDisposition)!!.joinToString(";")
val disposition: ContentDisposition = ContentDisposition.parse(dispositionHeader)
Expand Down
4 changes: 3 additions & 1 deletion ktor-http/common/src/io/ktor/http/content/Multipart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public sealed class PartData(public val dispose: () -> Unit, public val headers:
* Represents a file item
* @property provider of content bytes
*/
@Suppress("DEPRECATION")
public class FileItem(
public val provider: () -> Input,
dispose: () -> Unit,
Expand All @@ -41,6 +42,7 @@ public sealed class PartData(public val dispose: () -> Unit, public val headers:
* Represents a binary item
* @property provider of content bytes
*/
@Suppress("DEPRECATION")
public class BinaryItem(
public val provider: () -> Input,
dispose: () -> Unit,
Expand Down Expand Up @@ -103,7 +105,7 @@ public sealed class PartData(public val dispose: () -> Unit, public val headers:
*/
public interface MultiPartData {
/**
* Reads next part data or `null` if end of multipart stream encountered
* Reads next part data or `null` if the end of multipart stream encountered
*/
public suspend fun readPart(): PartData?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import io.ktor.utils.io.core.*
import io.ktor.utils.io.pool.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.native.concurrent.*

private const val MAX_CHUNK_SIZE_LENGTH = 128
private const val CHUNK_BUFFER_POOL_SIZE = 2048
Expand All @@ -25,11 +24,13 @@ private val ChunkSizeBufferPool: ObjectPool<StringBuilder> =
/**
* Decoder job type
*/
@Suppress("DEPRECATION")
public typealias DecoderJob = WriterJob

/**
* Start a chunked stream decoder coroutine
*/
@Suppress("TYPEALIAS_EXPANSION_DEPRECATION")
@Deprecated(
"Specify content length if known or pass -1L",
ReplaceWith("decodeChunked(input, -1L)")
Expand All @@ -40,7 +41,7 @@ public fun CoroutineScope.decodeChunked(input: ByteReadChannel): DecoderJob =
/**
* Start a chunked stream decoder coroutine
*/
@Suppress("UNUSED_PARAMETER")
@Suppress("UNUSED_PARAMETER", "TYPEALIAS_EXPANSION_DEPRECATION")
public fun CoroutineScope.decodeChunked(input: ByteReadChannel, contentLength: Long): DecoderJob =
writer(coroutineContext) {
decodeChunked(input, channel)
Expand Down Expand Up @@ -110,11 +111,13 @@ public suspend fun decodeChunked(input: ByteReadChannel, out: ByteWriteChannel,
/**
* Encoder job type
*/
@Suppress("DEPRECATION")
public typealias EncoderJob = ReaderJob

/**
* Start chunked stream encoding coroutine
*/
@Suppress("TYPEALIAS_EXPANSION_DEPRECATION")
@OptIn(DelicateCoroutinesApi::class)
public suspend fun encodeChunked(
output: ByteWriteChannel,
Expand Down Expand Up @@ -145,6 +148,7 @@ public suspend fun encodeChunked(output: ByteWriteChannel, input: ByteReadChanne
}
}

@Suppress("DEPRECATION")
private fun ByteReadChannel.rethrowCloseCause() {
val cause = when (this) {
is ByteChannel -> closedCause
Expand All @@ -157,6 +161,7 @@ private const val CrLfShort: Short = 0x0d0a
private val CrLf = "\r\n".toByteArray()
private val LastChunkBytes = "0\r\n\r\n".toByteArray()

@Suppress("DEPRECATION")
private suspend fun ByteWriteChannel.writeChunk(memory: Memory, startIndex: Int, endIndex: Int): Int {
val size = endIndex - startIndex
writeIntHex(size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public class CIOMultipartDataBase(
}
}

@Suppress("DEPRECATION")
private class MultipartInput(
private val head: ByteBuffer,
private val tail: ByteReadChannel
Expand Down
4 changes: 4 additions & 0 deletions ktor-io/common/src/io/ktor/utils/io/ByteChannelCtor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ import kotlinx.coroutines.*
* cannot be invoked concurrently with themselves. Exceptions are [close] and [flush] which can be invoked
* concurrently with any other operations and between themselves at any time.
*/
@Deprecated(IO_DEPRECATION_MESSAGE)
public interface ByteChannel : ByteReadChannel, ByteWriteChannel {
@Deprecated(IO_DEPRECATION_MESSAGE)
public fun attachJob(job: Job)
}

/**
* Creates buffered channel for asynchronous reading and writing of sequences of bytes.
*/
@Suppress("DEPRECATION")
@Deprecated(IO_DEPRECATION_MESSAGE)
public expect fun ByteChannel(autoFlush: Boolean = false): ByteChannel

/**
Expand Down
Loading

0 comments on commit 273fc7d

Please sign in to comment.