Ktor Documentation

repository·main·Indexed 19 days ago

https://github.com/ktorio/ktor-documentation

Official documentation for the Ktor framework, including Markdown/XML topics and a collection of runnable code snippets. The documentation provides guides and examples for various Ktor plugins, specifically focusing on authentication implementations such as Basic, Bearer, Digest, Form, JWT (HS256 and RS256), LDAP, and Google OAuth.

Tokens
178.6K
Snippets
864
Records
1.1K
Agent score
68%

What's inside Ktor Documentation

  1. Overview of Ktor Plugins

    main

    Ktor does not activate any plugins by default. You must explicitly install the functionality you need.

    • Built-in Plugins: Ktor provides many plugins out of the box (e.g., CORS, Compression, Sessions, CachingHeaders).
    • Custom Plugins: You can also implement your own custom plugins to extend the pipeline functionality.
  2. Trace requests in Ktor Server using the CallId plugin

    main

    The CallId plugin allows you to trace client requests end-to-end by using unique request IDs. The typical workflow involves:

    1. Obtaining a Call ID: Either by retrieving it from an incoming header (e.g., provided by Nginx or Heroku) or by generating a new one on the server.
    2. Verification: Ktor verifies the ID against a predefined dictionary (defaulting to lowercase alphanumeric and specific symbols) to ensure validity.
    3. Propagation: You can send the Call ID back to the client in a response header and include it in the MDC (Mapped Diagnostic Context) for logging purposes.

    Required dependency: io.ktor:ktor-server-call-id

  3. Enable HTTP/2 in Ktor

    main

    HTTP/2 support in Ktor is provided via the Jetty or Netty engines. Once the host is correctly configured with an SSL certificate and a suitable ALPN (Application-Layer Protocol Negotiation) implementation, HTTP/2 support is activated automatically.

    Key Requirements:

    1. SSL Certificate: Required because all browsers mandate encrypted connections for HTTP/2.
    2. ALPN Implementation: Required for protocol negotiation. The method for providing ALPN depends on the engine used (Jetty or Netty).
  4. Understand the Timeout sample endpoints

    main

    The Timeout sample demonstrates how to protect a client from long-running or hanging requests using the HttpTimeout plugin. It provides two specific endpoints:

    • /timeout: Emulates a long-running process that may hang.
    • /proxy: Acts as a proxy to /timeout. It uses the HttpTimeout plugin to automatically abort the request if the underlying process hangs, protecting the user from indefinite waiting.
  5. Authentication and authorization in Ktor Server

    main

    Ktor uses the Authentication plugin to handle user identity and access control. This plugin allows you to log in users, grant access to specific resources, and securely transmit information. It can be used in conjunction with Sessions to persist user information across multiple HTTP requests.

    Required dependency: io.ktor:ktor-server-auth

  6. What is the DoubleReceive plugin and when to use it

    main

    The DoubleReceive plugin allows you to receive a request body multiple times without triggering a RequestAlreadyConsumedException.

    By default, Ktor request streams can only be read once. This plugin is useful when a plugin (like CallLogging) needs to consume the request body for logging or processing, but you still need to access that same body later inside your route handler.

  7. What is the PartialContent plugin?

    main

    The PartialContent plugin adds support for handling HTTP range requests. This allows the server to send only a specific portion of an HTTP message back to a client, which is essential for streaming content or resuming partial downloads.

    Limitations:

    • It only works for HEAD and GET requests. Using the Range header with other methods will return a 405 Method Not Allowed error.
    • It only works for responses that have the Content-Length header defined.
    • It disables Compression when serving ranges.