diff options
author | Steven Barth <steven@midlink.org> | 2009-05-23 17:21:36 +0000 |
---|---|---|
committer | Steven Barth <steven@midlink.org> | 2009-05-23 17:21:36 +0000 |
commit | 8c4f847ea5b95aaf0e716beaf736b4e2b67655ae (patch) | |
tree | 5b931064a2df45c3903b66b425f49b621e9c31df /libs/lucid-http/luasrc/lucid/http/handler/luci.lua | |
parent | 0ad58e38b42dca2f46bc492b7f889b5031a9c6a1 (diff) |
GSoC Commit #1: LuCId + HTTP-Server
Diffstat (limited to 'libs/lucid-http/luasrc/lucid/http/handler/luci.lua')
-rw-r--r-- | libs/lucid-http/luasrc/lucid/http/handler/luci.lua | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/libs/lucid-http/luasrc/lucid/http/handler/luci.lua b/libs/lucid-http/luasrc/lucid/http/handler/luci.lua new file mode 100644 index 0000000000..c54e39366a --- /dev/null +++ b/libs/lucid-http/luasrc/lucid/http/handler/luci.lua @@ -0,0 +1,96 @@ +--[[ +LuCId HTTP-Slave +(c) 2009 Steven Barth <steven@midlink.org> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +$Id$ +]]-- + +local dsp = require "luci.dispatcher" +local util = require "luci.util" +local http = require "luci.http" +local ltn12 = require "luci.ltn12" +local srv = require "luci.lucid.http.server" +local coroutine = require "coroutine" +local type = type + +module "luci.lucid.http.handler.luci" + +Luci = util.class(srv.Handler) + +function Luci.__init__(self, name, prefix) + srv.Handler.__init__(self, name) + self.prefix = prefix +end + +function Luci.handle_HEAD(self, ...) + local stat, head = self:handle_GET(...) + return stat, head +end + +function Luci.handle_POST(self, ...) + return self:handle_GET(...) +end + +function Luci.handle_GET(self, request, sourcein) + local r = http.Request( + request.env, + sourcein + ) + + local res, id, data1, data2 = true, 0, nil, nil + local headers = {} + local status = 200 + local active = true + + local x = coroutine.create(dsp.httpdispatch) + while not id or id < 3 do + res, id, data1, data2 = coroutine.resume(x, r, self.prefix) + + if not res then + status = 500 + headers["Content-Type"] = "text/plain" + return status, headers, ltn12.source.string(id) + end + + if id == 1 then + status = data1 + elseif id == 2 then + if not headers[data1] then + headers[data1] = data2 + elseif type(headers[data1]) ~= "table" then + headers[data1] = {headers[data1], data2} + else + headers[data1][#headers[data1]+1] = data2 + end + end + end + + if id == 6 then + while (coroutine.resume(x)) do end + return status, headers, srv.IOResource(data1, data2) + end + + local function iter() + local res, id, data = coroutine.resume(x) + if not res then + return nil, id + elseif not id or not active then + return true + elseif id == 5 then + active = false + while (coroutine.resume(x)) do end + return nil + elseif id == 4 then + return data + end + end + + return status, headers, iter +end + |