-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.lua
More file actions
33 lines (27 loc) · 887 Bytes
/
app.lua
File metadata and controls
33 lines (27 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
local socket = require("socket")
local host = "127.0.0.1"
local port = 9000
local server = assert(socket.bind(host, port))
print("Lua HTTP server listening on " .. host .. ":" .. port)
while true do
local client = server:accept()
client:settimeout(1)
-- Read the request (we ignore details and always respond with Hello World)
local request = client:receive("*l")
-- Consume the rest of the HTTP headers
while true do
local line, err = client:receive("*l")
if not line or line == "" then
break
end
end
local body = "Hello World from Lua! Yousef"
local response = "HTTP/1.1 200 OK\r\n" ..
"Content-Type: text/plain\r\n" ..
"Content-Length: " .. #body .. "\r\n" ..
"Connection: close\r\n" ..
"\r\n" ..
body
client:send(response)
client:close()
end