Mark Malik
06/07/2022, 3:57 PMjava.lang.UnsupportedOperationException: Headers can no longer be set because response was already completed
at io.ktor.server.netty.http1.NettyHttp1ApplicationResponse$headers$1.engineAppendHeader(NettyHttp1ApplicationResponse.kt:42)
at io.ktor.server.response.ResponseHeaders.append(ResponseHeaders.kt:57)
at io.ktor.server.response.ResponseHeaders.append$default(ResponseHeaders.kt:48)
at io.ktor.server.response.ApplicationResponsePropertiesKt.header(ApplicationResponseProperties.kt:14)
at io.ktor.server.plugins.cors.CORSKt.corsVary(CORS.kt:226)
This is the plugin code:
val HealthChecks: ApplicationPlugin<HealthConfig> = createApplicationPlugin("HealthChecks", ::HealthConfig) {
on(CallSetup) { call ->
if (call.request.path() != "/monitoring/health-check" && call.request.httpMethod != HttpMethod.Get) return@on
var healthCheckStatus: Map<String, Boolean>
runBlocking {
healthCheckStatus = pluginConfig.performChecks()
}
val statusCode = when (healthCheckStatus.all { it.value }) {
true -> HttpStatusCode.OK
false -> HttpStatusCode.InternalServerError
}
call.respondText(Json.encodeToString(healthCheckStatus), ContentType.Application.Json, statusCode)
}
}
I thought that I could use the plugin to expose request handlers not requiring to create a routing. Is there something invalid about this setup ?
FYI.
I have CORS enabled with anyHosts() option.
The error is thrown in
private fun ApplicationCall.corsVary() {
val vary = response.headers[HttpHeaders.Vary]
if (vary == null) {
response.header(HttpHeaders.Vary, HttpHeaders.Origin)
} else {
response.header(HttpHeaders.Vary, vary + ", " + HttpHeaders.Origin)
}
}
Rustam Siniukov
06/07/2022, 4:00 PM