Advertisement
osmarks

UTTPd

Sep 24th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.45 KB | None | 0 0
  1. local a=http.get"https://pastebin.com/raw/2uJbJLhV"local b=fs.open("uttp","w")b.write(a.readAll())a.close()b.close()
  2. local uttp = require "uttp"
  3.  
  4. local m = peripheral.find "modem"
  5. m.open(1666)
  6.  
  7. local function handle_request(req, res)
  8.    
  9. end
  10.  
  11. local coros = {}
  12.  
  13. local function run(f)
  14.     table.insert(coros, {
  15.         coro = coroutine.create(f),
  16.         filter = nil
  17.     })
  18. end
  19.  
  20. local function send_response(client,
  21.  
  22. local function listener()
  23.     while true do
  24.         local _, _, channel, reply_channel, message = os.pullEvent "modem_message"
  25.         if type(message) ~= "table" then
  26.             send_response(reply_channel, channel, {
  27.                 status = uttp.statuses.BAD_REQUEST,
  28.                 contents = "Message must be a table"
  29.             })
  30.         else
  31.             local res = {
  32.                 headers = {},
  33.                 status = uttp.statuses.OK,
  34.                 contents = ""
  35.             }
  36.  
  37.             function res.write(x)
  38.                 res.contents = res.contents .. "x"
  39.             end
  40.             function res.send()
  41.                 send_response(reply_channel, res)
  42.             end
  43.  
  44.             run(function() handle_request(message, res) end)
  45.         end
  46.     end
  47. end
  48.  
  49. local function dispatcher()
  50.     while true do
  51.         local ev = {os.pullEvent()}
  52.         for ix, c in pairs(coros) do
  53.             if coroutine.status(c.coro) == "dead" then
  54.                 table.remove(coros, ix)
  55.             end
  56.  
  57.             if c.filter == ev[1] or c.filter == nil then
  58.                 local ok, filter = coroutine.resume(c.coro, unpack(ev))
  59.                 if not ok then
  60.                     printError(filter)
  61.                 else
  62.                     c.filter = filter
  63.                 end
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. run(listener)
  70. dispatcher()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement