make a lua script to create a http webserver
- User
make a lua script to create a http webserver
Assistant
Here's a Lua script that creates a basic HTTP web server using LuaSocket:
local socket = require("socket") -- Configuration local HOST = "0.0.0.0" -- Listen on all interfaces local PORT = 8080 local MAX_CONNECTIONS = 10 local RESPONSE_HEADERS = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n" -- Create server socket local server = assert(socket.bind(HOST, PORT)) server:settimeout(0) -- Non-blocking mode print("Server running at http://" .. HOST .. ":" .. PORT) -- Main loop while true do local client = server:accept() if client then client:settimeout(5) -- 5-second timeout for client operations local request, err = client:receive("*l") -- Read first line (request line) if not err then -- Parse request line (GET /path HTTP/1.1) local method, path = request:match("^(%w+)%s+(%S+)%s+HTTP/[%d%.]+$") method = method and method:upper() or "UNKNOWN" path = path or "/" print("Request:", method, path) -- Read remaining headers (until empty line) while true do local line, _ = client:receive("*l") if not line or line == "" then break end end -- Send response local html = string.format([[ <!DOCTYPE html> <html> <head><title>Lua Web Server</title></head> <body> <h1>Hello from Lua!</h1> <p>Server time: %s</p> <p>Request path: %s</p> </body> </html> ]], os.date("%Y-%m-%d %H:%M:%S"), path) client:send(RESPONSE_HEADERS .. html) end client:close() end socket.sleep(0.01) -- Small delay to reduce CPU usage endFeatures:
- Listens on all network interfaces at port 8080
- Responds to all requests with a simple HTML page
- Shows current server time and requested path
- Handles basic HTTP parsing
- Non-blocking operation with timeout protection
Requirements:
- Install LuaSocket:
luarocks install luasocket - Lua 5.1 or newer
To use:
- Save as
webserver.lua - Run with:
lua webserver.lua - Visit in browser:
http://localhost:8080orhttp://your-server-ip:8080
Customization:
- Change
PORTvariable to use different port - Modify HTML content in the
htmlvariable - Adjust
MAX_CONNECTIONSfor concurrent handling - Add route handling by checking the
pathvariable
Example output in browser:
Hello from Lua! Server time: 2023-09-15 14:30:45 Request path: /testSecurity Notes:
- This is a basic educational example
- Not suitable for production use
- No request validation or security features
- No persistent connections (Connection: close header)
- Limited error handling
For a more robust solution, consider:
- Adding proper error handling
- Implementing request size limits
- Adding route-based handling
- Supporting different content types
- Adding security headers
You can extend this foundation by:
- Adding route handlers:
local routes = { ["/"] = function() return "<h1>Home Page</h1>" end, ["/about"] = function() return "<h1>About Page</h1>" end } -- In response section local content = routes[path] and routes[path]() or "<h1>404 Not Found</h1>" client:send(RESPONSE_HEADERS .. content)- Adding file serving capability
- Implementing POST request handling
- Adding logging functionality
- Implementing HTTP basic authentication
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.