Move Http client from MiraiPlatformUtils to IMirai.Http

Delete MiraiPlatformUtils

Add timeout configuration for ktor HttpClient, fix #673
This commit is contained in:
Him188 2021-01-05 20:43:04 +08:00
parent d99fc810f9
commit 767eb75d4e
5 changed files with 30 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -13,6 +13,7 @@
package net.mamoe.mirai package net.mamoe.mirai
import io.ktor.client.*
import net.mamoe.kjbb.JvmBlockingBridge import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.contact.* import net.mamoe.mirai.contact.*
import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent
@ -55,6 +56,10 @@ public interface IMirai : LowLevelApiAccessor {
@MiraiExperimentalApi @MiraiExperimentalApi
public var FileCacheStrategy: FileCacheStrategy public var FileCacheStrategy: FileCacheStrategy
@Suppress("PropertyName")
@MiraiInternalApi
public val Http: HttpClient
/** /**
* 使用 groupCode 计算 groupUin. 这两个值仅在 mirai 内部协议区分, 一般人使用时无需在意. * 使用 groupCode 计算 groupUin. 这两个值仅在 mirai 内部协议区分, 一般人使用时无需在意.
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -13,6 +13,7 @@
package net.mamoe.mirai.utils package net.mamoe.mirai.utils
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.features.*
import kotlinx.io.core.Input import kotlinx.io.core.Input
import kotlinx.io.core.readAvailable import kotlinx.io.core.readAvailable
import java.io.* import java.io.*
@ -25,13 +26,6 @@ import java.util.zip.Inflater
import kotlin.contracts.InvocationKind import kotlin.contracts.InvocationKind
import kotlin.contracts.contract import kotlin.contracts.contract
public object MiraiPlatformUtils {
/**
* Ktor HttpClient. 不同平台使用不同引擎.
*/
public val Http: HttpClient = HttpClient()
}
@JvmOverloads @JvmOverloads
public fun ByteArray.unzip(offset: Int = 0, length: Int = size - offset): ByteArray { public fun ByteArray.unzip(offset: Int = 0, length: Int = size - offset): ByteArray {
checkOffsetAndLength(offset, length) checkOffsetAndLength(offset, length)

View File

@ -10,6 +10,8 @@
package net.mamoe.mirai.internal package net.mamoe.mirai.internal
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.features.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.request.forms.* import io.ktor.client.request.forms.*
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
@ -68,6 +70,14 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
override var FileCacheStrategy: FileCacheStrategy = net.mamoe.mirai.utils.FileCacheStrategy.PlatformDefault override var FileCacheStrategy: FileCacheStrategy = net.mamoe.mirai.utils.FileCacheStrategy.PlatformDefault
override val Http: HttpClient = HttpClient(OkHttp) {
install(HttpTimeout) {
this.requestTimeoutMillis = 30_0000
this.connectTimeoutMillis = 30_0000
this.socketTimeoutMillis = 30_0000
}
}
@OptIn(LowLevelApi::class) @OptIn(LowLevelApi::class)
override suspend fun acceptNewFriendRequest(event: NewFriendRequestEvent) { override suspend fun acceptNewFriendRequest(event: NewFriendRequestEvent) {
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") @Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
@ -404,7 +414,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
amount: Int amount: Int
): GroupAnnouncementList = bot.asQQAndroidBot().run { ): GroupAnnouncementList = bot.asQQAndroidBot().run {
val rep = bot.asQQAndroidBot().network.run { val rep = bot.asQQAndroidBot().network.run {
MiraiPlatformUtils.Http.post<String> { Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/list_announce") url("https://web.qun.qq.com/cgi-bin/announce/list_announce")
body = MultiPartFormDataContent(formData { body = MultiPartFormDataContent(formData {
append("qid", groupId) append("qid", groupId)
@ -432,7 +442,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
override suspend fun _lowLevelSendAnnouncement(bot: Bot, groupId: Long, announcement: GroupAnnouncement): String = override suspend fun _lowLevelSendAnnouncement(bot: Bot, groupId: Long, announcement: GroupAnnouncement): String =
bot.asQQAndroidBot().run { bot.asQQAndroidBot().run {
val rep = withContext(network.coroutineContext) { val rep = withContext(network.coroutineContext) {
MiraiPlatformUtils.Http.post<String> { Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice") url("https://web.qun.qq.com/cgi-bin/announce/add_qun_notice")
body = MultiPartFormDataContent(formData { body = MultiPartFormDataContent(formData {
append("qid", groupId) append("qid", groupId)
@ -468,7 +478,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
@MiraiExperimentalApi @MiraiExperimentalApi
override suspend fun _lowLevelDeleteAnnouncement(bot: Bot, groupId: Long, fid: String) = bot.asQQAndroidBot().run { override suspend fun _lowLevelDeleteAnnouncement(bot: Bot, groupId: Long, fid: String) = bot.asQQAndroidBot().run {
val data = withContext(network.coroutineContext) { val data = withContext(network.coroutineContext) {
MiraiPlatformUtils.Http.post<String> { Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/del_feed") url("https://web.qun.qq.com/cgi-bin/announce/del_feed")
body = MultiPartFormDataContent(formData { body = MultiPartFormDataContent(formData {
append("qid", groupId) append("qid", groupId)
@ -498,7 +508,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
override suspend fun _lowLevelGetAnnouncement(bot: Bot, groupId: Long, fid: String): GroupAnnouncement = override suspend fun _lowLevelGetAnnouncement(bot: Bot, groupId: Long, fid: String): GroupAnnouncement =
bot.asQQAndroidBot().run { bot.asQQAndroidBot().run {
val rep = network.run { val rep = network.run {
MiraiPlatformUtils.Http.post<String> { Mirai.Http.post<String> {
url("https://web.qun.qq.com/cgi-bin/announce/get_feed") url("https://web.qun.qq.com/cgi-bin/announce/get_feed")
body = MultiPartFormDataContent(formData { body = MultiPartFormDataContent(formData {
append("qid", groupId) append("qid", groupId)
@ -525,7 +535,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
override suspend fun _lowLevelGetGroupActiveData(bot: Bot, groupId: Long, page: Int): GroupActiveData = override suspend fun _lowLevelGetGroupActiveData(bot: Bot, groupId: Long, page: Int): GroupActiveData =
bot.asQQAndroidBot().run { bot.asQQAndroidBot().run {
val rep = network.run { val rep = network.run {
MiraiPlatformUtils.Http.get<String> { Mirai.Http.get<String> {
url("https://qqweb.qq.com/c/activedata/get_mygroup_data") url("https://qqweb.qq.com/c/activedata/get_mygroup_data")
parameter("bkn", bkn) parameter("bkn", bkn)
parameter("gc", groupId) parameter("gc", groupId)
@ -551,7 +561,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
type: GroupHonorType type: GroupHonorType
): GroupHonorListData? = bot.asQQAndroidBot().run { ): GroupHonorListData? = bot.asQQAndroidBot().run {
val rep = network.run { val rep = network.run {
MiraiPlatformUtils.Http.get<String> { Mirai.Http.get<String> {
url("https://qun.qq.com/interactive/honorlist") url("https://qun.qq.com/interactive/honorlist")
parameter("gc", groupId) parameter("gc", groupId)
parameter("type", type.value) parameter("type", type.value)
@ -781,7 +791,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
seconds: Int seconds: Int
) { ) {
bot as QQAndroidBot bot as QQAndroidBot
val response = MiraiPlatformUtils.Http.post<String> { val response = Mirai.Http.post<String> {
url("https://qqweb.qq.com/c/anonymoustalk/blacklist") url("https://qqweb.qq.com/c/anonymoustalk/blacklist")
body = MultiPartFormDataContent(formData { body = MultiPartFormDataContent(formData {
append("anony_id", anonymousId) append("anony_id", anonymousId)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@ -10,6 +10,7 @@
package net.mamoe.mirai.internal.contact package net.mamoe.mirai.internal.contact
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.contact.Friend import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.Stranger import net.mamoe.mirai.contact.Stranger
@ -26,7 +27,6 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.Cmd0x352
import net.mamoe.mirai.internal.network.protocol.packet.chat.image.LongConn import net.mamoe.mirai.internal.network.protocol.packet.chat.image.LongConn
import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.utils.ExternalResource import net.mamoe.mirai.utils.ExternalResource
import net.mamoe.mirai.utils.MiraiPlatformUtils
import net.mamoe.mirai.utils.toUHexString import net.mamoe.mirai.utils.toUHexString
import net.mamoe.mirai.utils.verbose import net.mamoe.mirai.utils.verbose
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@ -81,7 +81,7 @@ internal abstract class AbstractUser(
} }
val time = measureTime { val time = measureTime {
MiraiPlatformUtils.Http.postImage( Mirai.Http.postImage(
"0x6ff0070", "0x6ff0070",
bot.id, bot.id,
null, null,

View File

@ -20,6 +20,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.io.core.* import kotlinx.io.core.*
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.QQAndroidClient
import net.mamoe.mirai.internal.network.protocol.data.proto.CSDataHighwayHead import net.mamoe.mirai.internal.network.protocol.data.proto.CSDataHighwayHead
@ -199,7 +200,7 @@ internal object HighwayHelper {
uKey: ByteArray, uKey: ByteArray,
fileKey: ByteArray, fileKey: ByteArray,
) { ) {
MiraiPlatformUtils.Http.post<String> { Mirai.Http.post<String> {
url("http://$serverIp:$serverPort") url("http://$serverIp:$serverPort")
parameter("ver", 4679) parameter("ver", 4679)
parameter("ukey", uKey.toUHexString("")) parameter("ukey", uKey.toUHexString(""))