48 lines
1.2 KiB
Lua
48 lines
1.2 KiB
Lua
local api = ...
|
|
local ecs = api.ecs
|
|
|
|
return {
|
|
name = "URI",
|
|
enabled = true,
|
|
current = {}, -- will hold the most recent (_top) URL object
|
|
|
|
init = function()
|
|
local urlListener
|
|
urlListener = ecs.processingSystem({
|
|
updatethread = true,
|
|
filter = ecs.requireAll('URI', ecs.rejectAll('URLResponse') ),
|
|
onAdd = function(self,obj)
|
|
api.ext.URI.to(obj.URI, nil, obj)
|
|
end
|
|
})
|
|
ecs.addSystem( api.world, urlListener )
|
|
end,
|
|
|
|
to = function(URI,refererer, obj)
|
|
obj = obj or {}
|
|
print("[i] loading URL: " .. URI.url)
|
|
obj.URL = api.url.parse(URI.url)
|
|
obj.URLResponse = {}
|
|
local protocol = "file"
|
|
foreach( api.protocol, function(name, p)
|
|
if obj.URL.protocol:match("^"..name) then
|
|
protocol = p
|
|
end
|
|
end)
|
|
if protocol then
|
|
if obj.URL.protocol == 'file' then
|
|
return obj.commit('onURI')
|
|
end
|
|
local status, data, headers = protocol.request(URI.url)
|
|
obj.URLResponse = {
|
|
ok = (status ~= nil and status >= 200 and status < 300),
|
|
status = status,
|
|
data = data,
|
|
headers = headers
|
|
}
|
|
api.ecs.add( api.world, obj )
|
|
obj.commit('onURI')
|
|
end
|
|
end
|
|
|
|
}
|