Ktor HTTP client content encoding plugin for tvOS ARM64 that provides compression and decompression support
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Ktor Client Encoding is an HTTP client plugin that provides automatic content compression and decompression capabilities for the Ktor framework. It handles Accept-Encoding headers and transparently processes compressed response content, supporting gzip, deflate, and identity encodings.
implementation("io.ktor:ktor-client-encoding-tvosarm64:2.3.13")import io.ktor.client.plugins.compression.ContentEncoding
import io.ktor.client.plugins.compression.ContentEncoder
import io.ktor.client.plugins.compression.UnsupportedContentEncodingException
import io.ktor.util.Encoder
import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.CoroutineScopeimport io.ktor.client.*
import io.ktor.client.plugins.compression.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
// Create client with content encoding
val client = HttpClient {
ContentEncoding {
gzip()
deflate()
identity()
}
}
// Make request - compression is handled automatically
val response: HttpResponse = client.get("https://api.example.com/data")
val content: String = response.body<String>() // Automatically decompressedKtor Client Encoding integrates with the Ktor HTTP client pipeline through two main interception points:
The plugin uses platform-specific encoder implementations:
Install the ContentEncoding plugin with customizable encoder configuration and quality values.
fun HttpClientConfig<*>.ContentEncoding(
block: ContentEncoding.Config.() -> Unit = {
gzip()
deflate()
identity()
}
): UnitConfigure individual content encoders with optional quality values for Accept-Encoding header negotiation.
@KtorDsl
class ContentEncoding.Config {
/**
* Installs the gzip encoder.
* @param quality a priority value to use in the Accept-Encoding header.
*/
fun gzip(quality: Float? = null): Unit
/**
* Installs the deflate encoder.
* @param quality a priority value to use in the Accept-Encoding header.
*/
fun deflate(quality: Float? = null): Unit
/**
* Installs the identity encoder.
* @param quality a priority value to use in the Accept-Encoding header.
*/
fun identity(quality: Float? = null): Unit
/**
* Installs a custom encoder.
* @param encoder a custom encoder to use.
* @param quality a priority value to use in the Accept-Encoding header.
*/
fun customEncoder(encoder: ContentEncoder, quality: Float? = null): Unit
}Parameters:
quality: Optional priority value (0.0..1.0) for Accept-Encoding header. Higher values indicate preferred encodings.Usage Examples:
// Default configuration (all encoders, no quality values)
ContentEncoding()
// Custom quality preferences
ContentEncoding {
gzip(quality = 0.9f)
deflate(quality = 0.8f)
identity(quality = 0.1f)
}
// Selective encoders
ContentEncoding {
gzip()
// Only gzip encoding enabled
}Implement custom content encoders for specialized compression algorithms.
interface ContentEncoder : Encoder {
/**
* Encoder identifier to use in http headers.
*/
val name: String
}
interface Encoder {
/**
* Launch coroutine to encode source bytes.
*/
fun CoroutineScope.encode(source: ByteReadChannel): ByteReadChannel
/**
* Launch coroutine to decode source bytes.
*/
fun CoroutineScope.decode(source: ByteReadChannel): ByteReadChannel
}Properties:
name: Encoder identifier used in HTTP headers (e.g., "gzip", "br", "custom")Integration:
object CustomEncoder : ContentEncoder {
override val name = "custom"
// Implement Encoder interface methods
}
// Register custom encoder
ContentEncoding {
customEncoder(CustomEncoder, quality = 0.7f)
}Core plugin class implementing the HttpClientPlugin interface.
class ContentEncoding private constructor(
private val encoders: Map<String, ContentEncoder>,
private val qualityValues: Map<String, Float>
) {
@KtorDsl
class Config {
fun gzip(quality: Float? = null): Unit
fun deflate(quality: Float? = null): Unit
fun identity(quality: Float? = null): Unit
fun customEncoder(encoder: ContentEncoder, quality: Float? = null): Unit
}
companion object : HttpClientPlugin<Config, ContentEncoding> {
override val key: AttributeKey<ContentEncoding>
override fun prepare(block: Config.() -> Unit): ContentEncoding
override fun install(plugin: ContentEncoding, scope: HttpClient): Unit
}
}Exception thrown when encountering unsupported content encodings in response headers.
class UnsupportedContentEncodingException(encoding: String) :
IllegalStateException("Content-Encoding: \$encoding unsupported.")Parameters:
encoding: The unsupported encoding name from Content-Encoding headerWhen Thrown:
Example:
try {
val response = client.get("https://api.example.com/compressed")
val content = response.body<String>()
} catch (e: UnsupportedContentEncodingException) {
println("Unsupported encoding: ${e.message}")
// Handle unsupported encoding
}The plugin provides built-in support for standard content encodings through internal encoder implementations:
// Internal encoder objects (not directly accessible)
internal object GZipEncoder : ContentEncoder, Encoder by GZip { // JVM only
override val name: String = "gzip"
}
internal object DeflateEncoder : ContentEncoder, Encoder by Deflate { // JVM only
override val name: String = "deflate"
}
internal object IdentityEncoder : ContentEncoder, Encoder by Identity {
override val name: String = "identity"
}Encoder Types:
Platform-Specific Behavior:
The plugin automatically manages HTTP headers for content negotiation:
Request Headers:
Accept-Encoding with configured encoders and quality valuesAccept-Encoding: gzip;q=0.9,deflate;q=0.8,identity;q=0.1Response Processing:
Content-Encoding header from server responsesConfiguration Errors:
Runtime Errors:
Platform Limitations:
The ContentEncoding plugin is thread-safe and can be used concurrently across multiple requests. Internal state is immutable after configuration, and platform-specific encoders handle concurrent access appropriately.