libs/lucid-http: use SERVER_ADDR as fallback
[project/luci.git] / libs / lucittpd / luasrc / ttpd / module.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local pcall, ipairs, tonumber, type, next = pcall, ipairs, tonumber, type, next
16 local util = require "luci.util"
17 local http = require "luci.http.protocol"
18 local ltn12 = require "luci.ltn12"
19 local table = require "table"
20
21 module "luci.ttpd.module"
22
23
24 -- Server handler implementation
25 Handler = util.class()
26
27 -- Constructor
28 function Handler.__init__(self)
29         self.handler   = {}
30         self.filters   = {}
31         self.modifiers = {}
32 end
33
34 -- Add a filter
35 function Handler.setfilter(self, filter, key)
36         self.filters[(key) or (#self.filters+1)] = filter
37 end
38
39 -- Add a modifier
40 function Handler.setmodifier(self, modifier, key)
41         self.modifiers[(pos) or (#self.modifiers+1)] = modifier
42 end
43
44 -- Creates a failure reply
45 function Handler.failure(self, code, message)
46         local response = Response(code, { ["Content-Type"] = "text/plain" })
47         local sourceout = ltn12.source.string(message)
48         
49         return response, sourceout 
50 end
51
52 -- Processes a request
53 function Handler.process(self, request, sourcein, sinkerr)
54         local stat, response, sourceout
55
56         -- Detect request Method
57         local hname = "handle_" .. request.request_method
58         if self[hname] then
59                 local t = {
60                         processor = self[hname],
61                         handler = self,
62                         request = request,
63                         sourcein = sourcein,
64                         sinkerr = sinkerr
65                 }
66
67                 if next(self.modifiers) then
68                         for _, mod in util.kspairs(self.modifiers) do
69                                 mod(t)
70                         end
71                 end
72
73                 -- Run the handler
74                 stat, response, sourceout = pcall(
75                         t.processor, t.handler, t.request, t.sourcein, t.sinkerr
76                 )
77
78                 -- Check for any errors
79                 if not stat then
80                         response, sourceout = self:failure(500, response)
81                 elseif next(self.filters) then
82                         local t = {
83                                 response = response,
84                                 sourceout = sourceout,
85                                 sinkerr = t.sinkerr
86                         }
87
88                         for _, filter in util.kspairs(self.filters) do
89                                 filter(t)
90                         end
91
92                         response = t.response
93                         sourceout = t.sourceout
94                 end
95         else
96                 response, sourceout = self:failure(405, http.protocol.statusmsg[405])
97         end
98
99         -- Check data
100         if not util.instanceof(response, Response) then
101                 response, sourceout = self:failure(500, "Core error: Invalid module response!")
102         end
103
104         return response, sourceout
105 end
106
107 -- Handler Response 
108 Response = util.class()
109
110 function Response.__init__(self, status, headers)
111         self.status = tonumber(status) or 200
112         self.headers = (type(headers) == "table") and headers or {}
113 end
114
115 function Response.addheader(self, key, value)
116         self.headers[key] = value
117 end
118
119 function Response.setstatus(self, status)
120         self.status = status
121 end