local RESPONSE = [[ plop

Hello, from plop/lua!

MethodPathVersion
%s%s%s

HeaderValue %s
]] local HEADER = "%s%s%s\n" local function handle_request (stream) local status = 200 local method, path, version, h = stream:read(" ", " ", "\r\n", "\r\n\r\n") local headers = {} if h then for header, value in h:gmatch "([^%s:]+)%s*:%s+([^\r\n]*)" do headers[header:lower()] = value end end local connection = headers.connection or "close" if method == nil or path == nil or version ~= "HTTP/1.1" or h == nil then status = 400 end local headers_table = "" for header, value in pairs(headers) do headers_table = HEADER:format(headers_table, header, value) end local response = RESPONSE:format(method, path, version, headers_table) stream:write( "HTTP/1.1 ", status, "\r\n", "Connection: ", connection, "\r\n", "Content-Length: ", #response, "\r\n", "Content-Type: text/html\r\n\r\n", response) stream:flush() return connection end --- Default client request handler. -- TODO: Add documentation once request and response API are more stable. return function (stream) repeat local connection = handle_request(stream) until connection == "close" end