1 -- Copyright (C) 2011 - 2012 Kim Alvefur |
1 -- Copyright (C) 2011 - 2012 Kim Alvefur |
2 -- |
2 -- |
3 -- This file is MIT/X11 licensed. |
3 -- This file is MIT/X11 licensed. |
4 |
4 |
5 local http_request, formdecode, formencode = import("net.http", "request", "formdecode", "formencode"); |
5 local http = require "net.http"; |
|
6 local formdecode = http.formdecode; |
|
7 local formencode = http.formencode; |
6 local uuid = require "util.uuid".generate; |
8 local uuid = require "util.uuid".generate; |
7 local hmac_sha1 = require "util.hmac".sha1; |
9 local hmac_sha1 = require "util.hmac".sha1; |
8 local json_encode = require "util.json".encode; |
10 local json_encode = require "util.json".encode; |
9 local time = os.time; |
11 local time = os.time; |
10 local m_min, m_max = math.min, math.max; |
12 local m_min, m_max = math.min, math.max; |
11 local tostring = tostring; |
13 local tostring = tostring; |
12 |
|
13 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; |
14 local xmlns_pubsub = "http://jabber.org/protocol/pubsub"; |
14 local xmlns_pubsub_event = xmlns_pubsub .. "#event"; |
15 local xmlns_pubsub_event = xmlns_pubsub .. "#event"; |
15 local subs_by_topic = module:shared"subscriptions"; |
16 local subs_by_topic = module:shared"subscriptions"; |
16 |
17 |
17 local max_lease, min_lease, default_lease = 86400, 600, 3600; |
18 local max_lease, min_lease, default_lease = 86400, 600, 3600; |
88 ["hub.verify_token"] = verify_token, |
89 ["hub.verify_token"] = verify_token, |
89 } |
90 } |
90 module:log("debug", require"util.serialization".serialize(verify_modes)); |
91 module:log("debug", require"util.serialization".serialize(verify_modes)); |
91 if verify_modes["async"] then |
92 if verify_modes["async"] then |
92 module:log("debug", "Sending async verification request to %s for %s", tostring(callback_url), tostring(subscription)); |
93 module:log("debug", "Sending async verification request to %s for %s", tostring(callback_url), tostring(subscription)); |
93 http_request(callback_url, nil, function(body, code) |
94 http.request(callback_url, nil, function(body, code) |
94 if body == challenge and code > 199 and code < 300 then |
95 if body == challenge and code > 199 and code < 300 then |
95 if not subscription.want_state then |
96 if not subscription.want_state then |
96 module:log("warn", "Verification of already verified request, probably"); |
97 module:log("warn", "Verification of already verified request, probably"); |
97 return; |
98 return; |
98 end |
99 end |
106 subs_by_topic[topic][callback] = subscription; |
107 subs_by_topic[topic][callback] = subscription; |
107 end |
108 end |
108 end) |
109 end) |
109 return 202; |
110 return 202; |
110 elseif verify_modes["sync"] then |
111 elseif verify_modes["sync"] then |
111 http_request(callback_url, nil, function(body, code) |
112 http.request(callback_url, nil, function(body, code) |
112 if body == challenge and code > 199 and code < 300 then |
113 if body == challenge and code > 199 and code < 300 then |
113 if not subscription.want_state then |
114 if not subscription.want_state then |
114 module:log("warn", "Verification of already verified request, probably"); |
115 module:log("warn", "Verification of already verified request, probably"); |
115 return; |
116 return; |
116 end |
117 end |
158 ["hub.topic"] = topic, |
159 ["hub.topic"] = topic, |
159 ["hub.challenge"] = challenge, |
160 ["hub.challenge"] = challenge, |
160 ["hub.lease_seconds"] = subscription.lease_seconds, |
161 ["hub.lease_seconds"] = subscription.lease_seconds, |
161 ["hub.verify_token"] = subscription.verify_token, |
162 ["hub.verify_token"] = subscription.verify_token, |
162 } |
163 } |
163 http_request(callback_url, nil, function(body, code) |
164 http.request(callback_url, nil, function(body, code) |
164 if body == challenge and code > 199 and code < 300 then |
165 if body == challenge and code > 199 and code < 300 then |
165 subscription.expires = now + subscription.lease_seconds; |
166 subscription.expires = now + subscription.lease_seconds; |
166 end |
167 end |
167 end); |
168 end); |
168 else |
169 else |
198 ["Content-Type"] = "application/xml", |
199 ["Content-Type"] = "application/xml", |
199 }; |
200 }; |
200 if subscription.secret then |
201 if subscription.secret then |
201 headers["X-Hub-Signature"] = "sha1="..hmac_sha1(subscription.secret, body, true); |
202 headers["X-Hub-Signature"] = "sha1="..hmac_sha1(subscription.secret, body, true); |
202 end |
203 end |
203 http_request(subscription.callback, { method = "POST", body = body, headers = headers }, function(body, code) |
204 http.request(subscription.callback, { method = "POST", body = body, headers = headers }, function(body, code) |
204 if code >= 200 and code <= 299 then |
205 if code >= 200 and code <= 299 then |
205 module:log("debug", "Delivered"); |
206 module:log("debug", "Delivered"); |
206 else |
207 else |
207 module:log("warn", "Got status code %d on delivery to %s", tonumber(code) or -1, tostring(subscription.callback)); |
208 module:log("warn", "Got status code %d on delivery to %s", tonumber(code) or -1, tostring(subscription.callback)); |
208 -- TODO Retry |
209 -- TODO Retry |