diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 22:51:04 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 22:51:04 +0000 |
| commit | 9fc682a106f13901b60a665619fc61d9dae49fb0 (patch) | |
| tree | 6efdd73fa4f2abce56b6fb710f7516d0a5c6c758 /etc/check-links.lua | |
| parent | 574708380f19b15bd19419bfd64ccbe422f2d924 (diff) | |
| download | luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.tar.gz luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.tar.bz2 luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.zip | |
HTTP now has only one function.
Diffstat (limited to 'etc/check-links.lua')
| -rw-r--r-- | etc/check-links.lua | 25 |
1 files changed, 9 insertions, 16 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index a4e9ef8..4487593 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
| @@ -8,8 +8,6 @@ local http = require("http") | |||
| 8 | local url = require("url") | 8 | local url = require("url") |
| 9 | http.TIMEOUT = 10 | 9 | http.TIMEOUT = 10 |
| 10 | 10 | ||
| 11 | cache = {} | ||
| 12 | |||
| 13 | function readfile(path) | 11 | function readfile(path) |
| 14 | path = url.unescape(path) | 12 | path = url.unescape(path) |
| 15 | local file, error = io.open(path, "r") | 13 | local file, error = io.open(path, "r") |
| @@ -22,22 +20,14 @@ end | |||
| 22 | 20 | ||
| 23 | function getstatus(u) | 21 | function getstatus(u) |
| 24 | local parsed = url.parse(u, {scheme = "file"}) | 22 | local parsed = url.parse(u, {scheme = "file"}) |
| 25 | if cache[u] then return cache[u] end | ||
| 26 | local res | ||
| 27 | if parsed.scheme == "http" then | 23 | if parsed.scheme == "http" then |
| 28 | local request = {url = u, method = "HEAD"} | 24 | local r, c, h, s = http.request{url = u, method = "HEAD"} |
| 29 | local response = http.request(request) | 25 | if c ~= 200 then return s or c end |
| 30 | if response.code == 200 then res = nil | ||
| 31 | else res = response.status or response.error end | ||
| 32 | elseif parsed.scheme == "file" then | 26 | elseif parsed.scheme == "file" then |
| 33 | local file, error = io.open(url.unescape(parsed.path), "r") | 27 | local file, error = io.open(url.unescape(parsed.path), "r") |
| 34 | if file then | 28 | if file then file:close() |
| 35 | file:close() | 29 | else return error end |
| 36 | res = nil | 30 | else return string.format("unhandled scheme '%s'", parsed.scheme) end |
| 37 | else res = error end | ||
| 38 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | ||
| 39 | cache[u] = res | ||
| 40 | return res | ||
| 41 | end | 31 | end |
| 42 | 32 | ||
| 43 | function retrieve(u) | 33 | function retrieve(u) |
| @@ -45,10 +35,13 @@ function retrieve(u) | |||
| 45 | local body, headers, code, error | 35 | local body, headers, code, error |
| 46 | local base = u | 36 | local base = u |
| 47 | if parsed.scheme == "http" then | 37 | if parsed.scheme == "http" then |
| 48 | body, headers, code, error = http.get(u) | 38 | body, code, headers = http.request(u) |
| 49 | if code == 200 then | 39 | if code == 200 then |
| 50 | base = base or headers.location | 40 | base = base or headers.location |
| 51 | end | 41 | end |
| 42 | if not body then | ||
| 43 | error = code | ||
| 44 | end | ||
| 52 | elseif parsed.scheme == "file" then | 45 | elseif parsed.scheme == "file" then |
| 53 | body, error = readfile(parsed.path) | 46 | body, error = readfile(parsed.path) |
| 54 | else error = string.format("unhandled scheme '%s'", parsed.scheme) end | 47 | else error = string.format("unhandled scheme '%s'", parsed.scheme) end |
