aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-04 15:15:45 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-04 15:15:45 +0000
commit9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (patch)
tree8c3521366ef84f534bbec278437be7ea24e2ac1c /etc
parent63d60223da9de60f873ca08a25dbd9512c998929 (diff)
downloadluasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.gz
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.tar.bz2
luasocket-9ed7f955e5fc69af9bf1794fa2c8cd227981ba24.zip
Só pra não perder se der merda.
Diffstat (limited to 'etc')
-rw-r--r--etc/README2
-rw-r--r--etc/b64.lua4
-rw-r--r--etc/check-links.lua43
-rw-r--r--etc/dict.lua4
-rw-r--r--etc/eol.lua5
-rw-r--r--etc/get.lua39
6 files changed, 48 insertions, 49 deletions
diff --git a/etc/README b/etc/README
index 623adf7..eacb262 100644
--- a/etc/README
+++ b/etc/README
@@ -1,7 +1,7 @@
1This directory contains code that is more useful than the examples. This code 1This directory contains code that is more useful than the examples. This code
2*is* supported. 2*is* supported.
3 3
4 lua.lua and luasocket.lua 4 lua.lua
5 5
6These are modules to suport dynamic loading of LuaSocket by the stand alone 6These are modules to suport dynamic loading of LuaSocket by the stand alone
7Lua Interpreter with the use of the "require" function. For my Mac OS X 7Lua Interpreter with the use of the "require" function. For my Mac OS X
diff --git a/etc/b64.lua b/etc/b64.lua
index b86b870..4d5f83e 100644
--- a/etc/b64.lua
+++ b/etc/b64.lua
@@ -1,5 +1,5 @@
1require("ltn12") 1local ltn12 = require("ltn12")
2require("mime") 2local mime = require("mime")
3local source = ltn12.source.file(io.stdin) 3local source = ltn12.source.file(io.stdin)
4local sink = ltn12.sink.file(io.stdout) 4local sink = ltn12.sink.file(io.stdout)
5local convert 5local convert
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 7local http = require("http")
8require"http" 8local url = require("url")
9 9http.TIMEOUT = 10
10socket.http.TIMEOUT = 10
11 10
12cache = {} 11cache = {}
13 12
14function readfile(path) 13function 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
22end 21end
23 22
24function getstatus(url) 23function 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
42end 41end
43 42
44function retrieve(url) 43function 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
74end 73end
75 74
76function checklinks(url) 75function 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
91end 90end
92for _, a in ipairs(arg) do 91for _, 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))
95end 94end
diff --git a/etc/dict.lua b/etc/dict.lua
index e5d4740..dd001cf 100644
--- a/etc/dict.lua
+++ b/etc/dict.lua
@@ -4,7 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"socket" 7local socket = require("socket")
8 8
9function get_status(sock, valid) 9function get_status(sock, valid)
10 local line, err = sock:receive() 10 local line, err = sock:receive()
@@ -77,5 +77,5 @@ if arg and arg[1] then
77 defs, err = dict_get(arg[1], arg[2]) 77 defs, err = dict_get(arg[1], arg[2])
78 print(defs or err) 78 print(defs or err)
79else 79else
80 io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n") 80 io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n")
81end 81end
diff --git a/etc/eol.lua b/etc/eol.lua
index b13684b..d3da776 100644
--- a/etc/eol.lua
+++ b/etc/eol.lua
@@ -1,6 +1,5 @@
1require"mime.lua" 1local mime = require("mime")
2require"ltn12.lua" 2local ltn12 = require("ltn12")
3
4local marker = '\n' 3local marker = '\n'
5if arg and arg[1] == '-d' then marker = '\r\n' end 4if arg and arg[1] == '-d' then marker = '\r\n' end
6local filter = mime.normalize(marker) 5local filter = mime.normalize(marker)
diff --git a/etc/get.lua b/etc/get.lua
index 35de7d7..c1e0542 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -4,9 +4,10 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"http" 7socket = require("socket")
8require"ftp" 8http = require("http")
9require"url" 9ftp = require("ftp")
10url = require("url")
10 11
11-- formats a number of seconds into human readable form 12-- formats a number of seconds into human readable form
12function nicetime(s) 13function nicetime(s)
@@ -84,55 +85,55 @@ function stats(size)
84end 85end
85 86
86-- determines the size of a http file 87-- determines the size of a http file
87function gethttpsize(url) 88function gethttpsize(u)
88 local respt = socket.http.request {method = "HEAD", url = url} 89 local respt = http.request {method = "HEAD", url = u}
89 if respt.code == 200 then 90 if respt.code == 200 then
90 return tonumber(respt.headers["content-length"]) 91 return tonumber(respt.headers["content-length"])
91 end 92 end
92end 93end
93 94
94-- downloads a file using the http protocol 95-- downloads a file using the http protocol
95function getbyhttp(url, file) 96function getbyhttp(u, file)
96 local save = ltn12.sink.file(file or io.stdout) 97 local save = ltn12.sink.file(file or io.stdout)
97 -- only print feedback if output is not stdout 98 -- only print feedback if output is not stdout
98 if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end 99 if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
99 local respt = socket.http.request {url = url, sink = save } 100 local respt = http.request {url = u, sink = save }
100 if respt.code ~= 200 then print(respt.status or respt.error) end 101 if respt.code ~= 200 then print(respt.status or respt.error) end
101end 102end
102 103
103-- downloads a file using the ftp protocol 104-- downloads a file using the ftp protocol
104function getbyftp(url, file) 105function getbyftp(u, file)
105 local save = ltn12.sink.file(file or io.stdout) 106 local save = ltn12.sink.file(file or io.stdout)
106 -- only print feedback if output is not stdout 107 -- only print feedback if output is not stdout
107 -- and we don't know how big the file is 108 -- and we don't know how big the file is
108 if file then save = ltn12.sink.chain(stats(), save) end 109 if file then save = ltn12.sink.chain(stats(), save) end
109 local gett = socket.url.parse(url) 110 local gett = url.parse(u)
110 gett.sink = save 111 gett.sink = save
111 gett.type = "i" 112 gett.type = "i"
112 local ret, err = socket.ftp.get(gett) 113 local ret, err = ftp.get(gett)
113 if err then print(err) end 114 if err then print(err) end
114end 115end
115 116
116-- determines the scheme 117-- determines the scheme
117function getscheme(url) 118function getscheme(u)
118 -- this is an heuristic to solve a common invalid url poblem 119 -- this is an heuristic to solve a common invalid url poblem
119 if not string.find(url, "//") then url = "//" .. url end 120 if not string.find(u, "//") then u = "//" .. u end
120 local parsed = socket.url.parse(url, {scheme = "http"}) 121 local parsed = url.parse(u, {scheme = "http"})
121 return parsed.scheme 122 return parsed.scheme
122end 123end
123 124
124-- gets a file either by http or ftp, saving as <name> 125-- gets a file either by http or ftp, saving as <name>
125function get(url, name) 126function get(u, name)
126 local fout = name and io.open(name, "wb") 127 local fout = name and io.open(name, "wb")
127 local scheme = getscheme(url) 128 local scheme = getscheme(u)
128 if scheme == "ftp" then getbyftp(url, fout) 129 if scheme == "ftp" then getbyftp(u, fout)
129 elseif scheme == "http" then getbyhttp(url, fout) 130 elseif scheme == "http" then getbyhttp(u, fout)
130 else print("unknown scheme" .. scheme) end 131 else print("unknown scheme" .. scheme) end
131end 132end
132 133
133-- main program 134-- main program
134arg = arg or {} 135arg = arg or {}
135if table.getn(arg) < 1 then 136if table.getn(arg) < 1 then
136 io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n") 137 io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
137 os.exit(1) 138 os.exit(1)
138else get(arg[1], arg[2]) end 139else get(arg[1], arg[2]) end