Fix UncivServer not correctly handling put request (#7095)

This commit is contained in:
Timo T
2022-06-08 09:19:02 +02:00
committed by GitHub
parent 0ac89a906d
commit 006d360feb

View File

@ -13,9 +13,12 @@ import io.ktor.server.engine.*
import io.ktor.server.netty.* import io.ktor.server.netty.*
import io.ktor.utils.io.jvm.javaio.* import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.time.Instant
internal object UncivServer { internal object UncivServer {
@ -45,33 +48,35 @@ private class UncivServerRunner : CliktCommand() {
embeddedServer(Netty, port = serverPort) { embeddedServer(Netty, port = serverPort) {
routing { routing {
get("/isalive") { get("/isalive") {
println("Received isalive request from ${call.request.local.remoteHost}") log.info("Received isalive request from ${call.request.local.remoteHost}")
call.respondText("true") call.respondText("true")
} }
put("/files/{fileName}") { put("/files/{fileName}") {
val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!") val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!")
log.info("Receiving file: ${fileName}")
val file = File(fileFolderName, fileName)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val receivedBytes = file.outputStream().use {
call.request.receiveChannel().toInputStream().readBytes() call.request.receiveChannel().toInputStream().copyTo(it)
val textString = String(receivedBytes)
println("Received text: $textString")
File(fileFolderName, fileName).writeText(textString)
} }
} }
call.respond(HttpStatusCode.OK)
}
get("/files/{fileName}") { get("/files/{fileName}") {
val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!") val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!")
println("Get file: $fileName") log.info("File requested: $fileName")
val file = File(fileFolderName, fileName) val file = File(fileFolderName, fileName)
if (!file.exists()) { if (!file.exists()) {
log.info("File $fileName not found")
call.respond(HttpStatusCode.NotFound, "File does not exist") call.respond(HttpStatusCode.NotFound, "File does not exist")
return@get return@get
} }
val fileText = file.readText() val fileText = withContext(Dispatchers.IO) { file.readText() }
println("Text read: $fileText")
call.respondText(fileText) call.respondText(fileText)
} }
delete("/files/{fileName}") { delete("/files/{fileName}") {
val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!") val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!")
log.info("Deleting file: $fileName")
val file = File(fileFolderName, fileName) val file = File(fileFolderName, fileName)
if (!file.exists()) { if (!file.exists()) {
call.respond(HttpStatusCode.NotFound, "File does not exist") call.respond(HttpStatusCode.NotFound, "File does not exist")