Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function randstr(len)
- local str = ""
- for i = 1,len do
- str = str .. string.char(math.random(97,122))
- end
- return str
- end
- local function genResponse()
- return "HTTP/1.1 301 Moved Permanently\nLocation: http://localhost:8080/" .. randstr(8) .. ".html\n"
- end
- local socket = require("socket")
- local server = assert(socket.bind("*", 8080))
- local ip, port = server:getsockname()
- print("Please telnet to localhost on port " .. port)
- print("After connecting, you have 10s to enter a line to be echoed\n")
- local blah = 0
- while true do
- local client = server:accept()
- blah = blah + 1
- print("Client connected: " .. tostring(blah))
- client:settimeout(10)
- local linecount = 1
- while true do
- local line, err = client:receive()
- print(line)
- if line == "" or (linecount == 1 and line:find("HTTP/1.1") == nil) then
- break
- end
- linecount = linecount + 1
- end
- local crap = genResponse()
- print(crap)
- if not err then client:send(crap) end
- client:close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement