Compression

Ktor provides the capability to compress outgoing content by using the Compression feature. You can use different compression algorithms, including gzip and deflate, specify the required conditions for compressing data (such as a content type or response size), or even compress data based on specific request parameters.

Install Compression

To install the Compression feature, pass it to the install function in the application initialization code. This can be the main function …

  1. import io.ktor.features.*
  2. // ...
  3. fun Application.main() {
  4. install(Compression)
  5. // ...
  6. }

… or a specified module:

  1. import io.ktor.features.*
  2. // ...
  3. fun Application.module() {
  4. install(Compression)
  5. // ...
  6. }

This enables the gzip, deflate, and identity encoders on a server. In the next chapter, we’ll see how to enable only specific encoders and configure conditions for compressing data.

Configure Compression Settings

You can configure compression in multiple ways: enable only specific encoders, specify their priorities, compress only specific content types, and so on.

Add Specific Encoders

To enable only specific encoders, call the corresponding extension functions, for example:

  1. install(Compression) {
  2. gzip()
  3. deflate()
  4. }

You can specify the priority for each compression algorithm by establishing the priority property:

  1. install(Compression) {
  2. gzip {
  3. priority = 0.9
  4. }
  5. deflate {
  6. priority = 1.0
  7. }
  8. }

In the example above, deflate has a higher priority value and takes precedence over gzip. Note that the server first looks at the quality values within the Accept-Encoding header and then takes into account the specified priorities.

Configure Content Type

By default, Ktor doesn’t compress specific content types, such as audio, video, image, and text/event-stream. You can choose the content types to compress by calling matchContentType or exclude the desired media types from compression by using excludeContentType. The code snippet below shows how to compress all text subtypes and JavaScript code using gzip:

  1. install(Compression) {
  2. gzip {
  3. matchContentType(
  4. ContentType.Text.Any,
  5. ContentType.Application.JavaScript
  6. )
  7. }
  8. }

Configure Response Size

The Compression feature allows you to disable compression for responses whose size doesn’t exceed the specified value. To do this, pass the desired value (in bytes) to the minimumSize function:

  1. install(Compression) {
  2. deflate {
  3. minimumSize(1024)
  4. }
  5. }

Specify Custom Conditions

If necessary, you can provide a custom condition using the condition function and compress data depending on the specific request parameters. The code snippet below shows how to compress requests for the specified URI:

  1. install(Compression) {
  2. gzip {
  3. condition {
  4. request.uri == "/orders"
  5. }
  6. }
  7. }

HTTPS Security

HTTPS with the enabled compression is vulnerable to the BREACH attack. You can use various ways to mitigate this attack. For example, you can disable compression whenever the referrer header indicates a cross-site request. In Ktor, this can be done by checking the referrer header value:

  1. install(Compression) {
  2. gzip {
  3. condition {
  4. request.headers[HttpHeaders.Referrer]?.startsWith("https://my.domain/") == true
  5. }
  6. }
  7. }

Implement Custom Encoder

If necessary, you can provide your own encoder by implementing the CompressionEncoder interface. See GzipEncoder as an example implementation.