diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-27 20:02:58 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-09-27 20:02:58 +0000 |
| commit | ad997de5569c28a0877544d9b8dc8da66dd61c37 (patch) | |
| tree | 3189c0b8a01803273f3a5f78062019e2d15b60e1 | |
| parent | e5e1f3dc7a750595bb3fbe219c369c3950220986 (diff) | |
| download | luasocket-ad997de5569c28a0877544d9b8dc8da66dd61c37.tar.gz luasocket-ad997de5569c28a0877544d9b8dc8da66dd61c37.tar.bz2 luasocket-ad997de5569c28a0877544d9b8dc8da66dd61c37.zip | |
Initial revision
| -rw-r--r-- | etc/check-links.lua | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua new file mode 100644 index 0000000..5d33732 --- /dev/null +++ b/etc/check-links.lua | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | dofile("../lua/http.lua") | ||
| 2 | HTTP.TIMEOUT = 10 | ||
| 3 | dofile("../lua/code.lua") | ||
| 4 | dofile("../lua/url.lua") | ||
| 5 | dofile("../lua/concat.lua") | ||
| 6 | |||
| 7 | cache = {} | ||
| 8 | |||
| 9 | function readfile(path) | ||
| 10 | path = Code.unescape(path) | ||
| 11 | local file, error = openfile(path, "r") | ||
| 12 | if file then | ||
| 13 | local body = read(file, "*a") | ||
| 14 | closefile(file) | ||
| 15 | return body | ||
| 16 | else return nil, error end | ||
| 17 | end | ||
| 18 | |||
| 19 | function getstatus(url) | ||
| 20 | local parsed = URL.parse_url(url, { scheme = "file" }) | ||
| 21 | if cache[url] then return cache[url].res end | ||
| 22 | local res | ||
| 23 | if parsed.scheme == "http" then | ||
| 24 | local request = { url = url } | ||
| 25 | local response = { body_cb = function(chunk, err) | ||
| 26 | return nil | ||
| 27 | end } | ||
| 28 | local blocksize = HTTP.BLOCKSIZE | ||
| 29 | HTTP.BLOCKSIZE = 1 | ||
| 30 | response = HTTP.request_cb(request, response) | ||
| 31 | HTTP.BLOCKSIZE = blocksize | ||
| 32 | if response.code == 200 then res = nil | ||
| 33 | else res = response.status or response.error end | ||
| 34 | elseif parsed.scheme == "file" then | ||
| 35 | local file, error = openfile(Code.unescape(parsed.path), "r") | ||
| 36 | if file then | ||
| 37 | closefile(file) | ||
| 38 | res = nil | ||
| 39 | else res = error end | ||
| 40 | else res = format("unhandled scheme '%s'", parsed.scheme) end | ||
| 41 | cache[url] = { res = res } | ||
| 42 | return res | ||
| 43 | end | ||
| 44 | |||
| 45 | function retrieve(url) | ||
| 46 | local parsed = URL.parse_url(url, { scheme = "file" }) | ||
| 47 | local base, body, error | ||
| 48 | base = url | ||
| 49 | if parsed.scheme == "http" then | ||
| 50 | local response = HTTP.request{url = url} | ||
| 51 | if response.code ~= 200 then | ||
| 52 | error = response.status or response.error | ||
| 53 | else | ||
| 54 | base = response.headers.location or url | ||
| 55 | body = response.body | ||
| 56 | end | ||
| 57 | elseif parsed.scheme == "file" then | ||
| 58 | body, error = readfile(parsed.path) | ||
| 59 | else error = format("unhandled scheme '%s'", parsed.scheme) end | ||
| 60 | return base, body, error | ||
| 61 | end | ||
| 62 | |||
| 63 | function getlinks(body, base) | ||
| 64 | -- get rid of comments | ||
| 65 | body = gsub(body, "%<%!%-%-.-%-%-%>", "") | ||
| 66 | local links = {} | ||
| 67 | -- extract links | ||
| 68 | gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) | ||
| 69 | tinsert(%links, URL.absolute_url(%base, href)) | ||
| 70 | end) | ||
| 71 | gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) | ||
| 72 | tinsert(%links, URL.absolute_url(%base, href)) | ||
| 73 | end) | ||
| 74 | gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href) | ||
| 75 | tinsert(%links, URL.absolute_url(%base, href)) | ||
| 76 | end) | ||
| 77 | return links | ||
| 78 | end | ||
| 79 | |||
| 80 | function checklinks(url) | ||
| 81 | local base, body, error = retrieve(url) | ||
| 82 | if not body then print(error) return end | ||
| 83 | local links = getlinks(body, base) | ||
| 84 | for i = 1, getn(links) do | ||
| 85 | write(_STDERR, "\t", links[i], "\n") | ||
| 86 | local err = getstatus(links[i]) | ||
| 87 | if err then write(links[i], ": ", err, "\n") end | ||
| 88 | end | ||
| 89 | end | ||
| 90 | |||
| 91 | arg = arg or {} | ||
| 92 | if getn(arg) < 1 then | ||
| 93 | write("Usage:\n luasocket -f check-links.lua {<url>}\n") | ||
| 94 | exit() | ||
| 95 | end | ||
| 96 | for i = 1, getn(arg) do | ||
| 97 | write("Checking ", arg[i], "\n") | ||
| 98 | checklinks(URL.absolute_url("file:", arg[i])) | ||
| 99 | end | ||
