# HG changeset patch # User JC Brand # Date 1476708965 0 # Node ID 79432b859d218b458fcb4ef0c9ea388543267912 # Parent eb456fd639d258a8b8c6fd743dde4b1e5cb4f36c New module: mod_http_rest.lua diff -r eb456fd639d2 -r 79432b859d21 mod_http_rest/README.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_http_rest/README.markdown Mon Oct 17 12:56:05 2016 +0000 @@ -0,0 +1,29 @@ +--- +labels: +- 'Stage-Alpha' +summary: Send XMPP stanzas via REST/HTTP +... + +This module provides a [REST](https://en.wikipedia.org/wiki/Representational_state_transfer)ful +method for sending XMPP stanzas. + +This enables you to send stanzas by making HTTP requests to `http://${prosody-url}/rest`. + +**DANGER/ACHTUNG!: This module does NOT enforce any authentication or user-checking. +This means that by default stanzas can be sent *anyone* on behalf of *any* user.** + +You should enable [mod_http_authentication](https://modules.prosody.im/mod_http_authentication.html), +to require authentication for calls made to this module, or alternatively, you +could use a reverse proxy like Nginx. + +# To enable this module + +Add `"http_rest"` to `modules_enabled`, either globally or for a particular virtual +host. + +# How to test: + +You can use curl to make the HTTP request to Prosody, to test whether this +module is working properly: + + curl -k http://localhost:5280/rest -u username:password -H "Content-Type: text/xml" -d 'Hello World!' diff -r eb456fd639d2 -r 79432b859d21 mod_http_rest/mod_http_rest.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_http_rest/mod_http_rest.lua Mon Oct 17 12:56:05 2016 +0000 @@ -0,0 +1,39 @@ +module:depends"http" + +local jid_split = require "util.jid".split; +local jid_prep = require "util.jid".prep; +local stanza = require "util.stanza"; +local test_password = require "core.usermanager".test_password; +local b64_decode = require "util.encodings".base64.decode; +local formdecode = require "net.http".formdecode; +local xml = require"util.xml"; + +local function handle_post(event, path, authed_user) + local request = event.request; + local headers = request.headers; + local body_type = headers.content_type; + if body_type == "text/xml" and request.body then + local parsed, err = xml.parse(request.body); + if parsed then + module:log("debug", "Sending %s", parsed); + module:send(parsed); + return 201; + end + else + return 415; + end + return 422; +end + +module:provides("http", { + default_path = "/rest"; + route = { + ["POST"] = handle_post; + OPTIONS = function(e) + local headers = e.response.headers; + headers.allow = "POST"; + headers.accept = "test/xml"; + return 200; + end; + } +});