Streaming Movies and Other Content

This feature adds support for handling Partial Content requests:requests with the Range header. It intercepts the generatedresponse adding the Accept-Ranges and the Content-Range header and slicingthe served content when required.

Partial Content is well-suited for streaming content or resume partial downloads withdownload managers, or in unreliable networks.

It is especially useful for the Static Content Feature.

This feature is defined in the class io.ktor.features.PartialContent and no additional artifacts are required.

Usage

To install the PartialContent feature with the default configuration:

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

Configuration

  1. install(PartialContent) {
  2. // Maximum number of ranges that will be accepted from a HTTP request.
  3. // If the HTTP request specifies more ranges, they will all be merged into a single range.
  4. maxRangeCount = 10
  5. }

Detailed description

This feature only works with HEAD and GET requests.And it will return a 405 Method Not Allowed if the client tries to use the Rangeheader with other methods.

It disables compression when serving ranges.

It is only enabled for responses that define the Content-Length. And it:

  • Removes the Content-Length header
  • Adds the Accept-Ranges header
  • Adds the Content-Range header with the requested Ranges
  • Serves only the requested slice of the content

It should work with any served content of the type OutgoingContent.ReadChannelContentas long as its length is defined, like for example the LocalFileContent.

This HTTP mechanism for Partial Content is described in the RFC-7233.