diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-04 15:15:45 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-04 15:15:45 +0000 |
| commit | 9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (patch) | |
| tree | 8c3521366ef84f534bbec278437be7ea24e2ac1c /etc/check-links.lua | |
| parent | 63d60223da9de60f873ca08a25dbd9512c998929 (diff) | |
| download | luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2 luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip | |
Só pra não perder se der merda.
Diffstat (limited to 'etc/check-links.lua')
| -rw-r--r-- | etc/check-links.lua | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index 898ed4b..a4e9ef8 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
| @@ -4,15 +4,14 @@ | |||
| 4 | -- Author: Diego Nehab | 4 | -- Author: Diego Nehab |
| 5 | -- RCS ID: $Id$ | 5 | -- RCS ID: $Id$ |
| 6 | ----------------------------------------------------------------------------- | 6 | ----------------------------------------------------------------------------- |
| 7 | 7 | local http = require("http") | |
| 8 | require"http" | 8 | local url = require("url") |
| 9 | 9 | http.TIMEOUT = 10 | |
| 10 | socket.http.TIMEOUT = 10 | ||
| 11 | 10 | ||
| 12 | cache = {} | 11 | cache = {} |
| 13 | 12 | ||
| 14 | function readfile(path) | 13 | function readfile(path) |
| 15 | path = socket.url.unescape(path) | 14 | path = url.unescape(path) |
| 16 | local file, error = io.open(path, "r") | 15 | local file, error = io.open(path, "r") |
| 17 | if file then | 16 | if file then |
| 18 | local body = file:read("*a") | 17 | local body = file:read("*a") |
| @@ -21,32 +20,32 @@ function readfile(path) | |||
| 21 | else return nil, error end | 20 | else return nil, error end |
| 22 | end | 21 | end |
| 23 | 22 | ||
| 24 | function getstatus(url) | 23 | function getstatus(u) |
| 25 | local parsed = socket.url.parse(url, { scheme = "file" }) | 24 | local parsed = url.parse(u, {scheme = "file"}) |
| 26 | if cache[url] then return cache[url] end | 25 | if cache[u] then return cache[u] end |
| 27 | local res | 26 | local res |
| 28 | if parsed.scheme == "http" then | 27 | if parsed.scheme == "http" then |
| 29 | local request = { url = url, method = "HEAD" } | 28 | local request = {url = u, method = "HEAD"} |
| 30 | local response = socket.http.request(request) | 29 | local response = http.request(request) |
| 31 | if response.code == 200 then res = nil | 30 | if response.code == 200 then res = nil |
| 32 | else res = response.status or response.error end | 31 | else res = response.status or response.error end |
| 33 | elseif parsed.scheme == "file" then | 32 | elseif parsed.scheme == "file" then |
| 34 | local file, error = io.open(socket.url.unescape(parsed.path), "r") | 33 | local file, error = io.open(url.unescape(parsed.path), "r") |
| 35 | if file then | 34 | if file then |
| 36 | file:close() | 35 | file:close() |
| 37 | res = nil | 36 | res = nil |
| 38 | else res = error end | 37 | else res = error end |
| 39 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | 38 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end |
| 40 | cache[url] = res | 39 | cache[u] = res |
| 41 | return res | 40 | return res |
| 42 | end | 41 | end |
| 43 | 42 | ||
| 44 | function retrieve(url) | 43 | function retrieve(u) |
| 45 | local parsed = socket.url.parse(url, { scheme = "file" }) | 44 | local parsed = url.parse(u, { scheme = "file" }) |
| 46 | local body, headers, code, error | 45 | local body, headers, code, error |
| 47 | local base = url | 46 | local base = u |
| 48 | if parsed.scheme == "http" then | 47 | if parsed.scheme == "http" then |
| 49 | body, headers, code, error = socket.http.get(url) | 48 | body, headers, code, error = http.get(u) |
| 50 | if code == 200 then | 49 | if code == 200 then |
| 51 | base = base or headers.location | 50 | base = base or headers.location |
| 52 | end | 51 | end |
| @@ -62,19 +61,19 @@ function getlinks(body, base) | |||
| 62 | local links = {} | 61 | local links = {} |
| 63 | -- extract links | 62 | -- extract links |
| 64 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) | 63 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) |
| 65 | table.insert(links, socket.url.absolute(base, href)) | 64 | table.insert(links, url.absolute(base, href)) |
| 66 | end) | 65 | end) |
| 67 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) | 66 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) |
| 68 | table.insert(links, socket.url.absolute(base, href)) | 67 | table.insert(links, url.absolute(base, href)) |
| 69 | end) | 68 | end) |
| 70 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) | 69 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) |
| 71 | table.insert(links, socket.url.absolute(base, href)) | 70 | table.insert(links, url.absolute(base, href)) |
| 72 | end) | 71 | end) |
| 73 | return links | 72 | return links |
| 74 | end | 73 | end |
| 75 | 74 | ||
| 76 | function checklinks(url) | 75 | function checklinks(u) |
| 77 | local base, body, error = retrieve(url) | 76 | local base, body, error = retrieve(u) |
| 78 | if not body then print(error) return end | 77 | if not body then print(error) return end |
| 79 | local links = getlinks(body, base) | 78 | local links = getlinks(body, base) |
| 80 | for _, l in ipairs(links) do | 79 | for _, l in ipairs(links) do |
| @@ -91,5 +90,5 @@ if table.getn(arg) < 1 then | |||
| 91 | end | 90 | end |
| 92 | for _, a in ipairs(arg) do | 91 | for _, a in ipairs(arg) do |
| 93 | print("Checking ", a) | 92 | print("Checking ", a) |
| 94 | checklinks(socket.url.absolute("file:", a)) | 93 | checklinks(url.absolute("file:", a)) |
| 95 | end | 94 | end |
