Operating system for OpenComputers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

79 satır
1.8KB

  1. local minitel = require "minitel"
  2. local serial = require "serialization"
  3. local cfg = {["path"]="/boot/srv/frequest",["port"]=70}
  4. f=io.open("/boot/cfg/fserv.cfg","rb")
  5. if f then
  6. local ncfg = serial.unserialize(f:read("*a"))
  7. f:close()
  8. for k,v in pairs(ncfg) do
  9. cfg[k] = v
  10. end
  11. end
  12. local function fileHandler(socket,rtype,path)
  13. syslog(string.format("[%s:%d] %s %s",socket.addr,socket.port,rtype,path),syslog.info,"fserv")
  14. if rtype == "t" then
  15. if fs.exists(path) and fs.isDirectory(path) then
  16. socket:write("d")
  17. for _,file in ipairs(fs.list(path)) do
  18. socket:write(file.."\n")
  19. end
  20. elseif fs.exists(path) and not fs.isDirectory(path) then
  21. local f,err = io.open(path,"rb")
  22. if f then
  23. socket:write("y")
  24. while true do
  25. local c = f:read(4096)
  26. if not c or c == "" then break end
  27. socket:write(c)
  28. end
  29. else
  30. socket:write("fFailed to open file: "..err)
  31. end
  32. else
  33. socket:write("nFile not found")
  34. end
  35. elseif rtype == "s" then
  36. if fs.exists(path) then
  37. local ftype = "f"
  38. if fs.isDirectory(path) then
  39. ftype = "d"
  40. end
  41. socket:write(string.format("y%s\n%d",ftype,fs.size(path)))
  42. else
  43. socket:write("nFile not found.")
  44. end
  45. else
  46. socket:write("fUnknown request type")
  47. end
  48. socket:close()
  49. end
  50. local function httpHandler(socket,rtype,path)
  51. socket:write("fHTTP requests are not yet implemented.")
  52. socket:close()
  53. end
  54. local function socketHandler(socket)
  55. return function()
  56. local line = nil
  57. repeat
  58. coroutine.yield()
  59. line = socket:read()
  60. until line
  61. local rtype, path = line:match("(.)(.+)")
  62. if path:sub(1,6) == "/http/" or path:sub(1,5) == "http/" then
  63. httpHandler(socket,rtype,path)
  64. else
  65. path = (cfg.path .. "/" .. path:gsub("../","")):gsub("/+","/")
  66. fileHandler(socket,rtype,path)
  67. end
  68. socket:close()
  69. end
  70. end
  71. while true do
  72. os.spawn(socketHandler(minitel.listen(70)))
  73. end