🎉 initial commit
This commit is contained in:
1
requirements.sh
Executable file
1
requirements.sh
Executable file
@@ -0,0 +1 @@
|
|||||||
|
luarocks install luasocket
|
||||||
47
server.lua
Normal file
47
server.lua
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
local socket = require("socket")
|
||||||
|
|
||||||
|
local host = "localhost"
|
||||||
|
local port = 8080
|
||||||
|
local server = assert(socket.bind(host, port))
|
||||||
|
print("Server started on http://" .. host .. ":" .. port .. "/hello?name=YourName")
|
||||||
|
|
||||||
|
-- Helper function to extract query parameters
|
||||||
|
local function get_query_param(url, key)
|
||||||
|
local pattern = key .. "=([^&]+)"
|
||||||
|
return url:match(pattern)
|
||||||
|
end
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local client = server:accept()
|
||||||
|
client:settimeout(10)
|
||||||
|
|
||||||
|
local line, err = client:receive()
|
||||||
|
if not err then
|
||||||
|
-- Extract method and URI (e.g., "GET /hello?name=Raycast HTTP/1.1")
|
||||||
|
local method, uri = line:match("^(%a+)%s+(%S+)%s+HTTP")
|
||||||
|
|
||||||
|
local response_body = ""
|
||||||
|
local status = "404 Not Found"
|
||||||
|
|
||||||
|
-- Basic Routing: Check if path starts with /hello
|
||||||
|
if uri and uri:find("^/hello") then
|
||||||
|
status = "200 OK"
|
||||||
|
-- Extract 'name' param or default to 'User'
|
||||||
|
local name = get_query_param(uri, "name") or "User"
|
||||||
|
-- URL decoding for spaces (%20)
|
||||||
|
name = name:gsub("%%20", " ")
|
||||||
|
response_body = "Hello " .. name .. "!"
|
||||||
|
else
|
||||||
|
response_body = "Not Found"
|
||||||
|
end
|
||||||
|
|
||||||
|
local response = "HTTP/1.1 " .. status .. "\r\n" ..
|
||||||
|
"Content-Type: text/plain\r\n" ..
|
||||||
|
"Content-Length: " .. #response_body .. "\r\n" ..
|
||||||
|
"Connection: close\r\n\r\n" ..
|
||||||
|
response_body
|
||||||
|
|
||||||
|
client:send(response)
|
||||||
|
end
|
||||||
|
client:close()
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user