Mercurial > prosody-modules
comparison mod_pastebin/mod_pastebin.lua @ 654:7e444de959bb
mod_pastebin: Update to the new HTTP API
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sun, 29 Apr 2012 21:07:04 +0200 |
| parents | 9c2eea631811 |
| children | 4e43becc3bbe |
comparison
equal
deleted
inserted
replaced
| 653:c08b0e4b7b38 | 654:7e444de959bb |
|---|---|
| 1 | 1 |
| 2 local st = require "util.stanza"; | 2 local st = require "util.stanza"; |
| 3 local httpserver = require "net.httpserver"; | 3 module:depends("http"); |
| 4 local uuid_new = require "util.uuid".generate; | 4 local uuid_new = require "util.uuid".generate; |
| 5 local os_time = os.time; | 5 local os_time = os.time; |
| 6 local t_insert, t_remove = table.insert, table.remove; | 6 local t_insert, t_remove = table.insert, table.remove; |
| 7 local add_task = require "util.timer".add_task; | 7 local add_task = require "util.timer".add_task; |
| 8 | 8 |
| 52 add_task(expire_after, expire_pastes); | 52 add_task(expire_after, expire_pastes); |
| 53 end | 53 end |
| 54 return base_url..uuid; | 54 return base_url..uuid; |
| 55 end | 55 end |
| 56 | 56 |
| 57 function handle_request(method, body, request) | 57 function handle_request(event, pasteid) |
| 58 local pasteid = request.url.path:match("[^/]+$"); | |
| 59 if not pasteid or not pastes[pasteid] then | 58 if not pasteid or not pastes[pasteid] then |
| 60 return "Invalid paste id, perhaps it expired?"; | 59 return "Invalid paste id, perhaps it expired?"; |
| 61 end | 60 end |
| 62 | 61 |
| 63 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); | 62 --module:log("debug", "Received request, replying: %s", pastes[pasteid].text); |
| 125 end | 124 end |
| 126 end | 125 end |
| 127 end | 126 end |
| 128 | 127 |
| 129 | 128 |
| 130 local ports = module:get_option("pastebin_ports", { 5280 }); | 129 module:provides("http", { |
| 131 for _, options in ipairs(ports) do | 130 route = { |
| 132 local port, base, ssl, interface = 5280, "pastebin", false, nil; | 131 ["GET /*"] = handle_request; |
| 133 if type(options) == "number" then | 132 }; |
| 134 port = options; | 133 }); |
| 135 elseif type(options) == "table" then | |
| 136 port, base, ssl, interface = options.port or 5280, options.path or "pastebin", options.ssl or false, options.interface; | |
| 137 elseif type(options) == "string" then | |
| 138 base = options; | |
| 139 end | |
| 140 | |
| 141 if not ssl then | |
| 142 base_url = base_url or ("http://"..module:get_host()..(port ~= 80 and (":"..port) or "").."/"..base.."/"); | |
| 143 else | |
| 144 base_url = base_url or ("https://"..module:get_host()..(port ~= 443 and (":"..port) or "").."/"..base.."/"); | |
| 145 end | |
| 146 | |
| 147 httpserver.new{ interface = interface, port = port, base = base, handler = handle_request, ssl = ssl } | |
| 148 end | |
| 149 | 134 |
| 150 local function set_pastes_metatable() | 135 local function set_pastes_metatable() |
| 151 if expire_after == 0 then | 136 if expire_after == 0 then |
| 152 local dm = require "util.datamanager"; | 137 local dm = require "util.datamanager"; |
| 153 setmetatable(pastes, { | 138 setmetatable(pastes, { |
