Mercurial > prosody-modules
comparison mod_xml_status/mod_xml_status.lua @ 526:0529658efd1e
mod_xml_status: last code cleanup and optimization.
| author | Marco Cirillo <maranda@lightwitch.org> |
|---|---|
| date | Sat, 07 Jan 2012 00:29:06 +0000 |
| parents | a9cd75cc9563 |
| children | caf28c2c56a1 |
comparison
equal
deleted
inserted
replaced
| 525:a9cd75cc9563 | 526:0529658efd1e |
|---|---|
| 39 } | 39 } |
| 40 | 40 |
| 41 local function forge_response() | 41 local function forge_response() |
| 42 local hosts_s = {}; local components = {}; local stats = {}; local hosts_stats = {}; local comps_stats = {} | 42 local hosts_s = {}; local components = {}; local stats = {}; local hosts_stats = {}; local comps_stats = {} |
| 43 | 43 |
| 44 -- add headers | 44 local function t_builder(t,r) for _, bstring in ipairs(t) do r[#r+1] = bstring end end |
| 45 local result = {}; result[#result+1] = response_table.header; result[#result+1] = response_table.doc_header | 45 |
| 46 | 46 if show_hosts then t_builder(show_hosts, hosts_s) end |
| 47 if show_hosts then for _, name in ipairs(show_hosts) do hosts_s[#hosts_s+1] = name end end | 47 if show_comps then t_builder(show_comps, components) end |
| 48 if show_comps then for _, name in ipairs(show_comps) do components[#components+1] = name end end | |
| 49 | 48 |
| 50 -- build stanza stats if there | 49 -- build stanza stats if there |
| 51 if prosody.stanza_counter then | 50 if prosody.stanza_counter then |
| 52 stats[1] = response_table.stanzas.elem_header | 51 stats[1] = response_table.stanzas.elem_header |
| 53 stats[2] = response_table.stanzas.incoming:format(prosody.stanza_counter.iq["incoming"], | 52 stats[2] = response_table.stanzas.incoming:format(prosody.stanza_counter.iq["incoming"], |
| 78 hosts[name] and hosts[name].modules.component == nil and "online" or "offline") | 77 hosts[name] and hosts[name].modules.component == nil and "online" or "offline") |
| 79 end | 78 end |
| 80 comps_stats[#comps_stats+1] = response_table.comps.elem_closure | 79 comps_stats[#comps_stats+1] = response_table.comps.elem_closure |
| 81 end | 80 end |
| 82 | 81 |
| 83 -- finish building xml stats document | 82 -- build xml document |
| 84 for _, bstring in ipairs(stats) do result[#result+1] = bstring end | 83 local result = {} |
| 85 for _, bstring in ipairs(hosts_stats) do result[#result+1] = bstring end | 84 result[#result+1] = response_table.header; result[#result+1] = response_table.doc_header -- start |
| 86 for _, bstring in ipairs(comps_stats) do result[#result+1] = bstring end | 85 t_builder(stats, result); t_builder(hosts_stats, result); t_builder(comps_stats, result) |
| 87 result[#result+1] = response_table.doc_closure | 86 result[#result+1] = response_table.doc_closure -- end |
| 88 | 87 |
| 89 return table.concat(result, "\n") | 88 return table.concat(result, "\n") |
| 90 end | 89 end |
| 91 | 90 |
| 92 -- http handlers | 91 -- http handlers |
| 93 | 92 |
| 99 | 98 |
| 100 if h then response.headers = h; end | 99 if h then response.headers = h; end |
| 101 return response | 100 return response |
| 102 end | 101 end |
| 103 | 102 |
| 104 local function request(method, body, request) | 103 local function request(method, body, request) |
| 104 if not prosody.stanza_counter then | |
| 105 local err500 = r_err:format("500", "Internal server error") | |
| 106 return response(500, err500) end | |
| 105 if method == "GET" then | 107 if method == "GET" then |
| 106 return response(200, forge_response()) | 108 return response(200, forge_response()) |
| 107 else | 109 else |
| 108 local err405 = r_err:format("405", "Only GET is supported") | 110 local err405 = r_err:format("405", "Only GET is supported") |
| 109 return response(405, err405, {["Allow"] = "GET"}) | 111 return response(405, err405, {["Allow"] = "GET"}) |
