+
+Required dependencies: io.ktor:ktor-server-core
+
+
+
+Supported engines: Netty, CIO
+
+
+
+
+By default, Ktor continues processing a request even if the client disconnects. The
+[`%plugin_name%`](https://api.ktor.io/ktor-http/io.ktor.http/-http-request-lifecycle.html)
+plugin allows you to cancel request processing as soon as the client disconnects.
+
+This is useful for long-running or resource-intensive requests that should stop executing when the client is no longer
+waiting for a response.
+
+## Install and configure %plugin_name% {id="install_plugin"}
+
+To enable the `HttpRequestLifecycle` plugin, install it in your application module using the `install` function and
+set the `cancelCallOnClose` property:
+
+```kotlin
+```
+{src="snippets/server-http-request-lifecycle/src/main/kotlin/com/example/Application.kt" include-lines="9-10,18-22,37"}
+
+When the `cancelCallOnClose` property is enabled, the `%plugin_name%` plugin installs a cancellation handler per
+request. When a client disconnects, only the coroutine handling that specific route is canceled.
+
+Cancellation propagates through structured concurrency, so any child coroutines started from the request coroutine
+(for example, using `launch` or `async`) are also canceled. A `CancellationException` is thrown at the next
+suspension point.
+
+## Handling cancellation {id="handle_cancellation"}
+
+You can catch `CancellationException` to perform cleanup operations, such as releasing resources or stopping background
+work:
+
+```kotlin
+```
+{src="snippets/server-http-request-lifecycle/src/main/kotlin/com/example/Application.kt" include-lines="19-37"}
+
+> Coroutine cancellation is cooperative. Blocking or CPU-bound code is not interrupted automatically.
+> If request handling performs long-running work, call
+> `call.coroutineContext.ensureActive()` to react to cancellation.
+>
+> To learn more, see
+> [Coroutine cancellation](https://kotlinlang.org/docs/cancellation-and-timeouts.html).
+{style="note"}
+
+> For the full example, see [%example_name%](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/%example_name%).
+
+## Limitations
+
+This plugin is only fully supported on `CIO` and `Netty` engines. Engines based on servlets (or other unsupported engines)
+cannot detect client disconnects reliably. Cancellation can only be detected when the server attempts to write a
+response.
\ No newline at end of file
diff --git a/topics/server-oauth.md b/topics/server-oauth.md
index 460d48cf4..d53190ed3 100644
--- a/topics/server-oauth.md
+++ b/topics/server-oauth.md
@@ -38,7 +38,7 @@ request the resource.
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="14,28-32,104,131-132"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="14,29-33,115,141-143"}
## OAuth authorization flow {id="flow"}
@@ -77,7 +77,7 @@ For example, to install an `oauth` provider with the name "auth-oauth-google" it
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="9-10,28-29,34-37,57-58,104"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="9-10,29-30,35-38,65-66,115"}
## Configure OAuth {id="configure-oauth"}
@@ -111,7 +111,7 @@ JSON serializer is required to deserialize received JSON data [after a request t
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="21-27"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="22-28"}
The client instance is passed to the `main` [module function](server-modules.md) to have the capability to create a separate
client instance in a server [test](server-testing.md).
@@ -119,7 +119,7 @@ client instance in a server [test](server-testing.md).
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="29,104"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="30,115"}
### Step 2: Configure the OAuth provider {id="configure-oauth-provider"}
@@ -128,13 +128,14 @@ The code snippet below shows how to create and configure the `oauth` provider wi
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="33-58"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="34-66"}
* The `urlProvider` specifies a [redirect route](#redirect-route) that will be invoked when authorization is completed.
> Make sure that this route is added to a list of [**Authorised redirect URIs**](#authorization-credentials).
* `providerLookup` allows you to specify OAuth settings for a required provider. These settings are represented by
the [OAuthServerSettings](https://api.ktor.io/ktor-server-auth/io.ktor.server.auth/-o-auth-server-settings/index.html)
class and allow Ktor to make automatic requests to the OAuth server.
+* The `fallback` property handles OAuth flow errors by responding with a redirect or custom response.
* The `client` property specifies the [HttpClient](#create-http-client) used by Ktor to make requests to the OAuth
server.
@@ -149,7 +150,7 @@ in [providerLookup](#configure-oauth-provider).
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="59-63,79,103"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="67-71,87,114"}
A user will see the authorization page with the level of permissions required for a Ktor application. These permissions
depend on `defaultScopes` specified in [providerLookup](#configure-oauth-provider).
@@ -167,7 +168,7 @@ returned by the OAuth server.
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="59-79,103"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="67-87,114"}
In this example, the following actions are performed after receiving a token:
@@ -186,14 +187,14 @@ Create a new function called `getPersonalGreeting` which will make the request a
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="106-113"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="117-124"}
Then, you can call the function within a `get` route to retrieve a user's information:
```kotlin
```
-{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="96-102"}
+{src="snippets/auth-oauth-google/src/main/kotlin/com/example/oauth/google/Application.kt" include-lines="104-110"}
For the complete runnable example,
see [auth-oauth-google](https://github.com/ktorio/ktor-documentation/tree/%ktor_version%/codeSnippets/snippets/auth-oauth-google).
diff --git a/topics/server-responses.md b/topics/server-responses.md
index df4cb2ad9..ca33f0d16 100644
--- a/topics/server-responses.md
+++ b/topics/server-responses.md
@@ -4,18 +4,22 @@