Software /
code /
prosody
Annotate
plugins/mod_http.lua @ 4649:e07ce18c503e
mod_motd: Send only to resource coming online, not the user's bare JID (fixes #282)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 21 Apr 2012 21:16:53 +0100 |
parent | 4636:41983ec223f0 |
child | 4664:7438b3c68576 |
rev | line source |
---|---|
4635
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 -- Prosody IM |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 -- |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 -- |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 module:set_global(); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 --local sessions = module:shared("sessions"); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 --[[function listener.associate_session(conn, session) |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 sessions[conn] = session; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 end]] |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 |
4636
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
17 local NULL = {}; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
18 local handlers = {}; |
4635
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 |
4636
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
20 function build_handlers(host) |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
21 if not hosts[host] then return; end |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
22 local h = {}; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
23 handlers[host] = h; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
24 |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
25 for mod_name, module in pairs(modulemanager.get_modules(host)) do |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
26 module = module.module; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
27 if module.items then |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
28 for _, item in ipairs(module.items["http-handler"] or NULL) do |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
29 local previous = handlers[item.path]; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
30 if not previous and item.path then |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
31 h[item.path] = item; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
32 end |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
33 end |
4635
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 end |
4636
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
36 |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
37 return h; |
4635
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 end |
4636
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
39 function clear_handlers(event) |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
40 handlers[event.source.host] = nil; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
41 end |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
42 function get_handler(host, path) |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
43 local h = handlers[host] or build_handlers(host); |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
44 if h then |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
45 local item = h[path]; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
46 return item and item.handler; |
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
47 end |
4635
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 module:handle_items("http-handler", clear_handlers, clear_handlers, false); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 function http_handler(event) |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 local request, response = event.request, event.response; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 |
4636
41983ec223f0
mod_http: Include handlers of non-global modules.
Waqas Hussain <waqas20@gmail.com>
parents:
4635
diff
changeset
|
54 local handler = get_handler(request.headers.host:match("[^:]*"):lower(), request.path:match("[^?]*")); |
4635
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 if handler then |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 handler(request, response); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 return true; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 local server = require "net.http.server"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 local listener = server.listener; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 server.add_handler("*", http_handler); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 function module.unload() |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 server.remove_handler("*", http_handler); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 --require "net.http.server".listen_on(8080); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 module:add_item("net-provider", { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 name = "http"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 listener = listener; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 default_port = 5280; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 multiplex = { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 pattern = "^[A-Z]"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 }; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 }); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 module:add_item("net-provider", { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 name = "https"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 listener = listener; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 encryption = "ssl"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 multiplex = { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 pattern = "^[A-Z]"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 }; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 }); |