From 8c4f847ea5b95aaf0e716beaf736b4e2b67655ae Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Sat, 23 May 2009 17:21:36 +0000 Subject: GSoC Commit #1: LuCId + HTTP-Server --- libs/lucid-http/luasrc/lucid/http/handler/luci.lua | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 libs/lucid-http/luasrc/lucid/http/handler/luci.lua (limited to 'libs/lucid-http/luasrc/lucid/http/handler/luci.lua') 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 + +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 + -- cgit v1.2.3