Add Cache-Control and ETag headers to the exportGroup endpoint

Signed-off-by: Graham <gpe@openrs2.org>
Graham 2 years ago
parent 9c9a1ecf39
commit e804fdc065
  1. 21
      archive/src/main/kotlin/org/openrs2/archive/web/CachesController.kt
  2. 5
      archive/src/main/kotlin/org/openrs2/archive/web/WebServer.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),
)
}
}
}

@ -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()
}

Loading…
Cancel
Save