resource_script_demo.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. f:write(mg.read())
  68. f:close()
  69. mg.write("HTTP/1.0 200 OK\r\n")
  70. mg.write("Connection: close\r\n")
  71. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  72. mg.write("\r\n")
  73. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  74. mg.write("<body>Resource of type \"" .. mime .. "\" created.</body></html>\r\n")
  75. end
  76. return
  77. end
  78. if method=="DELETE" then
  79. file = resourcedir .. "/" .. subresource
  80. mime = mg.get_mime_type(file)
  81. if lfs.attributes(file) then
  82. os.remove(file)
  83. mg.write("HTTP/1.0 200 OK\r\n")
  84. mg.write("Connection: close\r\n")
  85. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  86. mg.write("\r\n")
  87. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  88. mg.write("<body>Resource of type \"" .. mime .. "\" deleted.</body></html>\r\n")
  89. else
  90. mime = mg.get_mime_type(file)
  91. mg.write("HTTP/1.0 404 Not Found\r\n")
  92. mg.write("Connection: close\r\n")
  93. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  94. mg.write("\r\n")
  95. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  96. mg.write("<body>Resource of type \"" .. mime .. "\" not found.</body></html>\r\n")
  97. end
  98. return
  99. end
  100. -- Any other method
  101. mg.write("HTTP/1.0 405 Method Not Allowed\r\n")
  102. mg.write("Connection: close\r\n")
  103. mg.write("Content-Type: text/html; charset=utf-8\r\n")
  104. mg.write("\r\n")
  105. mg.write("<html><head><title>Civetweb Lua script resource handling test</title></head>\r\n")
  106. mg.write("<body>Method not allowed.</body></html>\r\n")