From f47c76d1fcbbc7cf8bdb16e912b24b08d6cf57ab Mon Sep 17 00:00:00 2001 From: ryoii Date: Sun, 8 Oct 2023 19:00:53 +0800 Subject: [PATCH] Http `post` session injection --- .../api/http/adapter/http/plugin/Authorization.kt | 12 +++++++++++- .../mirai/api/http/adapter/http/plugin/hooks.kt | 13 +++++++++++++ .../mamoe/mirai/api/http/adapter/http/router/dsl.kt | 7 +------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/Authorization.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/Authorization.kt index 568bfcc3..3416d671 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/Authorization.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/Authorization.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020 Mamoe Technologies and contributors. + * Copyright 2023 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -12,6 +12,8 @@ package net.mamoe.mirai.api.http.adapter.http.plugin import io.ktor.server.application.* import io.ktor.server.request.* import io.ktor.util.* +import net.mamoe.mirai.api.http.adapter.common.IllegalSessionException +import net.mamoe.mirai.api.http.adapter.internal.dto.AuthedDTO import net.mamoe.mirai.api.http.context.MahContextHolder import net.mamoe.mirai.api.http.context.session.Session @@ -30,6 +32,14 @@ val Authorization = createApplicationPlugin("Authorization") { } } } + + on(ReceiveBodyTransformed) { call, body -> + if (body is AuthedDTO) { + body.session = call.session ?: MahContextHolder[body.sessionKey] + ?: throw IllegalSessionException + } + body + } } val ApplicationCall.session: Session? diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/hooks.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/hooks.kt index ef9537ff..4deaded7 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/hooks.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/hooks.kt @@ -10,6 +10,7 @@ package net.mamoe.mirai.api.http.adapter.http.plugin import io.ktor.server.application.* +import io.ktor.server.request.* internal object Monitor : Hook Unit> { override fun install(pipeline: ApplicationCallPipeline, handler: suspend (ApplicationCall) -> Unit) { @@ -18,3 +19,15 @@ internal object Monitor : Hook Unit> { } } } + +internal object ReceiveBodyTransformed : Hook Any> { + override fun install( + pipeline: ApplicationCallPipeline, + handler: suspend (call: ApplicationCall, state: Any) -> Any + ) { + pipeline.receivePipeline.intercept(ApplicationReceivePipeline.After) { + val body = handler(call, it) + proceedWith(body) + } + } +} \ No newline at end of file diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt index d3b60a2a..c29018f3 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt @@ -95,12 +95,7 @@ internal inline fun Route.httpBind(path: String, crossinline body: Strategy Route.httpAuthedPost( path: String, crossinline body: Strategy -) = routeWithHandle(path, HttpMethod.Post) { - val dto = context.receive() - - getAuthedSession(dto.sessionKey).also { dto.session = it } - this.body(dto) -} +) = routeWithHandle(path, HttpMethod.Post) { body(context.receive()) } /** * Get,用于获取bot的属性