prime_numbers.lp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?
  2. -- Lua server pages have full control over the output, including HTTP
  3. -- headers they send to the client. Send HTTP headers:
  4. print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
  5. ?>
  6. <html>
  7. <p>Prime numbers from 0 to 100, calculated by Lua:</p>
  8. <?
  9. function is_prime(n)
  10. if n <= 0 then return false end
  11. if n <= 2 then return true end
  12. if (n % 2 == 0) then return false end
  13. for i = 3, n / 2, 2 do
  14. if (n % i == 0) then return false end
  15. end
  16. return true
  17. end
  18. for i = 1, 100 do
  19. if is_prime(i) then print('<span>' .. i .. '</span>&nbsp;') end
  20. end
  21. ?>
  22. <p>Reading POST data from Lua (click submit):</p>
  23. <form method="POST" ><input type="text" name="t1"/><input type="submit"></form>
  24. <pre>
  25. POST data: [<? print(read())?>]
  26. request method: [<? print(request_info.request_method) ?>]
  27. IP/port: [<? print(request_info.remote_ip, ':', request_info.remote_port) ?>]
  28. URI: [<? print(request_info.uri) ?>]
  29. HTTP version [<? print(request_info.http_version) ?>]
  30. HEADERS:
  31. <?
  32. for name, value in pairs(request_info.http_headers) do
  33. print(name, ':', value, '\n')
  34. end
  35. ?>
  36. </pre>
  37. </html>