Advertisement
gamax92

Untitled

Jan 5th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. local function randstr(len)
  2. local str = ""
  3. for i = 1,len do
  4. str = str .. string.char(math.random(97,122))
  5. end
  6. return str
  7. end
  8.  
  9. local function genResponse()
  10. return "HTTP/1.1 301 Moved Permanently\nLocation: http://localhost:8080/" .. randstr(8) .. ".html\n"
  11. end
  12.  
  13. local socket = require("socket")
  14. local server = assert(socket.bind("*", 8080))
  15. local ip, port = server:getsockname()
  16. print("Please telnet to localhost on port " .. port)
  17. print("After connecting, you have 10s to enter a line to be echoed\n")
  18. local blah = 0
  19. while true do
  20. local client = server:accept()
  21. blah = blah + 1
  22. print("Client connected: " .. tostring(blah))
  23. client:settimeout(10)
  24. local linecount = 1
  25. while true do
  26. local line, err = client:receive()
  27. print(line)
  28. if line == "" or (linecount == 1 and line:find("HTTP/1.1") == nil) then
  29. break
  30. end
  31. linecount = linecount + 1
  32. end
  33. local crap = genResponse()
  34. print(crap)
  35. if not err then client:send(crap) end
  36. client:close()
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement