Skip to content

Commit

Permalink
Fix http server encoding error when receiving json post text. Closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
yyuueexxiinngg committed Aug 9, 2020
1 parent e681eed commit 7fb93ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ tasks {

val runMiraiConsole by creating(JavaExec::class.java) {
group = "mirai"
main = "mirai.RunMirai"
dependsOn(shadowJar)
dependsOn(testClasses)

Expand Down Expand Up @@ -91,7 +92,6 @@ tasks {
copyBuildOutput()

classpath = sourceSets["test"].runtimeClasspath
main = "mirai.RunMirai"
standardInput = System.`in`
args(miraiCoreVersion, miraiConsoleVersion)
}
Expand Down
22 changes: 20 additions & 2 deletions src/main/kotlin/tech/mihoyo/mirai/web/http/HttpApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import tech.mihoyo.mirai.callMiraiApi
import tech.mihoyo.mirai.data.common.CQResponseDTO
import tech.mihoyo.mirai.util.logger
import tech.mihoyo.mirai.util.toJson
import java.nio.charset.Charset
import kotlin.coroutines.EmptyCoroutineContext

@ExperimentalCoroutinesApi
Expand Down Expand Up @@ -252,7 +253,7 @@ internal inline fun Route.cqHttpApi(
}
post {
if (checkAccessToken(call, serviceConfig)) {
body(Pair(Json.parseJson(call.receiveText()).jsonObject, false))
body(Pair(Json.parseJson(call.receiveTextWithCorrectEncoding()).jsonObject, false))
}
}
}
Expand All @@ -269,7 +270,7 @@ internal inline fun Route.cqHttpApi(
}
post {
if (checkAccessToken(call, serviceConfig)) {
val req = call.receiveText()
val req = call.receiveTextWithCorrectEncoding()
call.responseDTO(CQResponseDTO.CQAsyncStarted())
CoroutineScope(EmptyCoroutineContext).launch {
body(Pair(Json.parseJson(req).jsonObject, true))
Expand All @@ -278,3 +279,20 @@ internal inline fun Route.cqHttpApi(
}
}
}

// https://github.com/ktorio/ktor/issues/384#issuecomment-458542686
/**
* Receive the request as String.
* If there is no Content-Type in the HTTP header specified use ISO_8859_1 as default charset, see https://www.w3.org/International/articles/http-charset/index#charset.
* But use UTF-8 as default charset for application/json, see https://tools.ietf.org/html/rfc4627#section-3
*/
private suspend fun ApplicationCall.receiveTextWithCorrectEncoding(): String {
fun ContentType.defaultCharset(): Charset = when (this) {
ContentType.Application.Json -> Charsets.UTF_8
else -> Charsets.ISO_8859_1
}

val contentType = request.contentType()
val suitableCharset = contentType.charset() ?: contentType.defaultCharset()
return receiveStream().bufferedReader(charset = suitableCharset).readText()
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ class HttpApiServer(
fun close() {
server.stop(5000, 5000)
}

}

0 comments on commit 7fb93ee

Please sign in to comment.