resource_script_demo.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. -- This is a Lua script that handles sub-resources, e.g. resource_script_demo.lua/path/file.ext
  2. scriptUri = "resource_script_demo.lua"
  3. envVar = "resource_script_demo_storage"
  4. resourcedir = os.getenv(envVar) or "R:\\RESOURCEDIR"
  5. method = mg.request_info.request_method:upper()
  6. if resourcedir then
  7. attr = lfs.attributes(resourcedir)
  8. end
  9. if (not mg.request_info.uri:find(scriptUri)) or (not resourcedir) or (not attr) or (attr.mode~="directory") then
  10. mg.write("HTTP/1.0 500 OK\r\n")
  11. mg.write("Connection: close\r\n")
  12. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  13. mg.write("\r\n")
  14. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  15. mg.write("<body>\r\nServer error.<br>\r\n")
  16. mg.write("The server admin must make sure this script is available as URI " .. scriptUri .. "<br>\r\n")
  17. mg.write("The server admin must set the environment variable " .. envVar .. " to a directory.<br>\r\n")
  18. mg.write("</body>\r\n</html>\r\n")
  19. return
  20. end
  21. subresource = mg.request_info.uri:match(scriptUri .. "/(.*)")
  22. if not subresource then
  23. if method=="GET" then
  24. mg.write("HTTP/1.0 200 OK\r\n")
  25. mg.write("Connection: close\r\n")
  26. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  27. mg.write("\r\n")
  28. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  29. mg.write("<body>No resource specified.<br>resourcedir is " .. resourcedir .. "</body></html>\r\n")
  30. else
  31. mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
  32. mg.write("Connection: close\r\n")
  33. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  34. mg.write("\r\n")
  35. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  36. mg.write("<body>Method not allowed.</body></html>\r\n")
  37. end
  38. return
  39. end
  40. if method=="GET" then
  41. file = resourcedir .. "/" .. subresource
  42. if lfs.attributes(file) then
  43. mg.send_file(file)
  44. else
  45. mime = mg.get_mime_type(file)
  46. mg.write("HTTP/1.0 404 Not Found\r\n")
  47. mg.write("Connection: close\r\n")
  48. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  49. mg.write("\r\n")
  50. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  51. mg.write("<body>Resource of type \"" .. mime .. "\" not found.</body></html>\r\n")
  52. end
  53. return
  54. end
  55. if method=="PUT" then
  56. file = resourcedir .. "/" .. subresource
  57. mime = mg.get_mime_type(file)
  58. if lfs.attributes(file) then
  59. mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
  60. mg.write("Connection: close\r\n")
  61. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  62. mg.write("\r\n")
  63. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  64. mg.write("<body>Resource of type \"" .. mime .. "\" already exists.</body></html>\r\n")
  65. else
  66. local f = io.open(file, "w")
  67. local data = {}
  68. repeat
  69. local l = mg.read();
  70. data[#data+1] = l;
  71. until ((l == "") or (l == nil));
  72. f:write(table.concat(data, ""))
  73. f:close()
  74. mg.write("HTTP/1.0 200 OK\r\n")
  75. mg.write("Connection: close\r\n")
  76. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  77. mg.write("\r\n")
  78. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  79. mg.write("<body>Resource of type \"" .. mime .. "\" created.</body></html>\r\n")
  80. end
  81. return
  82. end
  83. if method=="DELETE" then
  84. file = resourcedir .. "/" .. subresource
  85. mime = mg.get_mime_type(file)
  86. if lfs.attributes(file) then
  87. os.remove(file)
  88. mg.write("HTTP/1.0 200 OK\r\n")
  89. mg.write("Connection: close\r\n")
  90. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  91. mg.write("\r\n")
  92. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  93. mg.write("<body>Resource of type \"" .. mime .. "\" deleted.</body></html>\r\n")
  94. else
  95. mime = mg.get_mime_type(file)
  96. mg.write("HTTP/1.0 404 Not Found\r\n")
  97. mg.write("Connection: close\r\n")
  98. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  99. mg.write("\r\n")
  100. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  101. mg.write("<body>Resource of type \"" .. mime .. "\" not found.</body></html>\r\n")
  102. end
  103. return
  104. end
  105. -- Any other method
  106. mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
  107. mg.write("Connection: close\r\n")
  108. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  109. mg.write("\r\n")
  110. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  111. mg.write("<body>Method not allowed.</body></html>\r\n")