Revert "Merge branch 'fix-4xx-caching' into expermential_buffer_size_knob"

This reverts commit dcd05dc6a1, reversing
changes made to 3d4843debd.
This commit is contained in:
AviKav 2020-08-04 21:44:59 -04:00
parent 848e0ba170
commit d9a98148b7
2 changed files with 85 additions and 106 deletions

View file

@ -43,7 +43,7 @@ dependencies {
implementation group: "org.xerial", name: "sqlite-jdbc", version: "3.30.1" implementation group: "org.xerial", name: "sqlite-jdbc", version: "3.30.1"
implementation "com.goterl.lazycode:lazysodium-java:4.3.0" implementation "com.goterl.lazycode:lazysodium-java:4.2.6"
implementation "net.java.dev.jna:jna:5.5.0" implementation "net.java.dev.jna:jna:5.5.0"
} }

View file

@ -138,25 +138,13 @@ class ImageServer(
} }
val imageId = printHexString(rc4Bytes) val imageId = printHexString(rc4Bytes)
// These shouldn't be var but I don't want to touch the original conditional logic val snapshot = cache.getUnsafe(imageId.toCacheId())
var snapshot = cache.getUnsafe(imageId.toCacheId()) val imageDatum = synchronized(database) {
var imageDatum = synchronized(database) {
transaction(database) { transaction(database) {
ImageDatum.findById(imageId) ImageDatum.findById(imageId)
} }
} }
// TODO: Cleanup messy conditionals
// Ensure that cached file isn't a non-image from the Great Cache Propagation
val flaresFault = imageDatum != null && imageDatum.contentType.isImageMimetype().not()
if (flaresFault) {
snapshot?.close(); snapshot = null
cache.removeUnsafe(imageId.toCacheId()); imageDatum = null
LOGGER.warn { "Removing cache file for $sanitizedUri left over the Great Cache Propagation" }
}
if (snapshot != null && imageDatum != null) { if (snapshot != null && imageDatum != null) {
request.handleCacheHit(sanitizedUri, getRc4(rc4Bytes), snapshot, imageDatum) request.handleCacheHit(sanitizedUri, getRc4(rc4Bytes), snapshot, imageDatum)
} else { } else {
@ -186,8 +174,6 @@ class ImageServer(
} }
} }
private fun String.isImageMimetype() = this.toLowerCase().startsWith("image/")
private fun Request.handleCacheHit(sanitizedUri: String, cipher: Cipher, snapshot: DiskLruCache.Snapshot, imageDatum: ImageDatum): Response { private fun Request.handleCacheHit(sanitizedUri: String, cipher: Cipher, snapshot: DiskLruCache.Snapshot, imageDatum: ImageDatum): Response {
// our files never change, so it's safe to use the browser cache // our files never change, so it's safe to use the browser cache
return if (this.header("If-Modified-Since") != null) { return if (this.header("If-Modified-Since") != null) {
@ -231,26 +217,19 @@ class ImageServer(
val mdResponse = client(Request(Method.GET, "${serverSettings.imageServer}$sanitizedUri")) val mdResponse = client(Request(Method.GET, "${serverSettings.imageServer}$sanitizedUri"))
val contentType = mdResponse.header("Content-Type")
val contentLength = mdResponse.header("Content-Length")
val lastModified = mdResponse.header("Last-Modified")
if (mdResponse.status != Status.OK) { if (mdResponse.status != Status.OK) {
LOGGER.trace { "Upstream query for $sanitizedUri errored with status ${mdResponse.status}" } LOGGER.trace { "Upstream query for $sanitizedUri errored with status ${mdResponse.status}" }
mdResponse.close()
return Response(mdResponse.status)
}
contentType!!
if (!contentType.isImageMimetype()) {
LOGGER.trace { "Upstream query for $sanitizedUri returned bad mimetype $contentType" }
mdResponse.close() mdResponse.close()
return Response(mdResponse.status) return Response(mdResponse.status)
} }
LOGGER.trace { "Upstream query for $sanitizedUri succeeded" } LOGGER.trace { "Upstream query for $sanitizedUri succeeded" }
val contentType = mdResponse.header("Content-Type")!!
val contentLength = mdResponse.header("Content-Length")
val lastModified = mdResponse.header("Last-Modified")
val editor = cache.editUnsafe(imageId.toCacheId()) val editor = cache.editUnsafe(imageId.toCacheId())
// A null editor means that this file is being written to // A null editor means that this file is being written to