cgi.lua 904 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("gluon.web.cgi", package.seeall)
  5. local nixio = require("nixio")
  6. require("gluon.web.http")
  7. require("gluon.web.dispatcher")
  8. -- Limited source to avoid endless blocking
  9. local function limitsource(handle, limit)
  10. limit = limit or 0
  11. local BLOCKSIZE = 2048
  12. return function()
  13. if limit < 1 then
  14. handle:close()
  15. return nil
  16. else
  17. local read = (limit > BLOCKSIZE) and BLOCKSIZE or limit
  18. limit = limit - read
  19. local chunk = handle:read(read)
  20. if not chunk then handle:close() end
  21. return chunk
  22. end
  23. end
  24. end
  25. function run()
  26. local http = gluon.web.http.Http(
  27. nixio.getenv(),
  28. limitsource(io.stdin, tonumber(nixio.getenv("CONTENT_LENGTH"))),
  29. io.stdout
  30. )
  31. gluon.web.dispatcher.httpdispatch(http)
  32. end