From 33ecd68654922c8a6a8c1ad913cffa80934804b4 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 4 Apr 2023 22:56:03 +0100 Subject: [PATCH] Simplify CORS handling This makes caching simpler to reason about (no Vary header) and more efficient (also because there is no Vary header). Signed-off-by: Graham --- .../main/kotlin/org/openrs2/archive/web/WebServer.kt | 12 ++++++++---- gradle/libs.versions.toml | 2 -- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt index 4688841388..9f7e85b5f8 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt @@ -2,7 +2,9 @@ package org.openrs2.archive.web import com.fasterxml.jackson.databind.ObjectMapper import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders import io.ktor.serialization.jackson.JacksonConverter +import io.ktor.server.application.* import io.ktor.server.application.ApplicationCall import io.ktor.server.application.call import io.ktor.server.application.install @@ -14,9 +16,9 @@ import io.ktor.server.plugins.autohead.AutoHeadResponse import io.ktor.server.plugins.cachingheaders.CachingHeaders import io.ktor.server.plugins.conditionalheaders.ConditionalHeaders import io.ktor.server.plugins.contentnegotiation.ContentNegotiation -import io.ktor.server.plugins.cors.routing.CORS import io.ktor.server.plugins.defaultheaders.DefaultHeaders import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders +import io.ktor.server.response.header import io.ktor.server.response.respond import io.ktor.server.response.respondRedirect import io.ktor.server.routing.get @@ -44,9 +46,11 @@ public class WebServer @Inject constructor( install(CachingHeaders) install(ConditionalHeaders) - install(CORS) { - anyHost() - } + install(createApplicationPlugin(name = "CORS") { + onCall { call -> + call.response.header(HttpHeaders.AccessControlAllowOrigin, "*") + } + }) install(ContentNegotiation) { ignoreType() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 69b016331b..fc4e14a646 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -62,7 +62,6 @@ ktor-server-autoHeadResponse = { module = "io.ktor:ktor-server-auto-head-respons ktor-server-cachingHeaders = { module = "io.ktor:ktor-server-caching-headers", version.ref = "ktor" } ktor-server-conditionalHeaders = { module = "io.ktor:ktor-server-conditional-headers", version.ref = "ktor" } ktor-server-contentNegotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" } -ktor-server-cors = { module = "io.ktor:ktor-server-cors", version.ref = "ktor" } ktor-server-defaultHeaders = { module = "io.ktor:ktor-server-default-headers", version.ref = "ktor" } ktor-server-forwardedHeader = { module = "io.ktor:ktor-server-forwarded-header", version.ref = "ktor" } ktor-server-jetty = { module = "io.ktor:ktor-server-jetty", version.ref = "ktor" } @@ -95,7 +94,6 @@ ktor = [ "ktor-server-cachingHeaders", "ktor-server-conditionalHeaders", "ktor-server-contentNegotiation", - "ktor-server-cors", "ktor-server-defaultHeaders", "ktor-server-forwardedHeader", "ktor-server-jetty",