diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-26 00:18:41 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-26 00:18:41 +0000 |
commit | e77f1792005088f55353c6c85fe9335e60772754 (patch) | |
tree | 097b71fc87bae4e1b3a8dd4e0a7869e6976fd9c1 /etc | |
parent | 5b279bac9eea05d764e9cd14c89a699da8898126 (diff) | |
download | luasocket-e77f1792005088f55353c6c85fe9335e60772754.tar.gz luasocket-e77f1792005088f55353c6c85fe9335e60772754.tar.bz2 luasocket-e77f1792005088f55353c6c85fe9335e60772754.zip |
Adjusted some of the broken examples.
Diffstat (limited to 'etc')
-rw-r--r-- | etc/check-links.lua | 41 | ||||
-rw-r--r-- | etc/get.lua | 3 |
2 files changed, 17 insertions, 27 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index c45131c..03ca6de 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
@@ -9,11 +9,11 @@ socket.http.TIMEOUT = 10 | |||
9 | cache = {} | 9 | cache = {} |
10 | 10 | ||
11 | function readfile(path) | 11 | function readfile(path) |
12 | path = socket.code.unescape(path) | 12 | path = socket.url.unescape(path) |
13 | local file, error = openfile(path, "r") | 13 | local file, error = io.open(path, "r") |
14 | if file then | 14 | if file then |
15 | local body = read(file, "*a") | 15 | local body = file:read("*a") |
16 | closefile(file) | 16 | file:close() |
17 | return body | 17 | return body |
18 | else return nil, error end | 18 | else return nil, error end |
19 | end | 19 | end |
@@ -23,20 +23,14 @@ function getstatus(url) | |||
23 | if cache[url] then return cache[url] end | 23 | if cache[url] then return cache[url] end |
24 | local res | 24 | local res |
25 | if parsed.scheme == "http" then | 25 | if parsed.scheme == "http" then |
26 | local request = { url = url } | 26 | local request = { url = url, method = "HEAD" } |
27 | local response = { body_cb = function(chunk, err) | 27 | local response = socket.http.request(request) |
28 | return nil | ||
29 | end } | ||
30 | local blocksize = socket.http.BLOCKSIZE | ||
31 | socket.http.BLOCKSIZE = 1 | ||
32 | response = socket.http.request_cb(request, response) | ||
33 | socket.http.BLOCKSIZE = blocksize | ||
34 | if response.code == 200 then res = nil | 28 | if response.code == 200 then res = nil |
35 | else res = response.status or response.error end | 29 | else res = response.status or response.error end |
36 | elseif parsed.scheme == "file" then | 30 | elseif parsed.scheme == "file" then |
37 | local file, error = openfile(Code.unescape(parsed.path), "r") | 31 | local file, error = io.open(socket.url.unescape(parsed.path), "r") |
38 | if file then | 32 | if file then |
39 | closefile(file) | 33 | file:close() |
40 | res = nil | 34 | res = nil |
41 | else res = error end | 35 | else res = error end |
42 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | 36 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end |
@@ -46,15 +40,12 @@ end | |||
46 | 40 | ||
47 | function retrieve(url) | 41 | function retrieve(url) |
48 | local parsed = socket.url.parse(url, { scheme = "file" }) | 42 | local parsed = socket.url.parse(url, { scheme = "file" }) |
49 | local base, body, error | 43 | local body, headers, code, error |
50 | base = url | 44 | local base = url |
51 | if parsed.scheme == "http" then | 45 | if parsed.scheme == "http" then |
52 | local response = socket.http.request{url = url} | 46 | body, headers, code, error = socket.http.get(url) |
53 | if response.code ~= 200 then | 47 | if code == 200 then |
54 | error = response.status or response.error | 48 | base = base or headers.location |
55 | else | ||
56 | base = response.headers.location or url | ||
57 | body = response.body | ||
58 | end | 49 | end |
59 | elseif parsed.scheme == "file" then | 50 | elseif parsed.scheme == "file" then |
60 | body, error = readfile(parsed.path) | 51 | body, error = readfile(parsed.path) |
@@ -67,13 +58,13 @@ function getlinks(body, base) | |||
67 | body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") | 58 | body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") |
68 | local links = {} | 59 | local links = {} |
69 | -- extract links | 60 | -- extract links |
70 | string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) | 61 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) |
71 | table.insert(links, socket.url.absolute(base, href)) | 62 | table.insert(links, socket.url.absolute(base, href)) |
72 | end) | 63 | end) |
73 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) | 64 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) |
74 | table.insert(links, socket.url.absolute(base, href)) | 65 | table.insert(links, socket.url.absolute(base, href)) |
75 | end) | 66 | end) |
76 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href) | 67 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) |
77 | table.insert(links, socket.url.absolute(base, href)) | 68 | table.insert(links, socket.url.absolute(base, href)) |
78 | end) | 69 | end) |
79 | return links | 70 | return links |
diff --git a/etc/get.lua b/etc/get.lua index eafebda..aa29ec2 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
@@ -71,12 +71,11 @@ function stats(size) | |||
71 | io.stderr:write("\r", gauge(got, delta, size)) | 71 | io.stderr:write("\r", gauge(got, delta, size)) |
72 | io.stderr:flush() | 72 | io.stderr:flush() |
73 | end | 73 | end |
74 | return chunk | ||
75 | else | 74 | else |
76 | -- close up | 75 | -- close up |
77 | io.stderr:write("\r", gauge(got, delta), "\n") | 76 | io.stderr:write("\r", gauge(got, delta), "\n") |
78 | return "" | ||
79 | end | 77 | end |
78 | return chunk | ||
80 | end | 79 | end |
81 | end | 80 | end |
82 | 81 | ||