Software /
code /
prosody-modules
Comparison
mod_rest/mod_rest.lua @ 4066:07ae583bc565
mod_rest: Add support for form-encoded output
This roughtly matches the input capabilities when given an form-encoded
payload.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 20 Jul 2020 21:42:11 +0200 |
parent | 4037:991090cb5d18 |
child | 4242:6a91d217acc9 |
comparison
equal
deleted
inserted
replaced
4065:92152437ecfe | 4066:07ae583bc565 |
---|---|
109 }; | 109 }; |
110 | 110 |
111 local supported_outputs = { | 111 local supported_outputs = { |
112 "application/xmpp+xml", | 112 "application/xmpp+xml", |
113 "application/json", | 113 "application/json", |
114 "application/x-www-form-urlencoded", | |
114 }; | 115 }; |
115 | 116 |
116 if have_cbor then | 117 if have_cbor then |
117 table.insert(supported_inputs, "application/cbor"); | 118 table.insert(supported_inputs, "application/cbor"); |
118 table.insert(supported_outputs, "application/cbor"); | 119 table.insert(supported_outputs, "application/cbor"); |
119 end | 120 end |
120 | 121 |
122 -- Only { string : string } can be form-encoded, discard the rest | |
123 -- (jsonmap also discards anything unknown or unsupported) | |
124 local function flatten(t) | |
125 local form = {}; | |
126 for k, v in pairs(t) do | |
127 if type(v) == "string" then | |
128 form[k] = v; | |
129 elseif type(v) == "number" then | |
130 form[k] = tostring(v); | |
131 elseif v == true then | |
132 form[k] = ""; | |
133 end | |
134 end | |
135 return form; | |
136 end | |
137 | |
121 local function encode(type, s) | 138 local function encode(type, s) |
122 if type == "application/json" then | 139 if type == "application/json" then |
123 return json.encode(jsonmap.st2json(s)); | 140 return json.encode(jsonmap.st2json(s)); |
141 elseif type == "application/x-www-form-urlencoded" then | |
142 return http.formencode(flatten(jsonmap.st2json(s))); | |
124 elseif type == "application/cbor" then | 143 elseif type == "application/cbor" then |
125 return cbor.encode(jsonmap.st2json(s)); | 144 return cbor.encode(jsonmap.st2json(s)); |
126 elseif type == "text/plain" then | 145 elseif type == "text/plain" then |
127 return s:get_child_text("body") or ""; | 146 return s:get_child_text("body") or ""; |
128 end | 147 end |