Software /
code /
prosody-modules
Comparison
mod_json_streams/mod_json_streams.lua @ 350:98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 02 Apr 2011 03:10:04 +0500 |
parent | 349:ee99eafdd168 |
comparison
equal
deleted
inserted
replaced
349:ee99eafdd168 | 350:98569ec25ac2 |
---|---|
2 -- XEP-0295: JSON Encodings for XMPP | 2 -- XEP-0295: JSON Encodings for XMPP |
3 -- | 3 -- |
4 | 4 |
5 module.host = "*" | 5 module.host = "*" |
6 | 6 |
7 local httpserver = require "net.httpserver"; | |
7 local filters = require "util.filters" | 8 local filters = require "util.filters" |
8 local json = require "util.json" | 9 local json = require "util.json" |
9 | 10 |
10 local json_escapes = { | 11 local json_escapes = { |
11 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f", | 12 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f", |
118 end | 119 end |
119 function module.unload() | 120 function module.unload() |
120 filters.remove_filter_hook(filter_hook); | 121 filters.remove_filter_hook(filter_hook); |
121 end | 122 end |
122 | 123 |
124 function encode(data) | |
125 if type(data) == "string" then | |
126 data = json.encode({ s = data }); | |
127 elseif type(data) == "table" and data.body then | |
128 data.body = json.encode({ s = data.body }); | |
129 data.headers["Content-Type"] = "application/json"; | |
130 end | |
131 return data; | |
132 end | |
133 function handle_request(method, body, request) | |
134 local mod_bosh = modulemanager.get_module("*", "bosh") | |
135 if mod_bosh then | |
136 if body and method == "POST" then | |
137 pcall(function() body = json.decode(body).s; end); | |
138 end | |
139 local _send = request.send; | |
140 function request:send(data) return _send(self, encode(data)); end | |
141 return encode(mod_bosh.handle_request(method, body, request)); | |
142 end | |
143 return "<html><body>mod_bosh not loaded</body></html>"; | |
144 end | |
123 | 145 |
146 local function setup() | |
147 local ports = module:get_option("jsonstreams_ports") or { 5280 }; | |
148 httpserver.new_from_config(ports, handle_request, { base = "jsonstreams" }); | |
149 end | |
150 if prosody.start_time then -- already started | |
151 setup(); | |
152 else | |
153 prosody.events.add_handler("server-started", setup); | |
154 end |