Advertisement
Imgoodisher

silver_server_prealpha

Nov 6th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. local url = "www.example.com"
  2. if not fs.exists("/"..url) then fs.makeDir("/"..url) end
  3. rednet.open("right")
  4.  
  5. function server_loop()
  6.     while true do
  7.         local id, msg = rednet.receive()
  8.         print(msg)
  9.         if msg:find("^LIST%s.-%sRTTP") then
  10.             -- onList
  11.             rednet.send(id, url);
  12.         elseif msg:find("^GET") then
  13.             local page, head = msg:match("GET%s([^%s]+)[^\n]*\n([^\n]*)")
  14.             local headers = {}
  15.             head:gsub('"([^"]*)":"([^"]*)"', function(k, v)
  16.                 headers[k] = v
  17.             end)
  18.             -- onBeforeSend
  19.             if page then
  20.                 page = page:gsub("%.%.", "")
  21.                 print("/"..url..page)
  22.                 local f = io.open("/"..url..page, "r")
  23.                 if f then
  24.                     data = f:read("*a")
  25.                     f:close()
  26.                     print("200")
  27.                     rednet.send(id, '200 OK\n"type":"text/lua"\n'..data)
  28.                 else
  29.                     print("404")
  30.                     rednet.send(id, '404 Not Found\n"type":"text/lua"\nprint("404 Not Found")')
  31.                 end
  32.             else
  33.                 print("400-2")
  34.                 rednet.send(id, '400 Bad Request\n"type":"text/lua"\bprint("400 Bad Request")')
  35.             end
  36.         else
  37.             print("400-1")
  38.             rednet.send(id, '400 Bad Request\n"type":"text/lua"\bprint("400 Bad Request")')
  39.         end
  40.     end
  41. end
  42.  
  43. parallel.waitForAny(server_loop, function() shell.run("shell") end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement