Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UncivServer not correctly handling put request #7095

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions server/src/com/unciv/app/server/UncivServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileNotFoundException
import java.time.Instant


internal object UncivServer {
Expand Down Expand Up @@ -45,33 +48,35 @@ private class UncivServerRunner : CliktCommand() {
embeddedServer(Netty, port = serverPort) {
routing {
get("/isalive") {
println("Received isalive request from ${call.request.local.remoteHost}")
log.info("Received isalive request from ${call.request.local.remoteHost}")
call.respondText("true")
}
put("/files/{fileName}") {
val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!")
log.info("Receiving file: ${fileName}")
val file = File(fileFolderName, fileName)
withContext(Dispatchers.IO) {
val receivedBytes =
call.request.receiveChannel().toInputStream().readBytes()
val textString = String(receivedBytes)
println("Received text: $textString")
File(fileFolderName, fileName).writeText(textString)
file.outputStream().use {
call.request.receiveChannel().toInputStream().copyTo(it)
}
}
call.respond(HttpStatusCode.OK)
}
get("/files/{fileName}") {
val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!")
println("Get file: $fileName")
log.info("File requested: $fileName")
val file = File(fileFolderName, fileName)
if (!file.exists()) {
log.info("File $fileName not found")
call.respond(HttpStatusCode.NotFound, "File does not exist")
return@get
}
val fileText = file.readText()
println("Text read: $fileText")
val fileText = withContext(Dispatchers.IO) { file.readText() }
call.respondText(fileText)
}
delete("/files/{fileName}") {
val fileName = call.parameters["fileName"] ?: throw Exception("No fileName!")
log.info("Deleting file: $fileName")
val file = File(fileFolderName, fileName)
if (!file.exists()) {
call.respond(HttpStatusCode.NotFound, "File does not exist")
Expand All @@ -82,4 +87,4 @@ private class UncivServerRunner : CliktCommand() {
}
}.start(wait = true)
}
}
}