-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
302 additions
and
174 deletions.
There are no files selected for viewing
71 changes: 0 additions & 71 deletions
71
...-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/feature/auth/Authorization.kt
This file was deleted.
Oops, something went wrong.
94 changes: 0 additions & 94 deletions
94
...n/kotlin/net/mamoe/mirai/api/http/adapter/http/feature/handler/HttpRouterAccessHandler.kt
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/Authorization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2020 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. | ||
* | ||
* https://github.com/mamoe/mirai/blob/master/LICENSE | ||
*/ | ||
|
||
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.context.MahContextHolder | ||
import net.mamoe.mirai.api.http.context.session.Session | ||
|
||
private val sessionAttr: AttributeKey<Session> = AttributeKey("session") | ||
val Authorization = createApplicationPlugin("Authorization") { | ||
|
||
onCall { call -> | ||
if (MahContextHolder.singleMode) { | ||
return@onCall | ||
} | ||
|
||
val sessionKey = call.sessionKeyFromHeader() ?: call.sessionKeyFromAuthorization() | ||
if (sessionKey != null) { | ||
MahContextHolder[sessionKey]?.let { | ||
call.attributes.put(sessionAttr, it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
val ApplicationCall.session: Session? | ||
get() { | ||
return this.attributes.getOrNull(sessionAttr) | ||
} | ||
|
||
private fun ApplicationCall.sessionKeyFromHeader(): String? { | ||
return request.header("sessionKey") | ||
} | ||
|
||
private fun ApplicationCall.sessionKeyFromAuthorization(): String? { | ||
return request.header("Authorization")?.run { | ||
val (type, value) = split(' ', limit = 2) | ||
|
||
return if (type.equals("session", ignoreCase = true) || type.equals("sessionKey", ignoreCase = true)) { | ||
value | ||
} else { | ||
null | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/DoubleReceive.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.mamoe.mirai.api.http.adapter.http.plugin | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.util.* | ||
import io.ktor.utils.io.* | ||
|
||
private val doubleReceiveAttrKey: AttributeKey<Any> = AttributeKey("doubleReceiveBody") | ||
val DoubleReceive = createApplicationPlugin("DoubleReceive") { | ||
|
||
on(ReceiveBodyTransformed) { call, body -> | ||
if (body is ByteReadChannel) return@on body | ||
|
||
call.attributes.getOrNull(doubleReceiveAttrKey)?.let { | ||
return@on it | ||
} | ||
|
||
call.attributes.put(doubleReceiveAttrKey, body) | ||
return@on body | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...pi-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/HttpRouterMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2020 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. | ||
* | ||
* https://github.com/mamoe/mirai/blob/master/LICENSE | ||
*/ | ||
|
||
package net.mamoe.mirai.api.http.adapter.http.plugin | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.* | ||
import io.ktor.server.request.* | ||
import net.mamoe.mirai.utils.MiraiLogger | ||
|
||
val logger by lazy { MiraiLogger.Factory.create(HttpRouterMonitor::class, "MAH Access") } | ||
val HttpRouterMonitor = createApplicationPlugin("HttpRouterAccessMonitor") { | ||
on(Monitor) { call -> | ||
call.logAccess() | ||
} | ||
} | ||
|
||
private suspend fun ApplicationCall.logAccess() { | ||
logger.debug("requesting [${request.origin.version}] [${request.httpMethod.value}] ${request.uri}") | ||
if (!request.isMultipart()) { | ||
logger.debug("with ${parseRequestParameter()}") | ||
} | ||
} | ||
|
||
private suspend fun ApplicationCall.parseRequestParameter(): String = | ||
when (request.httpMethod) { | ||
HttpMethod.Get -> request.queryString() | ||
HttpMethod.Post -> receive<String>() | ||
else -> "<method: $request.httpMethod>" | ||
} |
24 changes: 24 additions & 0 deletions
24
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/hooks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.mamoe.mirai.api.http.adapter.http.plugin | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
|
||
internal object Monitor : Hook<suspend (ApplicationCall) -> Unit> { | ||
override fun install(pipeline: ApplicationCallPipeline, handler: suspend (ApplicationCall) -> Unit) { | ||
pipeline.intercept(ApplicationCallPipeline.Monitoring) { | ||
handler(call) | ||
} | ||
} | ||
} | ||
|
||
internal object ReceiveBodyTransformed : Hook<suspend (ApplicationCall, Any) -> 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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.