httpserver-static.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. --print("Begin sending:", args.file)
  6. --print("node.heap(): ", node.heap())
  7. if args.isGzipped and req.isCheckModifiedRequest() then
  8. dofile("httpserver-header.lc")(connection, 304, args.ext, args.isGzipped)
  9. else
  10. dofile("httpserver-header.lc")(connection, 200, args.ext, args.isGzipped)
  11. -- Send file in little chunks
  12. local continue = true
  13. local size = file.list()[args.file]
  14. local bytesSent = 0
  15. -- Chunks larger than 1024 don't work.
  16. -- https://github.com/nodemcu/nodemcu-firmware/issues/1075
  17. local chunkSize = 1024
  18. while continue do
  19. collectgarbage()
  20. -- NodeMCU file API lets you open 1 file at a time.
  21. -- So we need to open, seek, close each time in order
  22. -- to support multiple simultaneous clients.
  23. file.open(args.file)
  24. file.seek("set", bytesSent)
  25. local chunk = file.read(chunkSize)
  26. file.close()
  27. connection:send(chunk)
  28. bytesSent = bytesSent + #chunk
  29. chunk = nil
  30. --print("Sent: " .. bytesSent .. " of " .. size)
  31. if bytesSent == size then continue = false end
  32. end
  33. --print("Finished sending: ", args.file)
  34. end
  35. end