aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-16 22:51:04 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-16 22:51:04 +0000
commit9fc682a106f13901b60a665619fc61d9dae49fb0 (patch)
tree6efdd73fa4f2abce56b6fb710f7516d0a5c6c758 /etc
parent574708380f19b15bd19419bfd64ccbe422f2d924 (diff)
downloadluasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.tar.gz
luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.tar.bz2
luasocket-9fc682a106f13901b60a665619fc61d9dae49fb0.zip
HTTP now has only one function.
Diffstat (limited to 'etc')
-rw-r--r--etc/check-links.lua25
-rw-r--r--etc/get.lua10
2 files changed, 14 insertions, 21 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")
8local url = require("url") 8local url = require("url")
9http.TIMEOUT = 10 9http.TIMEOUT = 10
10 10
11cache = {}
12
13function readfile(path) 11function 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
23function getstatus(u) 21function 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
41end 31end
42 32
43function retrieve(u) 33function 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
diff --git a/etc/get.lua b/etc/get.lua
index 0603ce5..c7e2a43 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -87,9 +87,9 @@ end
87 87
88-- determines the size of a http file 88-- determines the size of a http file
89function gethttpsize(u) 89function gethttpsize(u)
90 local respt = http.request {method = "HEAD", url = u} 90 local r, c, h = http.request {method = "HEAD", url = u}
91 if respt.code == 200 then 91 if c == 200 then
92 return tonumber(respt.headers["content-length"]) 92 return tonumber(h["content-length"])
93 end 93 end
94end 94end
95 95
@@ -98,8 +98,8 @@ function getbyhttp(u, file)
98 local save = ltn12.sink.file(file or io.stdout) 98 local save = ltn12.sink.file(file or io.stdout)
99 -- only print feedback if output is not stdout 99 -- only print feedback if output is not stdout
100 if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end 100 if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
101 local respt = http.request {url = u, sink = save } 101 local r, c, h, s = http.request {url = u, sink = save }
102 if respt.code ~= 200 then print(respt.status or respt.error) end 102 if c ~= 200 then io.stderr:write(s or c, "\n") end
103end 103end
104 104
105-- downloads a file using the ftp protocol 105-- downloads a file using the ftp protocol