Software /
code /
prosody
Comparison
plugins/mod_http.lua @ 4635:ea5215bd2783
mod_http: Provide HTTP service.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 21 Apr 2012 17:38:48 +0500 |
child | 4636:41983ec223f0 |
comparison
equal
deleted
inserted
replaced
4634:8e582afc214d | 4635:ea5215bd2783 |
---|---|
1 -- Prosody IM | |
2 -- Copyright (C) 2008-2010 Matthew Wild | |
3 -- Copyright (C) 2008-2010 Waqas Hussain | |
4 -- | |
5 -- This project is MIT/X11 licensed. Please see the | |
6 -- COPYING file in the source package for more information. | |
7 -- | |
8 | |
9 module:set_global(); | |
10 | |
11 --local sessions = module:shared("sessions"); | |
12 | |
13 --[[function listener.associate_session(conn, session) | |
14 sessions[conn] = session; | |
15 end]] | |
16 | |
17 local handlers; | |
18 | |
19 function build_handlers() | |
20 handlers = {}; | |
21 for _, item in ipairs(module:get_host_items("http-handler")) do | |
22 local previous = handlers[item.path]; | |
23 if not previous and item.path then | |
24 handlers[item.path] = item; | |
25 end | |
26 end | |
27 end | |
28 function clear_handlers() | |
29 handlers = nil; | |
30 end | |
31 module:handle_items("http-handler", clear_handlers, clear_handlers, false); | |
32 | |
33 function http_handler(event) | |
34 local request, response = event.request, event.response; | |
35 | |
36 if not handlers then build_handlers(); end | |
37 local item = handlers[request.path:match("[^?]*")]; | |
38 local handler = item and item.handler; | |
39 if handler then | |
40 handler(request, response); | |
41 return true; | |
42 end | |
43 end | |
44 | |
45 local server = require "net.http.server"; | |
46 local listener = server.listener; | |
47 server.add_handler("*", http_handler); | |
48 function module.unload() | |
49 server.remove_handler("*", http_handler); | |
50 end | |
51 --require "net.http.server".listen_on(8080); | |
52 | |
53 module:add_item("net-provider", { | |
54 name = "http"; | |
55 listener = listener; | |
56 default_port = 5280; | |
57 multiplex = { | |
58 pattern = "^[A-Z]"; | |
59 }; | |
60 }); | |
61 | |
62 module:add_item("net-provider", { | |
63 name = "https"; | |
64 listener = listener; | |
65 encryption = "ssl"; | |
66 multiplex = { | |
67 pattern = "^[A-Z]"; | |
68 }; | |
69 }); |