summaryrefslogtreecommitdiffhomepage
path: root/libs/lucittpd/luasrc/ttpd/module.lua
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2008-11-30 13:19:45 +0000
committerSteven Barth <steven@midlink.org>2008-11-30 13:19:45 +0000
commitb33943a6e8596c1ddfc1b771a995d3cf21e81cd6 (patch)
tree6f67cdea044e708a599a06712491b5c60db6f954 /libs/lucittpd/luasrc/ttpd/module.lua
parenta7e7c31f8c659b55c1adb0863a8f2f66d3452d2b (diff)
Merge LuCIttpd
Diffstat (limited to 'libs/lucittpd/luasrc/ttpd/module.lua')
-rw-r--r--libs/lucittpd/luasrc/ttpd/module.lua121
1 files changed, 121 insertions, 0 deletions
diff --git a/libs/lucittpd/luasrc/ttpd/module.lua b/libs/lucittpd/luasrc/ttpd/module.lua
new file mode 100644
index 0000000000..1a7c574733
--- /dev/null
+++ b/libs/lucittpd/luasrc/ttpd/module.lua
@@ -0,0 +1,121 @@
+--[[
+LuCI - Lua Configuration Interface
+
+Copyright 2008 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 pcall, ipairs, tonumber, type, next = pcall, ipairs, tonumber, type, next
+local util = require "luci.util"
+local http = require "luci.http.protocol"
+local ltn12 = require "luci.ltn12"
+local table = require "table"
+
+module "luci.ttpd.module"
+
+
+-- Server handler implementation
+Handler = util.class()
+
+-- Constructor
+function Handler.__init__(self)
+ self.handler = {}
+ self.filters = {}
+ self.modifiers = {}
+end
+
+-- Add a filter
+function Handler.setfilter(self, filter, key)
+ self.filters[(key) or (#self.filters+1)] = filter
+end
+
+-- Add a modifier
+function Handler.setmodifier(self, modifier, key)
+ self.modifiers[(pos) or (#self.modifiers+1)] = modifier
+end
+
+-- Creates a failure reply
+function Handler.failure(self, code, message)
+ local response = Response(code, { ["Content-Type"] = "text/plain" })
+ local sourceout = ltn12.source.string(message)
+
+ return response, sourceout
+end
+
+-- Processes a request
+function Handler.process(self, request, sourcein, sinkerr)
+ local stat, response, sourceout
+
+ -- Detect request Method
+ local hname = "handle_" .. request.request_method
+ if self[hname] then
+ local t = {
+ processor = self[hname],
+ handler = self,
+ request = request,
+ sourcein = sourcein,
+ sinkerr = sinkerr
+ }
+
+ if next(self.modifiers) then
+ for _, mod in util.kspairs(self.modifiers) do
+ mod(t)
+ end
+ end
+
+ -- Run the handler
+ stat, response, sourceout = pcall(
+ t.processor, t.handler, t.request, t.sourcein, t.sinkerr
+ )
+
+ -- Check for any errors
+ if not stat then
+ response, sourceout = self:failure(500, response)
+ elseif next(self.filters) then
+ local t = {
+ response = response,
+ sourceout = sourceout,
+ sinkerr = t.sinkerr
+ }
+
+ for _, filter in util.kspairs(self.filters) do
+ filter(t)
+ end
+
+ response = t.response
+ sourceout = t.sourceout
+ end
+ else
+ response, sourceout = self:failure(405, http.protocol.statusmsg[405])
+ end
+
+ -- Check data
+ if not util.instanceof(response, Response) then
+ response, sourceout = self:failure(500, "Core error: Invalid module response!")
+ end
+
+ return response, sourceout
+end
+
+-- Handler Response
+Response = util.class()
+
+function Response.__init__(self, status, headers)
+ self.status = tonumber(status) or 200
+ self.headers = (type(headers) == "table") and headers or {}
+end
+
+function Response.addheader(self, key, value)
+ self.headers[key] = value
+end
+
+function Response.setstatus(self, status)
+ self.status = status
+end \ No newline at end of file