1 parent
dab6dc5bc4
commit
c5680f7b06
10 files changed
+222
-118
No files matched your search
@@ -12,11 +12,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Playlists, streams, podcasts and related channels to Channel UI
|
||||
- Pagination supported
|
||||
- Channels show up in web app (but no UI for channel details yet)
|
||||
- Support for `Etag` header on the web server to improve performance with caching
|
||||
|
||||
### Fixed
|
||||
|
||||
- Another bug where the app crashes if an error happens loading channel info
|
||||
|
||||
### Removed
|
||||
|
||||
- Partial content (`Range` header) from the web server, since we never serve very big files
|
||||
|
||||
## [0.12.1] - 2023-09-19
|
||||
|
||||
### Added
|
||||
|
||||
@@ -6,6 +6,7 @@ import "pkg:/components/Web/PlayletWebServer/Middleware/PlayQueueRouter.bs"
|
||||
import "pkg:/components/Web/WebServer/Middleware/CorsMiddleware.bs"
|
||||
import "pkg:/components/Web/WebServer/Middleware/HttpStaticFilesRouter.bs"
|
||||
import "pkg:/components/Web/PlayletWebServer/Middleware/PlayletLibUrlsRouter.bs"
|
||||
import "pkg:/components/Web/WebServer/Middleware/EtagMiddleware.bs"
|
||||
|
||||
function Init()
|
||||
m.top.packageName = "Playlet"
|
||||
@@ -37,14 +38,22 @@ function SetupRoutes(server as object)
|
||||
server.UseRouter(new Http.PlayQueueRouter())
|
||||
server.UseRouter(new Http.SearchHistoryRouter())
|
||||
server.UseRouter(new Http.PlayletLibUrlsRouter())
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", false, false, false))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/config", "libpkg:/config", false, false, false))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/logs", "cachefs:/logs", false, false))
|
||||
|
||||
etags = new Http.EtagUtils()
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", etags, { staticFiles: true }))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/config", "libpkg:/config", etags, { staticFiles: true }))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/logs", "cachefs:/logs", etags, { fsCheck: true }))
|
||||
#if DEBUG
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/libpkg", "libpkg:/", true, true, false))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/pkg", "pkg:/", true, true))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/tmp", "tmp:/", true, true))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/cachefs", "cachefs:/", true, true))
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/common", "common:/", true, true))
|
||||
options = { showDirectories: true, showHidden: true, staticFiles: true }
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/libpkg", "libpkg:/", etags, options))
|
||||
options = { showDirectories: true, showHidden: true, fsCheck: true, staticFiles: true }
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/pkg", "pkg:/", etags, options))
|
||||
options = { showDirectories: true, showHidden: true }
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/tmp", "tmp:/", etags, options))
|
||||
options = { showDirectories: true, showHidden: true }
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/cachefs", "cachefs:/", etags, options))
|
||||
options = { showDirectories: true, showHidden: true, fsCheck: true, staticFiles: true }
|
||||
server.UseRouter(new Http.HttpStaticFilesRouter("/debug/common", "common:/", etags, options))
|
||||
#end if
|
||||
server.UsePostRouter(new Http.EtagMiddleware(etags))
|
||||
end function
|
||||
@@ -24,10 +24,6 @@ namespace Http
|
||||
private state = Http.HttpRequestState.INIT
|
||||
private id = 0
|
||||
' copied members
|
||||
private range_begin = 0
|
||||
private range_end = 0
|
||||
private range_begin_given = false
|
||||
private range_end_given = false
|
||||
private ready_for_response = false
|
||||
|
||||
function new()
|
||||
@@ -197,7 +193,6 @@ namespace Http
|
||||
end function
|
||||
|
||||
function ProcessHeaders(connection as HttpConnection)
|
||||
m.ParseRange()
|
||||
m.ParseConnection(connection)
|
||||
end function
|
||||
|
||||
@@ -212,28 +207,6 @@ namespace Http
|
||||
end if
|
||||
end function
|
||||
|
||||
' TODO:P2 this function does not take into account multiple ranges.
|
||||
function ParseRange()
|
||||
range = m.headers.range
|
||||
if range <> invalid
|
||||
endpoints = lcase(range).tokenize("=")
|
||||
if endpoints.count() = 2 and endpoints.GetHead() = "bytes"
|
||||
range = endpoints.GetTail().Trim()
|
||||
hyphen = range.instr("-")
|
||||
if hyphen > 0
|
||||
m.range_begin = strtoi(range.left(hyphen))
|
||||
m.range_begin_given = true
|
||||
end if
|
||||
last = range.len() - 1
|
||||
if hyphen < last
|
||||
m.range_end = strtoi(range.right(last - hyphen))
|
||||
m.range_end_given = true
|
||||
end if
|
||||
end if
|
||||
LogInfo("range request begin" + Stri(m.range_begin) + " end" + Stri(m.range_end))
|
||||
end if
|
||||
end function
|
||||
|
||||
function ParseConnection(connection as HttpConnection)
|
||||
connectionStr = m.headers.connection
|
||||
if connectionStr <> invalid
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import "pkg:/source/utils/TimeUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "HttpResponseSource.bs"
|
||||
|
||||
namespace Http
|
||||
function GetHttpResponseId() as integer
|
||||
@@ -9,8 +8,6 @@ namespace Http
|
||||
end function
|
||||
|
||||
class HttpResponse
|
||||
private source = Http.HttpResponseSource.NONE
|
||||
|
||||
' members
|
||||
private body_buffer = invalid
|
||||
private header_buffer = invalid
|
||||
@@ -42,7 +39,14 @@ namespace Http
|
||||
routers = m.server.routers
|
||||
for each router in routers
|
||||
if router.OnRequest(m.request, m, connection)
|
||||
return true
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
|
||||
postRouters = m.server.postRouters
|
||||
for each router in postRouters
|
||||
if router.OnRequest(m.request, m, connection)
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
|
||||
@@ -62,8 +66,6 @@ namespace Http
|
||||
|
||||
m.SetBodyDataString(buffer)
|
||||
m.ContentType("text/html")
|
||||
|
||||
m.source = Http.HttpResponseSource.GENERATED
|
||||
end function
|
||||
|
||||
function Redirect(where as string, code = 302 as integer)
|
||||
@@ -78,8 +80,6 @@ namespace Http
|
||||
buffer = FormatJson(data)
|
||||
m.SetBodyDataString(buffer)
|
||||
m.ContentType("application/json")
|
||||
|
||||
m.source = Http.HttpResponseSource.GENERATED
|
||||
end function
|
||||
|
||||
function SetBodyDataBuffer(buffer as object)
|
||||
@@ -94,6 +94,20 @@ namespace Http
|
||||
m.ContentLength(m.body_buffer_length)
|
||||
end function
|
||||
|
||||
function SetBodyDataFile(path as string)
|
||||
m.body_buffer.ReadFile(path)
|
||||
m.body_buffer_length = m.body_buffer.count()
|
||||
m.ContentLength(m.body_buffer_length)
|
||||
end function
|
||||
|
||||
function ClearBody(clearContentLength = true as boolean)
|
||||
m.body_buffer.clear()
|
||||
m.body_buffer_length = 0
|
||||
if clearContentLength
|
||||
m.ContentLength(0)
|
||||
end if
|
||||
end function
|
||||
|
||||
function ContentLength(length as integer)
|
||||
m.headers["Content-Length"] = Stri(length).trim()
|
||||
end function
|
||||
@@ -104,13 +118,6 @@ namespace Http
|
||||
|
||||
function Send(sock as object, bufsize as integer) as integer
|
||||
sent = -1
|
||||
if m.source = Http.HttpResponseSource.FROMFILE
|
||||
if m.start + m.sent >= m.buf_start + m.body_buffer.count()
|
||||
m.buf_start = m.start + m.sent
|
||||
m.body_buffer.ReadFile(m.path, m.buf_start, bufsize)
|
||||
LogDebug(`Read ${m.body_buffer.count()} bytes from source file @${m.buf_start}`)
|
||||
end if
|
||||
end if
|
||||
buf_pos = m.start + m.sent - m.buf_start
|
||||
buf_remaining = m.body_buffer.count() - buf_pos
|
||||
if buf_remaining = 0
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace Http
|
||||
|
||||
enum HttpResponseSource
|
||||
NONE = 0
|
||||
GENERATED = 1
|
||||
FROMFILE = 2
|
||||
end enum
|
||||
|
||||
end namespace
|
||||
@@ -18,6 +18,7 @@ namespace Http
|
||||
private total_out = 0
|
||||
|
||||
private routers
|
||||
private postRouters
|
||||
|
||||
public success as boolean
|
||||
public settings as Http.HttpSettings
|
||||
@@ -27,6 +28,7 @@ namespace Http
|
||||
m.settings = settings
|
||||
m.task = task
|
||||
m.routers = []
|
||||
m.postRouters = []
|
||||
m.utils = new Utils()
|
||||
m.success = m.Init()
|
||||
end function
|
||||
@@ -76,6 +78,10 @@ namespace Http
|
||||
m.routers.Push(router)
|
||||
end function
|
||||
|
||||
function UsePostRouter(router as HttpRouter)
|
||||
m.postRouters.Push(router)
|
||||
end function
|
||||
|
||||
function PreWait()
|
||||
connections = m.connections
|
||||
for each id in connections
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -10,6 +10,10 @@ function IsAssociativeArray(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifAssociativeArray") <> invalid
|
||||
end function
|
||||
|
||||
function IsByteArray(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifByteArray") <> invalid
|
||||
end function
|
||||
|
||||
function IsInt(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifInt") <> invalid
|
||||
end function
|
||||
@@ -34,6 +38,14 @@ function ValidInt(obj as dynamic) as integer
|
||||
end if
|
||||
end function
|
||||
|
||||
function ValidBool(obj as dynamic) as boolean
|
||||
if obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
|
||||
return obj
|
||||
else
|
||||
return false
|
||||
end if
|
||||
end function
|
||||
|
||||
function FuncName(func as function) as string
|
||||
return (LCase(`${func}`)).replace("function:", "").trim()
|
||||
end function
|
||||
Reference in new issue
Block a user