Add debug web server

This commit is contained in:
Brahim Hadriche committed 2022-11-29 23:13:52 -05:00
1 parent 68a32afc38
commit a059702a0e
5 files changed
+49 -12

No files matched your search

@@ -35,7 +35,7 @@ namespace Http
response.Default(204, "OK") response.Default(204, "OK")
end if end if
return true return true
end function) end function)
end function end function
end class end class
@@ -71,8 +71,8 @@ namespace Http
sets = period.GetChildNodes() sets = period.GetChildNodes()
for each set in sets for each set in sets
attributes = set.GetAttributes() attributes = set.GetAttributes()
contentType = attributes.contentType contentType = attributes.contentType
mimeType = attributes.mimeType mimeType = attributes.mimeType
' If the DASH already has thumbnails, abort ' If the DASH already has thumbnails, abort
if contentType = "image" or (mimeType <> invalid and mimeType.StartsWith("image")) if contentType = "image" or (mimeType <> invalid and mimeType.StartsWith("image"))
return "" return ""
@@ -4,22 +4,34 @@ namespace Http
private log as log.Logger private log as log.Logger
private root private root
private filesOnly private route
private showDirectories
private showHidden
function new(root as string, filesOnly = false as boolean) function new(route as string, root as string, showDirectories = false as boolean, showHidden = false as boolean)
super() super()
m.log = new log.Logger("HttpStaticFilesRouter") m.log = new log.Logger("HttpStaticFilesRouter")
m.root = root m.root = root
m.filesOnly = filesOnly m.route = route
m.showDirectories = showDirectories
m.showHidden = showHidden
m.Get("*", function(context as object) as boolean m.Get("*", function(context as object) as boolean
router = context.router router = context.router
request = context.request request = context.request
response = context.response response = context.response
decoded_path = WebUtils.UrlUnescape(request.route) if not request.route.StartsWith(router.route)
path = CreateObject("roPath", router.root + decoded_path) return false
end if
path_in_route = request.route.Mid(router.route.Len())
decoded_path = WebUtils.UrlUnescape(path_in_route)
full_path = PathJoin(router.root, decoded_path)
path = CreateObject("roPath", full_path)
fs = CreateObject("roFilesystem") fs = CreateObject("roFilesystem")
if not path.isValid() if not path.isValid()
@@ -37,9 +49,9 @@ namespace Http
end if end if
if stat.type = "directory" if stat.type = "directory"
if router.filesOnly if not router.showDirectories
response.Default(403, "Forbidden") response.Default(403, "Forbidden")
return false return true
end if end if
response.ContentType("text/html") response.ContentType("text/html")
router.GenerateDirectoryListing(path, router, request, response) router.GenerateDirectoryListing(path, router, request, response)
@@ -141,8 +153,19 @@ namespace Http
if uriDir.len() > 0 and uriDir.right(1) <> "/" if uriDir.len() > 0 and uriDir.right(1) <> "/"
uriDir = uriDir + "/" uriDir = uriDir + "/"
end if end if
routeDir = router.route
if routeDir.len() > 0 and routeDir.right(1) <> "/"
routeDir = routeDir + "/"
end if
if not uriDir = routeDir
uri = uriDir + ".."
listing = listing + "<a href=" + Quote() + uri + Quote() + ">" + ".." + "</a>" + UnixNL()
end if
for each item in dirList for each item in dirList
if item.left(1) <> "." if router.showHidden or item.left(1) <> "."
indicator = invalid indicator = invalid
stat = fs.stat(dir + item) stat = fs.stat(dir + item)
if stat <> invalid if stat <> invalid
@@ -11,6 +11,7 @@ import "pkg:/components/WebServer/Middleware/BasicAuthMiddleware.bs"
import "pkg:/components/WebServer/Middleware/DashRouter.bs" import "pkg:/components/WebServer/Middleware/DashRouter.bs"
#const WEB_SERVER_BASIC_AUTH = false #const WEB_SERVER_BASIC_AUTH = false
#const WEB_SERVER_PACKAGE_DEBUG = false
function Init() function Init()
m.top.functionName = "WebServerLoop" m.top.functionName = "WebServerLoop"
@@ -38,7 +39,10 @@ function WebServerLoop()
m.server.UseRouter(new Http.CommandsApiRouter()) m.server.UseRouter(new Http.CommandsApiRouter())
m.server.UseRouter(new Http.StateApiRouter()) m.server.UseRouter(new Http.StateApiRouter())
m.server.UseRouter(new Http.InvidiousRouter()) m.server.UseRouter(new Http.InvidiousRouter())
m.server.UseRouter(new Http.HttpStaticFilesRouter(m.settings.WwwRoot)) m.server.UseRouter(new Http.HttpStaticFilesRouter("/", m.settings.WwwRoot))
#if WEB_SERVER_PACKAGE_DEBUG
m.server.UseRouter(new Http.HttpStaticFilesRouter("/debug", "pkg:/", true, true))
#end if
#if WEB_SERVER_BASIC_AUTH #if WEB_SERVER_BASIC_AUTH
m.server.UseRouter(new Http.BasicAuthMiddleware()) m.server.UseRouter(new Http.BasicAuthMiddleware())
#end if #end if
+10
View File
@@ -125,3 +125,13 @@ function StringLastIndexOf(str as string, substr as string) as integer
end while end while
return index return index
end function end function
function PathJoin(path1 as string, path2 as string) as string
if path1.EndsWith("/")
path1 = path1.left(path1.len() - 1)
end if
if path2.StartsWith("/")
path2 = path2.Mid(1)
end if
return path1 + "/" + path2
end function