From e804fdc0655cb95328250f29e46aee403de040b1 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 28 Jun 2022 16:59:40 +0100 Subject: [PATCH] Add Cache-Control and ETag headers to the exportGroup endpoint Signed-off-by: Graham --- .../openrs2/archive/web/CachesController.kt | 21 ++++++++++++++++++- .../org/openrs2/archive/web/WebServer.kt | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt b/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt index 5f37e2cff2..a604c19099 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt @@ -1,10 +1,15 @@ package org.openrs2.archive.web import io.ktor.application.ApplicationCall +import io.ktor.http.CacheControl import io.ktor.http.ContentDisposition import io.ktor.http.ContentType import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode +import io.ktor.http.content.CachingOptions +import io.ktor.http.content.EntityTagVersion +import io.ktor.http.content.caching +import io.ktor.http.content.versions import io.ktor.response.header import io.ktor.response.respond import io.ktor.response.respondBytes @@ -21,8 +26,10 @@ import org.openrs2.buffer.use import org.openrs2.cache.DiskStoreZipWriter import org.openrs2.cache.FlatFileStoreTarWriter import org.openrs2.compress.gzip.GzipLevelOutputStream +import org.openrs2.crypto.whirlpool import java.nio.file.attribute.FileTime import java.time.Instant +import java.util.Base64 import java.util.zip.Deflater import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream @@ -89,8 +96,20 @@ public class CachesController @Inject constructor( return } + val etag = Base64.getEncoder().encodeToString(buf.whirlpool().sliceArray(0 until 16)) + val bytes = ByteBufUtil.getBytes(buf, 0, buf.readableBytes(), false) - call.respondBytes(bytes, contentType = ContentType.Application.OctetStream) + call.respondBytes(bytes, contentType = ContentType.Application.OctetStream) { + caching = CachingOptions( + cacheControl = CacheControl.MaxAge( + maxAgeSeconds = 86400, + visibility = CacheControl.Visibility.Public, + ), + ) + versions = listOf( + EntityTagVersion(etag, weak = false), + ) + } } } 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 010819ac5e..90aae90d0c 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/WebServer.kt @@ -5,6 +5,8 @@ import io.ktor.application.ApplicationCall import io.ktor.application.call import io.ktor.application.install import io.ktor.features.CORS +import io.ktor.features.CachingHeaders +import io.ktor.features.ConditionalHeaders import io.ktor.features.ContentNegotiation import io.ktor.features.XForwardedHeaderSupport import io.ktor.http.ContentType @@ -36,6 +38,9 @@ public class WebServer @Inject constructor( ) { public fun start(address: String, port: Int) { embeddedServer(CIO, host = address, port = port) { + install(CachingHeaders) + install(ConditionalHeaders) + install(CORS) { anyHost() }