This repository was archived by the owner on Dec 6, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-server.lua
More file actions
91 lines (75 loc) · 2.56 KB
/
http-server.lua
File metadata and controls
91 lines (75 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
-- a majority of this http server logic was borrowed from other numelon projects
local http = require("coro-http")
local fs = require("coro-fs").chroot("./ui") -- ui directory is purely for testing - will be something else later
local url = require("url")
local querystring = require("querystring")
local urldecode = querystring.urldecode
local function output(code, reason, mimetype, content)
return {
{ "content-length", tostring(#content) },
{ "content-type", mimetype },
code = code,
reason = reason
}, content
end
local mimes = {
["htm"] = "text/html",
["html"] = "text/html",
["css"] = "text/css",
["js"] = "text/javascript",
["mjs"] = "text/javascript",
["json"] = "application/json",
["xml"] = "application/xml",
["txt"] = "text/plain",
["csv"] = "text/csv",
-- images
["png"] = "image/png",
["jpg"] = "image/jpeg",
["jpeg"] = "image/jpeg",
["gif"] = "image/gif",
["svg"] = "image/svg+xml",
["webp"] = "image/webp",
["ico"] = "image/vnd.microsoft.icon",
["bmp"] = "image/bmp",
["avif"] = "image/avif",
-- fonts
["woff"] = "font/woff",
["woff2"] = "font/woff2",
["ttf"] = "font/ttf",
["otf"] = "font/otf",
-- audio / video
["mp3"] = "audio/mpeg",
["wav"] = "audio/wav",
["ogg"] = "audio/ogg",
["mp4"] = "video/mp4",
["webm"] = "video/webm",
["ogv"] = "video/ogg",
-- compressed archivws
["zip"] = "application/zip",
["gz"] = "application/gzip",
["tar"] = "application/x-tar",
["rar"] = "application/vnd.rar",
["7z"] = "application/x-7z-compressed",
}
-- TODO: generate random port and check if occupied
-- then make function return port so it can be used in lya.main()
return function()
http.createServer("127.0.0.1", 8392, function(request)
local parsed = url.parse(request.path) ---@diagnostic disable-line undefined-field
local path = "./" .. parsed.pathname:gsub("^/", ""):gsub("/$", "")
local content, err = fs.readFile(path)
if not content then
path = path .. "/index.html" -- try index
content, err = fs.readFile(path)
end
if not content then
return output(404, "Not Found", "text/plain", err)
end
local ext_mime = (path:match("^.+%.([%w]+)$") or ""):lower()
ext_mime = mimes[ext_mime] or "application/octet-stream"
return output(200, "OK", ext_mime, content)
end)
return 8392
end
-- TODO:
-- / --> index.html