Skip to content

Commit e937bff

Browse files
author
Develo
committed
Minor fixes, added user events to tbconnection
1 parent b4f4d50 commit e937bff

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

httpserver-request.lua

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,20 @@ end
105105
-- Parses the client's request. Returns a dictionary containing pretty much everything
106106
-- the server needs to know about the uri.
107107
return function (request)
108-
local e = request:find("\r\n", 1, true)
109-
if not e then
110-
print("httpserver-request.lc: nil e")
111-
return nil
112-
end
113-
local line = request:sub(1, e - 1)
114-
local r = {}
115-
local _ = {}
116-
local i = {}
117-
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
118-
r.methodIsValid = validateMethod(r.method)
119-
r.uri = parseUri(r.request)
120-
r.getRequestData = getRequestData(request)
121-
collectgarbage()
122-
return r
108+
local r = {}
109+
local e = request:find("\r\n", 1, true)
110+
if not e then
111+
print("httpserver-request.lc: nil e")
112+
r.methodIsValid = false
113+
return r
114+
end
115+
local line = request:sub(1, e - 1)
116+
local _ = {}
117+
local i = {}
118+
_, i, r.method, r.request = line:find("^([A-Z]+) (.-) HTTP/[1-9]+.[0-9]+$")
119+
r.methodIsValid = validateMethod(r.method)
120+
r.uri = parseUri(r.request)
121+
r.getRequestData = getRequestData(request)
122+
collectgarbage()
123+
return r
123124
end

httpserver-servefunction.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ return function (connection, payload)
1515
end
1616

1717
local req = dofile("httpserver-request.lc")(payload)
18-
print(req.method .. ": " .. req.request)
1918

2019
local serveFunction = nil
2120
local methodIsAllowed = {GET=true, POST=true, PUT=true}
2221

2322
if user and req.methodIsValid and methodIsAllowed[req.method] and #(req.uri.file) <= 31 then
23+
print(req.method .. ": " .. req.request)
2424
local uri = req.uri
2525

2626
local fileExists = file.exists(uri.file)

tbconnection.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ return function(connectionArg, keepconnectionArg)
5151

5252

5353
local connectionThread = nil
54-
local onDisconnectionUserCallback = nil
54+
local onDisconnectionUserCallback = nil --called when connection gets dropped
55+
local onSentUserCallback = nil --called when functionArg is done sending (i.e.: returns in coroutine). Call is done via node.task.post, so callback executes in main thread instead of coroutine.
5556

5657
-- clean up closure context
5758
local function cleanup()
@@ -61,6 +62,7 @@ return function(connectionArg, keepconnectionArg)
6162
end
6263
connectionThread = nil
6364
onDisconnectionUserCallback = nil
65+
onSentUserCallback = nil
6466
collectgarbage()
6567
end
6668

@@ -138,7 +140,11 @@ return function(connectionArg, keepconnectionArg)
138140
bconn:flush()
139141
bconn = nil
140142
collectgarbage()
143+
if onSentUserCallback then
144+
node.task.post(node.task.MEDIUM_PRIORITY, onSentUserCallback)
145+
end
141146
cleanup()
147+
collectgarbage()
142148
end
143149
)
144150

@@ -148,12 +154,22 @@ return function(connectionArg, keepconnectionArg)
148154
print("Error: ", err)
149155
end
150156
end
151-
157+
152158
function threadedBufferedConnection:close()
153159
connection:close()
154160
cleanup()
155161
end
156162

163+
function threadedBufferedConnection:on(event, callback)
164+
if event == "disconnection" then
165+
onDisconnectionUserCallback = callback
166+
elseif event == "sent" then
167+
onSentUserCallback = callback
168+
else
169+
error("Error: unknown event: "..event)
170+
end
171+
end
172+
157173
local function onDisconnection(conn)
158174
if onDisconnectionUserCallback then
159175
onDisconnectionUserCallback(conn)

0 commit comments

Comments
 (0)