Skip to content

Commit 20abe16

Browse files
committed
Allow callbacks to be callable tables also
1 parent b69cf36 commit 20abe16

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

http/server.lua

+7-19
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,6 @@ local function is_callable(obj)
3535
return false
3636
end
3737

38-
local function is_callable(obj)
39-
local t_obj = type(obj)
40-
if t_obj == 'function' then
41-
return true
42-
end
43-
if t_obj == 'table' then
44-
local mt = getmetatable(obj)
45-
return (type(mt) == 'table' and type(mt.__call) == 'function')
46-
end
47-
return false
48-
end
49-
5038
local function uri_escape(str)
5139
local res = {}
5240
if type(str) == 'table' then
@@ -726,15 +714,15 @@ local function match_route(self, method, route)
726714
end
727715

728716
local function set_helper(self, name, sub)
729-
if sub == nil or type(sub) == 'function' then
717+
if sub == nil or is_callable(sub) then
730718
self.helpers[ name ] = sub
731719
return self
732720
end
733721
errorf("Wrong type for helper function: %s", type(sub))
734722
end
735723

736724
local function set_hook(self, name, sub)
737-
if sub == nil or type(sub) == 'function' then
725+
if sub == nil or is_callable(sub) then
738726
self.hooks[ name ] = sub
739727
return self
740728
end
@@ -778,7 +766,7 @@ local function ctx_action(tx)
778766
local action = tx.endpoint.action
779767
if tx.httpd.options.cache_controllers then
780768
if tx.httpd.cache[ ctx ] ~= nil then
781-
if type(tx.httpd.cache[ ctx ][ action ]) ~= 'function' then
769+
if is_callable(tx.httpd.cache[ ctx ][ action ]) then
782770
errorf("Controller '%s' doesn't contain function '%s'",
783771
ctx, action)
784772
end
@@ -807,7 +795,7 @@ local function ctx_action(tx)
807795
errorf("require '%s' didn't return table", ctx)
808796
end
809797

810-
if type(mod[ action ]) ~= 'function' then
798+
if is_callable(mod[ action ]) then
811799
errorf("Controller '%s' doesn't contain function '%s'", ctx, action)
812800
end
813801

@@ -849,8 +837,8 @@ local function add_route(self, opts, sub)
849837

850838
sub = ctx_action
851839

852-
elseif type(sub) ~= 'function' then
853-
errorf("wrong argument: expected function, but received %s",
840+
elseif not is_callable(sub) then
841+
errorf("wrong argument: expected callable, but received %s",
854842
type(sub))
855843
end
856844

@@ -1081,7 +1069,7 @@ local function httpd_http11_handler(session)
10811069
if type(body) == 'string' then
10821070
-- Plain string
10831071
hdrs['content-length'] = #body
1084-
elseif type(body) == 'function' then
1072+
elseif is_callable(body) then
10851073
-- Generating function
10861074
gen = body
10871075
hdrs['transfer-encoding'] = 'chunked'

0 commit comments

Comments
 (0)