Add Etag support (#151)

* Add Etag support

* small tweak
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-09-23 18:22:23 -04:00
1 parent dab6dc5bc4
commit c5680f7b06
10 files changed
+222 -118

No files matched your search

@@ -0,0 +1,33 @@
import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/Types.bs"
import "EtagUtils.bs"
namespace Http
class EtagMiddleware extends HttpRouter
function new(etags as EtagUtils)
super()
m.etags = etags
m.Get("*", function(context as object) as boolean
router = context.router
request = context.request
response = context.response
if response.http_code <> 200
return false
end if
router.etags.AddEtagFromBody(request, response)
router.etags.ClearContentIfNoneMatch(request, response)
return false
end function)
end function
end class
end namespace
@@ -0,0 +1,87 @@
namespace Http
class EtagUtils
function new()
m.pathToEtags = {}
end function
function AddEtagFromBody(request as object, response as object, path = "" as string) as boolean
cacheControl = request.headers["Cache-Control"]
if IsString(cacheControl) and cacheControl.instr("no-cache") <> -1
return false
end if
etag = response.headers["ETag"]
if not StringUtils.IsNullOrEmpty(etag)
return false
end if
body = response.body_buffer
if not IsByteArray(body)
return false
end if
response.headers["ETag"] = `"${m.HashContent(body)}"`
if not StringUtils.IsNullOrEmpty(path)
m.pathToEtags[path] = {
etag: response.headers["ETag"],
size: body.count()
}
end if
return true
end function
function AddFileEtagFromCache(request as object, response as object, path as string) as boolean
if not m.pathToEtags.DoesExist(path)
return false
end if
cacheControl = request.headers["Cache-Control"]
if IsString(cacheControl) and cacheControl.instr("no-cache") <> -1
return false
end if
etag = response.headers["ETag"]
if not StringUtils.IsNullOrEmpty(etag)
return false
end if
etagObject = m.pathToEtags[path]
response.headers["ETag"] = etagObject.etag
response.ContentLength(etagObject.size)
return true
end function
function ClearContentIfNoneMatch(request as object, response as object) as boolean
ifNoneMatch = request.headers["If-None-Match"]
if StringUtils.IsNullOrEmpty(ifNoneMatch)
return false
end if
etag = response.headers["ETag"]
if StringUtils.IsNullOrEmpty(etag)
return false
end if
if etag <> ifNoneMatch
return false
end if
LogInfo("ETag cache hit:", etag)
response.http_code = 304
' The response should not contain the body with a 304, but it should contain
' the original Content-Length header.
response.ClearBody(false)
return true
end function
function HashContent(content as object) as string
digest = CreateObject("roEVPDigest")
digest.Setup("md5")
return digest.Process(content)
end function
end class
end namespace
@@ -5,18 +5,31 @@ namespace Http
class HttpStaticFilesRouter extends HttpRouter
private root
private route
private etags
private showDirectories
private showHidden
private fsCheck
private staticFiles
function new(route as string, root as string, showDirectories = false as boolean, showHidden = false as boolean, fsCheck = true as boolean)
function new(route as string, root as string, etags as object, options = {} as object)
super()
m.root = root
m.route = route
m.showDirectories = showDirectories
m.showHidden = showHidden
m.fsCheck = fsCheck
m.etags = etags
' showDirectories: if true, will generate a directory listing for directories. Otherwise, 403.
m.showDirectories = ValidBool(options.showDirectories)
' showHidden: if true, will show hidden files. Hidden files are those that start with a dot.
' Actual attributes not checked.
m.showHidden = ValidBool(options.showHidden)
' fsCheck: if true, will use roFilesystem to check if file exists.
' Otherwise, will use roByteArray.ReadFile() to check if file exists.
' fsCheck should be false primarily for libpkg:/ files, which do not work with roFilesystem.
m.fsCheck = ValidBool(options.fsCheck)
' staticFiles: if true, Etag tags will be cached to skip reading the file entirely.
m.staticFiles = ValidBool(options.staticFiles)
m.Get("*", function(context as object) as boolean
router = context.router
@@ -33,6 +46,16 @@ namespace Http
decoded_path = path_in_route.DecodeUri()
full_path = StringUtils.PathJoin(router.root, decoded_path)
if router.staticFiles
' Although we have an Etag middleware, having a check here allows us to
' skip reading the file entirely if the Etag matches.
if router.etags.AddFileEtagFromCache(request, response, full_path)
if router.etags.ClearContentIfNoneMatch(request, response)
return true
end if
end if
end if
if not router.fsCheck
' libpkg:/ does not work with roFilesystem...
buffer = createObject("roByteArray")
@@ -41,7 +64,10 @@ namespace Http
response.SetBodyDataBuffer(buffer)
response.ContentType(mimetype)
response.http_code = 200
response.source = Http.HttpResponseSource.GENERATED
if router.staticFiles
router.etags.AddEtagFromBody(request, response, full_path)
end if
return true
end if
end if
@@ -70,13 +96,19 @@ namespace Http
end if
response.ContentType("text/html")
router.GenerateDirectoryListing(path, router, request, response)
if router.staticFiles
router.etags.AddEtagFromBody(request, response, full_path)
end if
return true
else if stat.type = "file"
mimetype = server.utils.MimeType(decoded_path)
response.ContentType(mimetype)
response.fileLength = stat.size
LogInfo("uri:", request.uri, "target:", path, "content-type:", mimetype, "content-length", response.fileLength)
router.GenerateFile(path, router, request, response)
response.SetBodyDataFile(path)
response.http_code = 200
LogInfo("uri:", request.uri, "target:", path, "content-type:", mimetype, "content-length", stat.size)
if router.staticFiles
router.etags.AddEtagFromBody(request, response, full_path)
end if
return true
else
response.Default(403, "Not a regular file.")
@@ -85,56 +117,6 @@ namespace Http
end function)
end function
function GenerateFile(path as object, router as object, request as HttpRequest, response as HttpResponse) as boolean
response.path = path
response.source = Http.HttpResponseSource.FROMFILE
fileFinish = response.fileLength - 1
if request.range_begin_given or request.range_end_given
start = 0
finish = 0
if request.range_begin_given and request.range_end_given
' 100-200
start = request.range_begin
finish = request.range_end
' clamp finish to fileFinish
if finish > fileFinish
finish = fileFinish
end if
else if request.range_begin_given and not request.range_end_given
' 100- :: yields 100 to end
start = request.range_begin
finish = fileFinish
else if not request.range_begin_given and request.range_end_given
' -200 :: yields last 200
finish = fileFinish
start = finish - request.range_end + 1
' check for wrapping
if start > finish
start = 0
end if
else
LogError(1, "internal error - range start/finish logic inconsistency")
end if
response.start = start
response.body_buffer_length = finish - start + 1
response.ContentLength(response.body_buffer_length)
response.headers["Content-Range"] = "bytes " + response.MakeRange(response.start, response.body_buffer_length, response.filelength)
response.http_code = 206
LogInfo("sending range " + response.MakeRange(start, response.body_buffer_length, response.fileLength))
else ' no range stuff
response.body_buffer_length = response.fileLength
response.ContentLength(response.body_buffer_length)
response.http_code = 200
end if
response.headers["Accept-Ranges"] = "bytes"
response.KeepAlive(true)
return true
end function
function GenerateDirectoryListing(path as object, router as object, request as HttpRequest, response as HttpResponse) as boolean
fs = CreateObject("roFilesystem")
dirList = fs.GetDirectoryListing(path)
@@ -205,7 +187,6 @@ namespace Http
response.SetBodyDataString(listing)
response.http_code = 200
response.KeepAlive(true)
response.source = Http.HttpResponseSource.GENERATED
return true
end function
end class