Refactor project
This commit is contained in:
1 parent
9c1a2b987b
commit
2f2516eade
115 files changed
+12522
No files matched your search
@@ -0,0 +1,24 @@
|
||||
namespace Http
|
||||
|
||||
class HttpDefaultRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.All("*", function(router as object, request as HttpRequest, response as HttpResponse) as boolean
|
||||
method = request.method
|
||||
route = request.uri
|
||||
|
||||
if method = ""
|
||||
response.Default(400, "You sent a request that the server couldn't understand.")
|
||||
else
|
||||
response.Default(404, `Cannot ${method} ${route}`)
|
||||
end if
|
||||
|
||||
return true
|
||||
end function)
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,177 @@
|
||||
namespace Http
|
||||
|
||||
class HttpStaticFilesRouter extends HttpRouter
|
||||
private log as log.Logger
|
||||
|
||||
private root
|
||||
private filesOnly
|
||||
|
||||
function new(root as string, filesOnly = false as boolean)
|
||||
super()
|
||||
m.log = new log.Logger("HttpStaticFilesRouter")
|
||||
|
||||
m.root = root
|
||||
m.filesOnly = filesOnly
|
||||
|
||||
m.Get("*", function(router as object, request as HttpRequest, response as HttpResponse) as boolean
|
||||
uri = request.uri
|
||||
decoded_uri = WebUtils.UrlUnescape(uri)
|
||||
path = CreateObject("roPath", router.root + decoded_uri)
|
||||
fs = CreateObject("roFilesystem")
|
||||
|
||||
if not path.isValid()
|
||||
' Skip, let next middleware handle it
|
||||
return false
|
||||
else if not fs.exists(path)
|
||||
' Skip, let next middleware handle it
|
||||
return false
|
||||
end if
|
||||
|
||||
stat = fs.stat(path)
|
||||
if stat = invalid
|
||||
response.Default(500, "fstat() failed.")
|
||||
return true
|
||||
end if
|
||||
|
||||
if stat.type = "directory"
|
||||
if router.filesOnly
|
||||
response.Default(403, "Forbidden")
|
||||
return false
|
||||
end if
|
||||
response.mimetype = "text/html"
|
||||
router.GenerateDirectoryListing(path, router, request, response)
|
||||
return true
|
||||
else if stat.type = "file"
|
||||
' TODO: implement
|
||||
response.mimetype = WebUtils.MimeType(decoded_uri)
|
||||
response.fileLength = stat.size
|
||||
router.log.info("uri=" + uri + ", target=" + path + ", content-type=" + response.mimetype + ", content-length=" + Stri(response.fileLength).trim())
|
||||
router.GenerateFile(path, router, request, response)
|
||||
return true
|
||||
else
|
||||
response.Default(403, "Not a regular file.")
|
||||
return true
|
||||
end if
|
||||
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
|
||||
router.log.error(1, "internal error - range start/finish logic inconsistency")
|
||||
end if
|
||||
|
||||
response.start = start
|
||||
response.length = finish - start + 1
|
||||
response.http_code = 206
|
||||
|
||||
router.log.info("sending range " + response.MakeRange(start, response.length, response.fileLength))
|
||||
else ' no range stuff
|
||||
response.length = response.fileLength
|
||||
response.http_code = 200
|
||||
end if
|
||||
|
||||
response.GenerateHeader()
|
||||
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)
|
||||
if dirList = invalid
|
||||
response.default(500, "Couldn't list directory")
|
||||
return false
|
||||
end if
|
||||
|
||||
' for tabbed alignment of file sizes
|
||||
maxlen = 0
|
||||
for each item in dirList
|
||||
il = item.len()
|
||||
if maxlen < il
|
||||
maxlen = il
|
||||
end if
|
||||
end for
|
||||
|
||||
listing = "<html>" + UnixNL()
|
||||
listing = listing + "<head>" + UnixNL()
|
||||
listing = listing + "<title>" + response.settings.ServerName + "</title>" + UnixNL()
|
||||
listing = listing + "</head>" + UnixNL()
|
||||
listing = listing + "<body>" + UnixNL()
|
||||
listing = listing + "<h1>" + WebUtils.URLUnescape(request.uri) + "</h1>" + UnixNL()
|
||||
listing = listing + "<tt><pre>" + UnixNL()
|
||||
|
||||
dir = path.getString()
|
||||
if dir.len() > 0 and dir.right(1) <> "/"
|
||||
dir = dir + "/"
|
||||
end if
|
||||
uriDir = request.uri
|
||||
if uriDir.len() > 0 and uriDir.right(1) <> "/"
|
||||
uriDir = uriDir + "/"
|
||||
end if
|
||||
for each item in dirList
|
||||
if item.left(1) <> "."
|
||||
indicator = invalid
|
||||
stat = fs.stat(dir + item)
|
||||
if stat <> invalid
|
||||
if stat.doesexist("type") and stat.type = "directory"
|
||||
indicator = "/"
|
||||
else if stat.doesexist("size")
|
||||
indicator = string(maxlen - item.len(), " ") + Stri(stat.size)
|
||||
end if
|
||||
end if
|
||||
if indicator <> invalid
|
||||
uri = uriDir + item
|
||||
safe_url = uri ' UrlEncode(uri) ' too much encoding
|
||||
listing = listing + "<a href=" + Quote() + safe_url + Quote() + ">" + item
|
||||
listing = listing + indicator
|
||||
listing = listing + "</a>" + UnixNL()
|
||||
else
|
||||
router.log.warn("insufficient stat info to include '" + dir + item + "'")
|
||||
end if
|
||||
end if
|
||||
end for
|
||||
|
||||
listing = listing + "</pre></tt>" + UnixNL()
|
||||
listing = listing + "<hr>" + UnixNL()
|
||||
|
||||
listing = listing + response.GeneratedBy() + UnixNL()
|
||||
listing = listing + "</body>" + UnixNL()
|
||||
listing = listing + "</html>" + UnixNL()
|
||||
|
||||
response.buf.FromAsciiString(listing)
|
||||
response.length = response.buf.count()
|
||||
|
||||
response.http_code = 200
|
||||
response.GenerateHeader()
|
||||
response.source = Http.HttpResponseSource.GENERATED
|
||||
return true
|
||||
end function
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace Http
|
||||
|
||||
class InvidiousRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.Get("*", function(router as object, request as Http.HttpRequest, response as Http.HttpResponse) as boolean
|
||||
uri = request.uri
|
||||
if uri.StartsWith("/token_callback")
|
||||
components = WebUtils.UrlQueryComponents(uri)
|
||||
if components["token"] <> invalid
|
||||
' Token is encoded twice for some reason
|
||||
token = WebUtils.UrlUnescape(components["token"]).DecodeUriComponent()
|
||||
print(`Got Token: ${token}`)
|
||||
RegistryUtils.Write("token", token)
|
||||
end if
|
||||
response.Redirect(Invidious.GetCurrentHost())
|
||||
return true
|
||||
end if
|
||||
return false
|
||||
end function)
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Http
|
||||
|
||||
class RegistryApiRouter extends HttpRouter
|
||||
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.Get("/api/registry", function(router as object, request as HttpRequest, response as HttpResponse) as boolean
|
||||
registryValues = RegistryUtils.ReadAll()
|
||||
response.Json(registryValues)
|
||||
return true
|
||||
end function)
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
Reference in new issue
Block a user