Mercurial > prosody-modules
comparison mod_pubsub_post/mod_pubsub_post.lua @ 4552:c87181a98f29
mod_pubsub_post: Add support for urlencoded form-data
This would allow a subset of JSON payloads to be passed easily with
simple `curl -d foo=bar -d hello=there` calls.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 26 Apr 2021 02:49:25 +0200 |
| parents | 08b71d02c6dc |
| children |
comparison
equal
deleted
inserted
replaced
| 4551:b92147edd172 | 4552:c87181a98f29 |
|---|---|
| 1 module:depends("http"); | 1 module:depends("http"); |
| 2 | 2 |
| 3 local st = require "util.stanza"; | 3 local st = require "util.stanza"; |
| 4 local json = require "util.json"; | 4 local json = require "util.json"; |
| 5 local xml = require "util.xml"; | 5 local xml = require "util.xml"; |
| 6 local http = require "net.http"; | |
| 6 local uuid_generate = require "util.uuid".generate; | 7 local uuid_generate = require "util.uuid".generate; |
| 7 local timestamp_generate = require "util.datetime".datetime; | 8 local timestamp_generate = require "util.datetime".datetime; |
| 8 local hashes = require "util.hashes"; | 9 local hashes = require "util.hashes"; |
| 9 local from_hex = require "util.hex".from; | 10 local from_hex = require "util.hex".from; |
| 10 local hmacs = { | 11 local hmacs = { |
| 100 else | 101 else |
| 101 return publish_payload(node, actor, "current", xmlpayload); | 102 return publish_payload(node, actor, "current", xmlpayload); |
| 102 end | 103 end |
| 103 end | 104 end |
| 104 | 105 |
| 106 local function handle_urlencoded(node, actor, data) | |
| 107 local parsed = http.formdecode(data); | |
| 108 if type(parsed) ~= "table" then return {status_code = 400; body = "invalid payload"}; end | |
| 109 for i = 1, #parsed do parsed[i] = nil; end | |
| 110 | |
| 111 local payload = wrap(node, parsed, json.encode(parsed)); | |
| 112 local item_id = "current"; | |
| 113 if payload.attr["http://jabber.org/protocol/pubsub\1id"] then | |
| 114 item_id = payload.attr["http://jabber.org/protocol/pubsub\1id"]; | |
| 115 payload.attr["http://jabber.org/protocol/pubsub\1id"] = nil; | |
| 116 elseif type(parsed.id) == "string" then | |
| 117 item_id = parsed.id; | |
| 118 end | |
| 119 return publish_payload(node, actor, item_id, payload); | |
| 120 end | |
| 121 | |
| 105 local actor_source = module:get_option_string("pubsub_post_actor"); -- COMPAT | 122 local actor_source = module:get_option_string("pubsub_post_actor"); -- COMPAT |
| 106 local default_secret = module:get_option_string("pubsub_post_default_secret"); | 123 local default_secret = module:get_option_string("pubsub_post_default_secret"); |
| 107 local actor_secrets = module:get_option("pubsub_post_secrets"); | 124 local actor_secrets = module:get_option("pubsub_post_secrets"); |
| 108 local actors = module:get_option("pubsub_post_actors"); | 125 local actors = module:get_option("pubsub_post_actors"); |
| 109 local default_actor = module:get_option_string("pubsub_post_default_actor"); | 126 local default_actor = module:get_option_string("pubsub_post_default_actor"); |
| 140 | 157 |
| 141 if content_type == "application/xml" or content_type:sub(-4) == "+xml" then | 158 if content_type == "application/xml" or content_type:sub(-4) == "+xml" then |
| 142 return handle_xml(path, actor, request.body); | 159 return handle_xml(path, actor, request.body); |
| 143 elseif content_type == "application/json" or content_type:sub(-5) == "+json" then | 160 elseif content_type == "application/json" or content_type:sub(-5) == "+json" then |
| 144 return handle_json(path, actor, request.body); | 161 return handle_json(path, actor, request.body); |
| 162 elseif content_type == "application/x-www-form-urlencoded" then | |
| 163 return handle_urlencoded(path, actor, request.body); | |
| 145 end | 164 end |
| 146 | 165 |
| 147 module:log("debug", "Unsupported content-type: %q", content_type); | 166 module:log("debug", "Unsupported content-type: %q", content_type); |
| 148 return 415; | 167 return 415; |
| 149 end | 168 end |
