diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2011-05-25 20:57:22 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2011-05-25 20:57:22 +0000 |
commit | 3a8ba90dfb0c2eb224f317dd692ede426691e72a (patch) | |
tree | fe1cc5379a2e0e031663fe9c15d908653844bc73 /etc | |
parent | bce60be30fe8e9c1b0eb33128c23c93d7bca5303 (diff) | |
download | luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.gz luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.tar.bz2 luasocket-3a8ba90dfb0c2eb224f317dd692ede426691e72a.zip |
Saving before big changes to support IPv6.
Diffstat (limited to 'etc')
-rw-r--r-- | etc/dict.lua | 44 | ||||
-rw-r--r-- | etc/get.lua | 106 | ||||
-rw-r--r-- | etc/lp.lua | 8 | ||||
-rw-r--r-- | etc/tftp.lua | 68 |
4 files changed, 113 insertions, 113 deletions
diff --git a/etc/dict.lua b/etc/dict.lua index 5c85aae..6eb3210 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
@@ -44,48 +44,48 @@ function metat.__index:check(ok) | |||
44 | end | 44 | end |
45 | 45 | ||
46 | function metat.__index:getdef() | 46 | function metat.__index:getdef() |
47 | local line = socket.try(self.tp:receive()) | 47 | local line = socket.try(self.tp:receive()) |
48 | local def = {} | 48 | local def = {} |
49 | while line ~= "." do | 49 | while line ~= "." do |
50 | table.insert(def, line) | 50 | table.insert(def, line) |
51 | line = socket.try(self.tp:receive()) | 51 | line = socket.try(self.tp:receive()) |
52 | end | 52 | end |
53 | return table.concat(def, "\n") | 53 | return table.concat(def, "\n") |
54 | end | 54 | end |
55 | 55 | ||
56 | function metat.__index:define(database, word) | 56 | function metat.__index:define(database, word) |
57 | database = database or "!" | 57 | database = database or "!" |
58 | socket.try(self.tp:command("DEFINE", database .. " " .. word)) | 58 | socket.try(self.tp:command("DEFINE", database .. " " .. word)) |
59 | local code, count = self:check(150) | 59 | local code, count = self:check(150) |
60 | local defs = {} | 60 | local defs = {} |
61 | for i = 1, count do | 61 | for i = 1, count do |
62 | self:check(151) | 62 | self:check(151) |
63 | table.insert(defs, self:getdef()) | 63 | table.insert(defs, self:getdef()) |
64 | end | 64 | end |
65 | self:check(250) | 65 | self:check(250) |
66 | return defs | 66 | return defs |
67 | end | 67 | end |
68 | 68 | ||
69 | function metat.__index:match(database, strat, word) | 69 | function metat.__index:match(database, strat, word) |
70 | database = database or "!" | 70 | database = database or "!" |
71 | strat = strat or "." | 71 | strat = strat or "." |
72 | socket.try(self.tp:command("MATCH", database .." ".. strat .." ".. word)) | 72 | socket.try(self.tp:command("MATCH", database .." ".. strat .." ".. word)) |
73 | self:check(152) | 73 | self:check(152) |
74 | local mat = {} | 74 | local mat = {} |
75 | local line = socket.try(self.tp:receive()) | 75 | local line = socket.try(self.tp:receive()) |
76 | while line ~= '.' do | 76 | while line ~= '.' do |
77 | database, word = socket.skip(2, string.find(line, "(%S+) (.*)")) | 77 | database, word = socket.skip(2, string.find(line, "(%S+) (.*)")) |
78 | if not mat[database] then mat[database] = {} end | 78 | if not mat[database] then mat[database] = {} end |
79 | table.insert(mat[database], word) | 79 | table.insert(mat[database], word) |
80 | line = socket.try(self.tp:receive()) | 80 | line = socket.try(self.tp:receive()) |
81 | end | 81 | end |
82 | self:check(250) | 82 | self:check(250) |
83 | return mat | 83 | return mat |
84 | end | 84 | end |
85 | 85 | ||
86 | function metat.__index:quit() | 86 | function metat.__index:quit() |
87 | self.tp:command("QUIT") | 87 | self.tp:command("QUIT") |
88 | return self:check(221) | 88 | return self:check(221) |
89 | end | 89 | end |
90 | 90 | ||
91 | function metat.__index:close() | 91 | function metat.__index:close() |
diff --git a/etc/get.lua b/etc/get.lua index 09f639d..0d631c2 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
@@ -12,53 +12,53 @@ local ltn12 = require("ltn12") | |||
12 | 12 | ||
13 | -- formats a number of seconds into human readable form | 13 | -- formats a number of seconds into human readable form |
14 | function nicetime(s) | 14 | function nicetime(s) |
15 | local l = "s" | 15 | local l = "s" |
16 | if s > 60 then | 16 | if s > 60 then |
17 | s = s / 60 | 17 | s = s / 60 |
18 | l = "m" | 18 | l = "m" |
19 | if s > 60 then | 19 | if s > 60 then |
20 | s = s / 60 | 20 | s = s / 60 |
21 | l = "h" | 21 | l = "h" |
22 | if s > 24 then | 22 | if s > 24 then |
23 | s = s / 24 | 23 | s = s / 24 |
24 | l = "d" -- hmmm | 24 | l = "d" -- hmmm |
25 | end | 25 | end |
26 | end | 26 | end |
27 | end | 27 | end |
28 | if l == "s" then return string.format("%5.0f%s", s, l) | 28 | if l == "s" then return string.format("%5.0f%s", s, l) |
29 | else return string.format("%5.2f%s", s, l) end | 29 | else return string.format("%5.2f%s", s, l) end |
30 | end | 30 | end |
31 | 31 | ||
32 | -- formats a number of bytes into human readable form | 32 | -- formats a number of bytes into human readable form |
33 | function nicesize(b) | 33 | function nicesize(b) |
34 | local l = "B" | 34 | local l = "B" |
35 | if b > 1024 then | 35 | if b > 1024 then |
36 | b = b / 1024 | 36 | b = b / 1024 |
37 | l = "KB" | 37 | l = "KB" |
38 | if b > 1024 then | 38 | if b > 1024 then |
39 | b = b / 1024 | 39 | b = b / 1024 |
40 | l = "MB" | 40 | l = "MB" |
41 | if b > 1024 then | 41 | if b > 1024 then |
42 | b = b / 1024 | 42 | b = b / 1024 |
43 | l = "GB" -- hmmm | 43 | l = "GB" -- hmmm |
44 | end | 44 | end |
45 | end | 45 | end |
46 | end | 46 | end |
47 | return string.format("%7.2f%2s", b, l) | 47 | return string.format("%7.2f%2s", b, l) |
48 | end | 48 | end |
49 | 49 | ||
50 | -- returns a string with the current state of the download | 50 | -- returns a string with the current state of the download |
51 | local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining" | 51 | local remaining_s = "%s received, %s/s throughput, %2.0f%% done, %s remaining" |
52 | local elapsed_s = "%s received, %s/s throughput, %s elapsed " | 52 | local elapsed_s = "%s received, %s/s throughput, %s elapsed " |
53 | function gauge(got, delta, size) | 53 | function gauge(got, delta, size) |
54 | local rate = got / delta | 54 | local rate = got / delta |
55 | if size and size >= 1 then | 55 | if size and size >= 1 then |
56 | return string.format(remaining_s, nicesize(got), nicesize(rate), | 56 | return string.format(remaining_s, nicesize(got), nicesize(rate), |
57 | 100*got/size, nicetime((size-got)/rate)) | 57 | 100*got/size, nicetime((size-got)/rate)) |
58 | else | 58 | else |
59 | return string.format(elapsed_s, nicesize(got), | 59 | return string.format(elapsed_s, nicesize(got), |
60 | nicesize(rate), nicetime(delta)) | 60 | nicesize(rate), nicetime(delta)) |
61 | end | 61 | end |
62 | end | 62 | end |
63 | 63 | ||
64 | -- creates a new instance of a receive_cb that saves to disk | 64 | -- creates a new instance of a receive_cb that saves to disk |
@@ -89,10 +89,10 @@ end | |||
89 | 89 | ||
90 | -- determines the size of a http file | 90 | -- determines the size of a http file |
91 | function gethttpsize(u) | 91 | function gethttpsize(u) |
92 | local r, c, h = http.request {method = "HEAD", url = u} | 92 | local r, c, h = http.request {method = "HEAD", url = u} |
93 | if c == 200 then | 93 | if c == 200 then |
94 | return tonumber(h["content-length"]) | 94 | return tonumber(h["content-length"]) |
95 | end | 95 | end |
96 | end | 96 | end |
97 | 97 | ||
98 | -- downloads a file using the http protocol | 98 | -- downloads a file using the http protocol |
@@ -101,7 +101,7 @@ function getbyhttp(u, file) | |||
101 | -- only print feedback if output is not stdout | 101 | -- only print feedback if output is not stdout |
102 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end | 102 | if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end |
103 | local r, c, h, s = http.request {url = u, sink = save } | 103 | local r, c, h, s = http.request {url = u, sink = save } |
104 | if c ~= 200 then io.stderr:write(s or c, "\n") end | 104 | if c ~= 200 then io.stderr:write(s or c, "\n") end |
105 | end | 105 | end |
106 | 106 | ||
107 | -- downloads a file using the ftp protocol | 107 | -- downloads a file using the ftp protocol |
@@ -114,29 +114,29 @@ function getbyftp(u, file) | |||
114 | gett.sink = save | 114 | gett.sink = save |
115 | gett.type = "i" | 115 | gett.type = "i" |
116 | local ret, err = ftp.get(gett) | 116 | local ret, err = ftp.get(gett) |
117 | if err then print(err) end | 117 | if err then print(err) end |
118 | end | 118 | end |
119 | 119 | ||
120 | -- determines the scheme | 120 | -- determines the scheme |
121 | function getscheme(u) | 121 | function getscheme(u) |
122 | -- this is an heuristic to solve a common invalid url poblem | 122 | -- this is an heuristic to solve a common invalid url poblem |
123 | if not string.find(u, "//") then u = "//" .. u end | 123 | if not string.find(u, "//") then u = "//" .. u end |
124 | local parsed = url.parse(u, {scheme = "http"}) | 124 | local parsed = url.parse(u, {scheme = "http"}) |
125 | return parsed.scheme | 125 | return parsed.scheme |
126 | end | 126 | end |
127 | 127 | ||
128 | -- gets a file either by http or ftp, saving as <name> | 128 | -- gets a file either by http or ftp, saving as <name> |
129 | function get(u, name) | 129 | function get(u, name) |
130 | local fout = name and io.open(name, "wb") | 130 | local fout = name and io.open(name, "wb") |
131 | local scheme = getscheme(u) | 131 | local scheme = getscheme(u) |
132 | if scheme == "ftp" then getbyftp(u, fout) | 132 | if scheme == "ftp" then getbyftp(u, fout) |
133 | elseif scheme == "http" then getbyhttp(u, fout) | 133 | elseif scheme == "http" then getbyhttp(u, fout) |
134 | else print("unknown scheme" .. scheme) end | 134 | else print("unknown scheme" .. scheme) end |
135 | end | 135 | end |
136 | 136 | ||
137 | -- main program | 137 | -- main program |
138 | arg = arg or {} | 138 | arg = arg or {} |
139 | if table.getn(arg) < 1 then | 139 | if table.getn(arg) < 1 then |
140 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") | 140 | io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n") |
141 | os.exit(1) | 141 | os.exit(1) |
142 | else get(arg[1], arg[2]) end | 142 | else get(arg[1], arg[2]) end |
@@ -268,11 +268,11 @@ send = socket.protect(function(option) | |||
268 | local class = string.sub(option.class or localip or localhost,1,31) | 268 | local class = string.sub(option.class or localip or localhost,1,31) |
269 | local _,_,ctlfn = string.find(file,".*[%/%\\](.*)") | 269 | local _,_,ctlfn = string.find(file,".*[%/%\\](.*)") |
270 | ctlfn = string.sub(ctlfn or file,1,131) | 270 | ctlfn = string.sub(ctlfn or file,1,131) |
271 | local cfile = | 271 | local cfile = |
272 | string.format("H%-s\nC%-s\nJ%-s\nP%-s\n%.1s%-s\nU%-s\nN%-s\n", | 272 | string.format("H%-s\nC%-s\nJ%-s\nP%-s\n%.1s%-s\nU%-s\nN%-s\n", |
273 | localhost, | 273 | localhost, |
274 | class, | 274 | class, |
275 | option.job or "LuaSocket", | 275 | option.job or "LuaSocket", |
276 | user, | 276 | user, |
277 | fmt, lpfile, | 277 | fmt, lpfile, |
278 | lpfile, | 278 | lpfile, |
diff --git a/etc/tftp.lua b/etc/tftp.lua index c028b20..4051e74 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua | |||
@@ -35,18 +35,18 @@ local OP_INV = {"RRQ", "WRQ", "DATA", "ACK", "ERROR"} | |||
35 | -- Packet creation functions | 35 | -- Packet creation functions |
36 | ----------------------------------------------------------------------------- | 36 | ----------------------------------------------------------------------------- |
37 | local function RRQ(source, mode) | 37 | local function RRQ(source, mode) |
38 | return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0) | 38 | return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0) |
39 | end | 39 | end |
40 | 40 | ||
41 | local function WRQ(source, mode) | 41 | local function WRQ(source, mode) |
42 | return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0) | 42 | return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0) |
43 | end | 43 | end |
44 | 44 | ||
45 | local function ACK(block) | 45 | local function ACK(block) |
46 | local low, high | 46 | local low, high |
47 | low = math.mod(block, 256) | 47 | low = math.mod(block, 256) |
48 | high = (block - low)/256 | 48 | high = (block - low)/256 |
49 | return char(0, OP_ACK, high, low) | 49 | return char(0, OP_ACK, high, low) |
50 | end | 50 | end |
51 | 51 | ||
52 | local function get_OP(dgram) | 52 | local function get_OP(dgram) |
@@ -58,16 +58,16 @@ end | |||
58 | -- Packet analysis functions | 58 | -- Packet analysis functions |
59 | ----------------------------------------------------------------------------- | 59 | ----------------------------------------------------------------------------- |
60 | local function split_DATA(dgram) | 60 | local function split_DATA(dgram) |
61 | local block = byte(dgram, 3)*256 + byte(dgram, 4) | 61 | local block = byte(dgram, 3)*256 + byte(dgram, 4) |
62 | local data = string.sub(dgram, 5) | 62 | local data = string.sub(dgram, 5) |
63 | return block, data | 63 | return block, data |
64 | end | 64 | end |
65 | 65 | ||
66 | local function get_ERROR(dgram) | 66 | local function get_ERROR(dgram) |
67 | local code = byte(dgram, 3)*256 + byte(dgram, 4) | 67 | local code = byte(dgram, 3)*256 + byte(dgram, 4) |
68 | local msg | 68 | local msg |
69 | _,_, msg = string.find(dgram, "(.*)\000", 5) | 69 | _,_, msg = string.find(dgram, "(.*)\000", 5) |
70 | return string.format("error code %d: %s", code, msg) | 70 | return string.format("error code %d: %s", code, msg) |
71 | end | 71 | end |
72 | 72 | ||
73 | ----------------------------------------------------------------------------- | 73 | ----------------------------------------------------------------------------- |
@@ -77,40 +77,40 @@ local function tget(gett) | |||
77 | local retries, dgram, sent, datahost, dataport, code | 77 | local retries, dgram, sent, datahost, dataport, code |
78 | local last = 0 | 78 | local last = 0 |
79 | socket.try(gett.host, "missing host") | 79 | socket.try(gett.host, "missing host") |
80 | local con = socket.try(socket.udp()) | 80 | local con = socket.try(socket.udp()) |
81 | local try = socket.newtry(function() con:close() end) | 81 | local try = socket.newtry(function() con:close() end) |
82 | -- convert from name to ip if needed | 82 | -- convert from name to ip if needed |
83 | gett.host = try(socket.dns.toip(gett.host)) | 83 | gett.host = try(socket.dns.toip(gett.host)) |
84 | con:settimeout(1) | 84 | con:settimeout(1) |
85 | -- first packet gives data host/port to be used for data transfers | 85 | -- first packet gives data host/port to be used for data transfers |
86 | local path = string.gsub(gett.path or "", "^/", "") | 86 | local path = string.gsub(gett.path or "", "^/", "") |
87 | path = url.unescape(path) | 87 | path = url.unescape(path) |
88 | retries = 0 | 88 | retries = 0 |
89 | repeat | 89 | repeat |
90 | sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port)) | 90 | sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port)) |
91 | dgram, datahost, dataport = con:receivefrom() | 91 | dgram, datahost, dataport = con:receivefrom() |
92 | retries = retries + 1 | 92 | retries = retries + 1 |
93 | until dgram or datahost ~= "timeout" or retries > 5 | 93 | until dgram or datahost ~= "timeout" or retries > 5 |
94 | try(dgram, datahost) | 94 | try(dgram, datahost) |
95 | -- associate socket with data host/port | 95 | -- associate socket with data host/port |
96 | try(con:setpeername(datahost, dataport)) | 96 | try(con:setpeername(datahost, dataport)) |
97 | -- default sink | 97 | -- default sink |
98 | local sink = gett.sink or ltn12.sink.null() | 98 | local sink = gett.sink or ltn12.sink.null() |
99 | -- process all data packets | 99 | -- process all data packets |
100 | while 1 do | 100 | while 1 do |
101 | -- decode packet | 101 | -- decode packet |
102 | code = get_OP(dgram) | 102 | code = get_OP(dgram) |
103 | try(code ~= OP_ERROR, get_ERROR(dgram)) | 103 | try(code ~= OP_ERROR, get_ERROR(dgram)) |
104 | try(code == OP_DATA, "unhandled opcode " .. code) | 104 | try(code == OP_DATA, "unhandled opcode " .. code) |
105 | -- get data packet parts | 105 | -- get data packet parts |
106 | local block, data = split_DATA(dgram) | 106 | local block, data = split_DATA(dgram) |
107 | -- if not repeated, write | 107 | -- if not repeated, write |
108 | if block == last+1 then | 108 | if block == last+1 then |
109 | try(sink(data)) | 109 | try(sink(data)) |
110 | last = block | 110 | last = block |
111 | end | 111 | end |
112 | -- last packet brings less than 512 bytes of data | 112 | -- last packet brings less than 512 bytes of data |
113 | if string.len(data) < 512 then | 113 | if string.len(data) < 512 then |
114 | try(con:send(ACK(block))) | 114 | try(con:send(ACK(block))) |
115 | try(con:close()) | 115 | try(con:close()) |
116 | try(sink(nil)) | 116 | try(sink(nil)) |
@@ -118,13 +118,13 @@ local function tget(gett) | |||
118 | end | 118 | end |
119 | -- get the next packet | 119 | -- get the next packet |
120 | retries = 0 | 120 | retries = 0 |
121 | repeat | 121 | repeat |
122 | sent = try(con:send(ACK(last))) | 122 | sent = try(con:send(ACK(last))) |
123 | dgram, err = con:receive() | 123 | dgram, err = con:receive() |
124 | retries = retries + 1 | 124 | retries = retries + 1 |
125 | until dgram or err ~= "timeout" or retries > 5 | 125 | until dgram or err ~= "timeout" or retries > 5 |
126 | try(dgram, err) | 126 | try(dgram, err) |
127 | end | 127 | end |
128 | end | 128 | end |
129 | 129 | ||
130 | local default = { | 130 | local default = { |