Added X-Cache header to image responses

This commit is contained in:
Amos Ng 2020-06-14 04:35:11 +08:00
parent f725ac910e
commit 2643efa960
No known key found for this signature in database
GPG Key ID: 89086414F634D123
2 changed files with 13 additions and 4 deletions

View File

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- [2020-06-13] Allow for the two log levels to be configurable by [@lflare].
- [2020-06-13] Added X-Cache header to image responses by [@lflare].
### Changed
- [2020-06-13] Modified AsyncAppender queue size to 1024 by [@lflare].

View File

@ -85,7 +85,7 @@ fun getServer(cache: DiskLruCache, serverSettings: ServerSettings, clientSetting
statistics.get().requestsServed.incrementAndGet()
// Netty doesn't do Content-Length or Content-Type, so we have the pleasure of doing that ourselves
fun respondWithImage(input: InputStream, length: String?, type: String, lastModified: String?): Response =
fun respondWithImage(input: InputStream, length: String?, type: String, lastModified: String?, cached: Boolean): Response =
Response(Status.OK)
.header("Content-Type", type)
.header("X-Content-Type-Options", "nosniff")
@ -108,6 +108,13 @@ fun getServer(cache: DiskLruCache, serverSettings: ServerSettings, clientSetting
it
}
}
.let {
if (cached != null && cached == true) {
it.header("X-Cache", "HIT")
} else {
it.header("X-Cache", "MISS")
}
}
val snapshot = cache.get(cacheId)
if (snapshot != null) {
@ -131,7 +138,8 @@ fun getServer(cache: DiskLruCache, serverSettings: ServerSettings, clientSetting
respondWithImage(
CipherInputStream(BufferedInputStream(snapshot.getInputStream(0)), getRc4(rc4Bytes)),
snapshot.getLength(0).toString(), snapshot.getString(1), snapshot.getString(2)
snapshot.getLength(0).toString(), snapshot.getString(1), snapshot.getString(2),
true
)
}
} else {
@ -187,14 +195,14 @@ fun getServer(cache: DiskLruCache, serverSettings: ServerSettings, clientSetting
editor.abort()
}
}
respondWithImage(tee, contentLength, contentType, lastModified)
respondWithImage(tee, contentLength, contentType, lastModified, false)
} else {
editor?.abort()
if (LOGGER.isTraceEnabled) {
LOGGER.trace("Request for $sanitizedUri is being served")
}
respondWithImage(mdResponse.body.stream, contentLength, contentType, lastModified)
respondWithImage(mdResponse.body.stream, contentLength, contentType, lastModified, false)
}
}
}