Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function setupConnection(addr)
- local url = string.gsub(addr,"https?://","",1)
- local domain = string.gsub(url,"/.*","",1)
- local path = string.gsub(url,".-/","/",1)
- local file
- local header = ""
- file = internet.open(domain, 80)
- file:setTimeout(10)
- file:write("GET "..path.." HTTP/1.1\r\nHost: "..domain.."\r\nConnection: close\r\n\r\n")
- repeat
- local hBlock = file:read(block)
- if not hBlock or #hBlock <= 0 then
- error("no valid HTTP response")
- end
- header = header .. hBlock
- until string.find(header,"\r\n\r\n")
- if string.match(header, "\r\n\r\n(.-)\r\n") then
- local status = string.match(header, "\r\n\r\n(.-)\r\n")
- local location = string.match(header, "[Ll]ocation: (.-)\r\n")
- if string.match(status, "^3%d%d") then
- if location ~= addr then
- file:close()
- print("Status: "..status)
- print("Redirecting to "..location)
- return setupConnection(location)
- end
- end
- if string.match(status, "^2%d%d") then
- print("Status: "..status)
- print("Domain: "..domain)
- print("Path: "..path)
- return file, header
- end
- error(string.match(header, "\r\n\r\n(.*)"))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement