httpserver-static.lua 986 B

123456789101112131415161718192021222324252627282930
  1. -- httpserver-static.lua
  2. -- Part of nodemcu-httpserver, handles sending static files to client.
  3. -- Author: Marcos Kirsch
  4. return function (connection, req, args)
  5. dofile("httpserver-header.lc")(connection, 200, args.ext, args.gzipped)
  6. --print("Begin sending:", args.file)
  7. -- Send file in little chunks
  8. local continue = true
  9. local bytesSent = 0
  10. while continue do
  11. collectgarbage()
  12. -- NodeMCU file API lets you open 1 file at a time.
  13. -- So we need to open, seek, close each time in order
  14. -- to support multiple simultaneous clients.
  15. file.open(args.file)
  16. file.seek("set", bytesSent)
  17. local chunk = file.read(256)
  18. file.close()
  19. if chunk == nil then
  20. continue = false
  21. else
  22. connection:send(chunk)
  23. bytesSent = bytesSent + #chunk
  24. chunk = nil
  25. --print("Sent" .. args.file, bytesSent)
  26. end
  27. end
  28. --print("Finished sending:", args.file)
  29. end