Mercurial > prosody-modules
annotate mod_json_streams/mod_json_streams.lua @ 1038:edb06824a5a4
mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 01 Jun 2013 23:21:48 +0100 |
parents | 98569ec25ac2 |
children |
rev | line source |
---|---|
349
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 -- |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 -- XEP-0295: JSON Encodings for XMPP |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 -- |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 module.host = "*" |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 |
350
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
7 local httpserver = require "net.httpserver"; |
349
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local filters = require "util.filters" |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 local json = require "util.json" |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local json_escapes = { |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 ["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f", |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 ["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t"}; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local s_char = string.char; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 for i=0,31 do |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 local ch = s_char(i); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 if not json_escapes[ch] then json_escapes[ch] = ("\\u%.4X"):format(i); end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 local state_out = 0; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local state_key_before = 1; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 local state_key_in = 2; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local state_key_escape = 3; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local state_key_after = 4; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 local state_val_before = 5; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 local state_val_in = 6; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local state_val_escape = 7; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 local state_val_after = 8; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local whitespace = { [" "] = true, ["\n"] = true, ["\r"] = true, ["\t"] = true }; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 function json_decoder() |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 local state = state_out; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local quote; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local output = ""; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 local buffer = ""; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 return function(input) |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 for ch in input:gmatch(".") do |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 module:log("debug", "%s | %d", ch, state) |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local final = false; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 if state == state_out then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 if whitespace[ch] then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 elseif ch ~= "{" then return nil, "{ expected"; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 else state = state_key_before end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 elseif state == state_key_before then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 if whitespace[ch] then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 elseif ch ~= "'" and ch ~= "\"" then return nil, "\" expected"; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 else quote = ch; state = state_key_in; end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 elseif state == state_key_in then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 if ch == quote then state = state_key_after; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 elseif ch ~= "s" then return nil, "invalid key, 's' expected"; -- only s as key allowed |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 else end -- ignore key |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 elseif state == state_key_after then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 if whitespace[ch] then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 elseif ch ~= ":" then return nil, ": expected"; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 else state = state_val_before; end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 elseif state == state_val_before then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 if whitespace[ch] then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 elseif ch ~= "'" and ch ~= "\"" then return nil, "\" expected"; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 else quote = ch; state = state_val_in; end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 elseif state == state_val_in then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 if ch == quote then state = state_val_after; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 elseif ch == "\\" then state = state_val_escape; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 else end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 elseif state == state_val_after then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 if whitespace[ch] then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 elseif ch ~= "}" then return nil, "} expected"; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 else state = state_out; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 final = true; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 elseif state == state_val_escape then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 state = state_val_in; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 else |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 module:log("error", "Unhandled state: "..state); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 return nil, "Unhandled state in parser" |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 buffer = buffer..ch; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 if final then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 module:log("debug", "%s", buffer) |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 local tmp; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 pcall(function() tmp = json.decode(buffer); end); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 if not tmp then return nil, "Invalid JSON"; end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 output, buffer = output..tmp.s, ""; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 local _ = output; output = ""; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 return _; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 end; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 function filter_hook(session) |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 local determined = false; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 local is_json = false; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 local function in_filter(t) |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 if not determined then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 is_json = (t:sub(1,1) == "{") and json_decoder(); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 determined = true; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 if is_json then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 local s, err = is_json(t); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 if not err then return s; end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 session:close("not-well-formed"); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 return; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 return t; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 local function out_filter(t) |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 if is_json then |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 return '{"s":"' .. t:gsub(".", json_escapes) .. '"}'; -- encode |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 return t; |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 filters.add_filter(session, "bytes/in", in_filter, 100); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 filters.add_filter(session, "bytes/out", out_filter, 100); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 function module.load() |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 filters.add_filter_hook(filter_hook); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 function module.unload() |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 filters.remove_filter_hook(filter_hook); |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 end |
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 |
350
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
124 function encode(data) |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
125 if type(data) == "string" then |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
126 data = json.encode({ s = data }); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
127 elseif type(data) == "table" and data.body then |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
128 data.body = json.encode({ s = data.body }); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
129 data.headers["Content-Type"] = "application/json"; |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
130 end |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
131 return data; |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
132 end |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
133 function handle_request(method, body, request) |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
134 local mod_bosh = modulemanager.get_module("*", "bosh") |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
135 if mod_bosh then |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
136 if body and method == "POST" then |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
137 pcall(function() body = json.decode(body).s; end); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
138 end |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
139 local _send = request.send; |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
140 function request:send(data) return _send(self, encode(data)); end |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
141 return encode(mod_bosh.handle_request(method, body, request)); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
142 end |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
143 return "<html><body>mod_bosh not loaded</body></html>"; |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
144 end |
349
ee99eafdd168
mod_json_streams: An implementation of XEP-0295: JSON Encodings for XMPP.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 |
350
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
146 local function setup() |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
147 local ports = module:get_option("jsonstreams_ports") or { 5280 }; |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
148 httpserver.new_from_config(ports, handle_request, { base = "jsonstreams" }); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
149 end |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
150 if prosody.start_time then -- already started |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
151 setup(); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
152 else |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
153 prosody.events.add_handler("server-started", setup); |
98569ec25ac2
mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams").
Waqas Hussain <waqas20@gmail.com>
parents:
349
diff
changeset
|
154 end |