Software /
code /
prosody
Annotate
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 |
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 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local handlers; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 function build_handlers() |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 handlers = {}; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 for _, item in ipairs(module:get_host_items("http-handler")) do |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local previous = handlers[item.path]; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 if not previous and item.path then |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 handlers[item.path] = item; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 function clear_handlers() |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 handlers = nil; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 module:handle_items("http-handler", clear_handlers, clear_handlers, false); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 function http_handler(event) |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local request, response = event.request, event.response; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 if not handlers then build_handlers(); end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 local item = handlers[request.path:match("[^?]*")]; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 local handler = item and item.handler; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 if handler then |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 handler(request, response); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 return true; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 local server = require "net.http.server"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 local listener = server.listener; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 server.add_handler("*", http_handler); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 function module.unload() |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 server.remove_handler("*", http_handler); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 end |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 --require "net.http.server".listen_on(8080); |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 module:add_item("net-provider", { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 name = "http"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 listener = listener; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 default_port = 5280; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 multiplex = { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 pattern = "^[A-Z]"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 }; |
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 |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 module:add_item("net-provider", { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 name = "https"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 listener = listener; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 encryption = "ssl"; |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 multiplex = { |
ea5215bd2783
mod_http: Provide HTTP service.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 pattern = "^[A-Z]"; |
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 }); |