diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-04 15:15:45 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-04 15:15:45 +0000 |
| commit | 9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (patch) | |
| tree | 8c3521366ef84f534bbec278437be7ea24e2ac1c /etc | |
| parent | 63d60223da9de60f873ca08a25dbd9512c998929 (diff) | |
| download | luasocket-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/README | 2 | ||||
| -rw-r--r-- | etc/b64.lua | 4 | ||||
| -rw-r--r-- | etc/check-links.lua | 43 | ||||
| -rw-r--r-- | etc/dict.lua | 4 | ||||
| -rw-r--r-- | etc/eol.lua | 5 | ||||
| -rw-r--r-- | etc/get.lua | 39 |
6 files changed, 48 insertions, 49 deletions
| @@ -1,7 +1,7 @@ | |||
| 1 | This directory contains code that is more useful than the examples. This code | 1 | This 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 | ||
| 6 | These are modules to suport dynamic loading of LuaSocket by the stand alone | 6 | These are modules to suport dynamic loading of LuaSocket by the stand alone |
| 7 | Lua Interpreter with the use of the "require" function. For my Mac OS X | 7 | Lua 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 @@ | |||
| 1 | require("ltn12") | 1 | local ltn12 = require("ltn12") |
| 2 | require("mime") | 2 | local mime = require("mime") |
| 3 | local source = ltn12.source.file(io.stdin) | 3 | local source = ltn12.source.file(io.stdin) |
| 4 | local sink = ltn12.sink.file(io.stdout) | 4 | local sink = ltn12.sink.file(io.stdout) |
| 5 | local convert | 5 | local 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 | 7 | local http = require("http") | |
| 8 | require"http" | 8 | local url = require("url") |
| 9 | 9 | http.TIMEOUT = 10 | |
| 10 | socket.http.TIMEOUT = 10 | ||
| 11 | 10 | ||
| 12 | cache = {} | 11 | cache = {} |
| 13 | 12 | ||
| 14 | function readfile(path) | 13 | function 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 |
| 22 | end | 21 | end |
| 23 | 22 | ||
| 24 | function getstatus(url) | 23 | function 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 |
| 42 | end | 41 | end |
| 43 | 42 | ||
| 44 | function retrieve(url) | 43 | function 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 |
| 74 | end | 73 | end |
| 75 | 74 | ||
| 76 | function checklinks(url) | 75 | function 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 | |||
| 91 | end | 90 | end |
| 92 | for _, a in ipairs(arg) do | 91 | for _, 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)) |
| 95 | end | 94 | end |
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 | ----------------------------------------------------------------------------- |
| 7 | require"socket" | 7 | local socket = require("socket") |
| 8 | 8 | ||
| 9 | function get_status(sock, valid) | 9 | function 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) |
| 79 | else | 79 | else |
| 80 | io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n") | 80 | io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n") |
| 81 | end | 81 | end |
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 @@ | |||
| 1 | require"mime.lua" | 1 | local mime = require("mime") |
| 2 | require"ltn12.lua" | 2 | local ltn12 = require("ltn12") |
| 3 | |||
| 4 | local marker = '\n' | 3 | local marker = '\n' |
| 5 | if arg and arg[1] == '-d' then marker = '\r\n' end | 4 | if arg and arg[1] == '-d' then marker = '\r\n' end |
| 6 | local filter = mime.normalize(marker) | 5 | local 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 | ----------------------------------------------------------------------------- |
| 7 | require"http" | 7 | socket = require("socket") |
| 8 | require"ftp" | 8 | http = require("http") |
| 9 | require"url" | 9 | ftp = require("ftp") |
| 10 | url = require("url") | ||
| 10 | 11 | ||
| 11 | -- formats a number of seconds into human readable form | 12 | -- formats a number of seconds into human readable form |
| 12 | function nicetime(s) | 13 | function nicetime(s) |
| @@ -84,55 +85,55 @@ function stats(size) | |||
| 84 | end | 85 | end |
| 85 | 86 | ||
| 86 | -- determines the size of a http file | 87 | -- determines the size of a http file |
| 87 | function gethttpsize(url) | 88 | function 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 |
| 92 | end | 93 | end |
| 93 | 94 | ||
| 94 | -- downloads a file using the http protocol | 95 | -- downloads a file using the http protocol |
| 95 | function getbyhttp(url, file) | 96 | function 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 |
| 101 | end | 102 | end |
| 102 | 103 | ||
| 103 | -- downloads a file using the ftp protocol | 104 | -- downloads a file using the ftp protocol |
| 104 | function getbyftp(url, file) | 105 | function 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 |
| 114 | end | 115 | end |
| 115 | 116 | ||
| 116 | -- determines the scheme | 117 | -- determines the scheme |
| 117 | function getscheme(url) | 118 | function 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 |
| 122 | end | 123 | end |
| 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> |
| 125 | function get(url, name) | 126 | function 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 |
| 131 | end | 132 | end |
| 132 | 133 | ||
| 133 | -- main program | 134 | -- main program |
| 134 | arg = arg or {} | 135 | arg = arg or {} |
| 135 | if table.getn(arg) < 1 then | 136 | if 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) |
| 138 | else get(arg[1], arg[2]) end | 139 | else get(arg[1], arg[2]) end |
