A Ktor server plugin that provides HTTP response compression and request decompression capabilities with support for gzip, deflate, and identity encoding algorithms.
—
Compression conditions determine when responses should be compressed based on content type, size, and custom predicates. Conditions can be applied globally to all encoders or specifically to individual encoders.
interface ConditionsHolderBuilder {
val conditions: MutableList<ApplicationCall.(OutgoingContent) -> Boolean>
}Both CompressionConfig and CompressionEncoderBuilder implement this interface, allowing conditions at both global and per-encoder levels.
fun ConditionsHolderBuilder.condition(predicate: ApplicationCall.(OutgoingContent) -> Boolean)Add custom logic to determine when compression should be applied:
install(Compression) {
// Global condition - applies to all encoders
condition { call, content ->
// Only compress responses for specific routes
call.request.uri.startsWith("/api/")
}
gzip {
// Encoder-specific condition
condition { call, content ->
// Don't compress responses for mobile clients
call.request.headers["User-Agent"]?.contains("Mobile") != true
}
}
}fun ConditionsHolderBuilder.minimumSize(minSize: Long)Only compress content that meets the minimum size threshold:
install(Compression) {
// Global minimum size
minimumSize(1024) // Only compress responses >= 1KB
gzip {
// Override for gzip - compress smaller JSON responses
minimumSize(200)
matchContentType(ContentType.Application.Json)
}
}fun ConditionsHolderBuilder.matchContentType(vararg mimeTypes: ContentType)Only compress responses with matching content types:
install(Compression) {
matchContentType(
ContentType.Text.Plain,
ContentType.Text.Html,
ContentType.Application.Json,
ContentType.Application.JavaScript
)
}fun ConditionsHolderBuilder.excludeContentType(vararg mimeTypes: ContentType)Exclude specific content types from compression:
install(Compression) {
// Exclude media files and streams
excludeContentType(
ContentType.Video.Any,
ContentType.Image.Any,
ContentType.Audio.Any,
ContentType.Text.EventStream,
ContentType.MultiPart.Any
)
}When no conditions are explicitly configured, the plugin applies sensible defaults:
// Default exclusions applied automatically
excludeContentType(
ContentType.Video.Any,
ContentType.Image.JPEG,
ContentType.Image.PNG,
ContentType.Audio.Any,
ContentType.MultiPart.Any,
ContentType.Text.EventStream
)
minimumSize(200) // DEFAULT_MINIMAL_COMPRESSION_SIZEConditions are evaluated in the following order:
CompressionConfig) - must all return trueCompressionEncoderBuilder) - must all return trueIf any condition returns false, compression is skipped for that request.
install(Compression) {
condition { call, content ->
val contentType = content.contentType ?:
call.response.headers[HttpHeaders.ContentType]?.let { ContentType.parse(it) }
contentType?.let {
it.match(ContentType.Application.Json) ||
it.match(ContentType.Text.Any)
} ?: false
}
}install(Compression) {
condition { call, _ ->
// Only compress for API versions that support it
val apiVersion = call.request.headers["API-Version"]
apiVersion != null && apiVersion >= "2.0"
}
condition { call, _ ->
// Skip compression for debug requests
call.request.queryParameters["debug"] != "true"
}
}install(Compression) {
condition { _, content ->
when (val length = content.contentLength) {
null -> true // Unknown length - allow compression
in 0..100 -> false // Too small
in 101..1000 -> content.contentType?.match(ContentType.Application.Json) == true
else -> true // Large content - always compress
}
}
}When both global and encoder-specific conditions are present:
install(Compression) {
// Global: must be JSON or text
matchContentType(ContentType.Application.Json, ContentType.Text.Any)
minimumSize(100)
gzip {
// Additional encoder requirement: minimum 500 bytes for gzip
minimumSize(500)
// Inherits global content type matching
}
deflate {
// Uses global conditions only (JSON/text, >= 100 bytes)
}
}In this example:
deflate compresses JSON/text >= 100 bytesgzip compresses JSON/text >= 500 bytes (more restrictive)Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-server-compression-jvm