-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathladle.lua
More file actions
183 lines (157 loc) · 4.34 KB
/
ladle.lua
File metadata and controls
183 lines (157 loc) · 4.34 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
-----------------------------------------------------
-- Ladle web server
-- Version 0.1.2
-- Original work Copyright (c) 2008 Samuel Saint-Pettersen (original author)
-- Modified work Copyright (c) 2015 Daniel Rempel
-- Released under the MIT License
-----------------------------------------------------
Server = "Ladle"
ServerVersion = "0.1.2"
-- load required modules
socket = require('socket')
-- load server components
ladleutil = require('ladleutil')
-- load configuration file
-- TODO: have a default array with config and merge with the loaded one
if ladleutil.fileExists('config.lua') then
config = require('config')
else
config = nil
end
-- load extensions
extensions = {}
function getHandler(request)
local handler = nil
for k,ext in pairs(extensions) do
if ext["id"] ~= "generic" and ext.match(request["uri"])
then
handler = ext.handler
break
end
end
-- no specific match, use generic handler
if not handler
then
handler = extensions["generic"].handler
end
return handler
end
-- checks whether the root index file is requested and finds an appropriate
-- one if needed
function checkURI(uri)
-- if index file was requested
-- loop til' the first index.* file found
if(uri == "")
then
if ladleutil.fileExists(config["webroot"] .. "index.html")
then
uri = "index.html"
else
local wrootIndex = ladleutil.scandir(config["webroot"])
local chosenIndex = ""
for k,v in pairs(wrootIndex) do
if v:match("^index.*")
then
chosenIndex = "" .. v
break
end
end
uri = chosenIndex
end
end
return uri
end
function serve(request, client)
request["uri"] = checkURI(request["uri"])
-- find an appropriate handler in extensions
local handler = getHandler(request)
-- Got a handler, run it
handler(request, client, config)
-- done with client, close request
client:close()
end
function clientHandler(client)
-- set timeout - 1 minute.
client:settimeout(60)
-- receive request from client
local request_text, err = ladleutil.receiveHTTPRequest(client)
-- if there's no error, begin serving content or kill server
if not err then
-- parse request
local request = ladleutil.parseRequest(request_text)
-- run a prep [e.g. for POST with files - download files]
ladleutil.prepMain(request, client)
-- begin serving content
serve(request, client)
end
end
-- TODO: maybe implement keepalive?
function waitReceive(server)
-- loop while waiting for a client request
while 1 do
-- accept a client request
local client = server:accept()
clientHandler(client)
end
end
function loadExtensions()
local extdir = "extensions/"
local files = ladleutil.scandir(extdir)
for i,v in pairs(files) do
if v:match("^.+%.lua$")
then
local extfile = io.open(extdir .. v, "r")
local extcode
if extfile
then
extcode = extfile:read("*all")
end
if extcode
then
local extf, message = load(extcode)
if not message
then
local ext = extf()
extensions[ext['id']] = ext
ladleutil.trace(("Extension %q loaded successfully"):format(ext['name']))
else
ladleutil.trace(("Failed to load extension %s: %q"):format(v, message))
end
end
end
end
end
-- start web server
function main(arg1)
loadExtensions()
-- command line argument overrides config file entry:
local port = arg1
-- if no port specified on command line, use config entry:
if port == nil then port = config['port'] end
-- if still no port, fall back to default port, 80:
if port == nil then port = 80 end
-- load hostname from config file:
local hostname = config['hostname']
if hostname == nil then hostname = '*' end -- fall back to default
if config["webroot"] == "" or config["webroot"] == nil
then
config["webroot"] = "www/"
end
-- display initial program information
ladleutil.trace(("%s web server v%s"):format(Server,ServerVersion))
ladleutil.trace("Copyright (c) 2008 Samuel Saint-Pettersen")
ladleutil.trace("Copyright (c) 2015 Daniel Rempel")
-- create tcp socket on localhost:$port
local server = socket.bind(hostname, port)
if not server
then
ladleutil.trace("Failed to bind to given hostname:port")
os.exit(1)
end
-- display message to web server is running
ladleutil.trace(("Serving on %s:%d"):format(hostname,port))
waitReceive(server) -- begin waiting for client requests
end
-- invoke program starting point:
-- parameter is command-line argument for port number
main(arg[1])