* libs/httpd: Introduced keep-alive and pipelining support
[project/luci.git] / libs / httpd / luasrc / httpd / 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 module("luci.httpd.module", package.seeall)
15 require("luci.util")
16 require("luci.http.protocol")
17 require("ltn12")
18
19
20
21 -- Server handler implementation
22 Handler = luci.util.class()
23
24 -- Constructor
25 function Handler.__init__(self)
26         self.filters = {}
27         self.handler = {}
28 end
29
30
31 -- Adds a filter to the filter chain
32 function Handler.addfilter(self, filter)
33         table.insert(self.filters, filter)
34 end
35
36
37 -- Creates a failure reply
38 function Handler.failure(self, code, message)
39         local response = Response(code, { ["Content-Type"] = "text/plain" })
40         local sourceout = ltn12.source.string(message)
41         
42         return response, sourceout 
43 end
44
45 -- Processes a request
46 function Handler.process(self, request, sourcein, sinkerr, ...)
47         local stat, response, sourceout
48
49         -- Process incoming filters
50         for i, f in ipairs(self.filters) do
51                 local i = f:get("input")
52                 
53                 if i then
54                         sourcein = ltn12.source.chain(sourcein, i) 
55                 end
56                 
57                 if f.request then
58                         f:request(request)
59                 end
60         end
61         
62         -- Detect request Method
63         local hname = "handle_" .. request.request_method
64         if self[hname] then
65                 -- Run the handler
66                 stat, response, sourceout = luci.util.copcall(
67                         self[hname], self, request, sourcein, sinkerr, ...
68                 )
69         
70                 -- Check for any errors
71                 if not stat then
72                         response, sourceout = self:failure(500, response)
73                 end
74         else
75                 response, sourceout = self:failure(405, luci.http.protocol.statusmsg[405])
76         end
77         
78         -- Check data
79         if not luci.util.instanceof(response, Response) then
80                 response, sourceout = self:failure(500, "Core error: Invalid module response!")
81         end
82         
83         -- Process outgoing filters
84         for i, f in ipairs(self.filters) do
85                 local o = f:get("output")
86                 
87                 if o then
88                         sourceout = ltn12.source.chain(sourceout, o) 
89                 end
90                 
91                 if f.response then
92                         f:response(response)
93                 end
94         end
95         
96         return response, sourceout
97 end
98
99
100
101 -- Server Filter implementation
102 Filter = luci.util.class()
103
104 function Filter.get(self, name)
105         return self[name] and function(...) return self[name](self, ...) end
106 end
107
108 -- Filters the incoming body stream
109 -- abstract function Filter.input(chunk)
110
111 -- Filters the outgoing body stream
112 -- abstract function Filter.output(chunk)
113
114 -- Filters the request object
115 -- abstract function Filter.request(request)
116
117 -- Filters the response object
118 -- abstract function Filter.response(response)
119
120
121
122 -- Handler Response 
123 Response = luci.util.class()
124
125 function Response.__init__(self, status, headers)
126         self.status = tonumber(status) or 200
127         self.headers = (type(headers) == "table") and headers or {}
128 end
129
130 function Response.addheader(self, key, value)
131         self.headers[key] = value
132 end
133
134 function Response.setstatus(self, status)
135         self.status = status
136 end