diff options
| author | moteus <mimir@newmail.ru> | 2013-05-27 12:45:09 +0400 |
|---|---|---|
| committer | moteus <mimir@newmail.ru> | 2013-05-27 12:45:09 +0400 |
| commit | 920bc9762954454468d056a61391635cbd849406 (patch) | |
| tree | 048c112f95fa578d239ae3e35b2aec0a482f0c23 /src/ftp.lua | |
| parent | bd51d8c1a5bb30e6a358dee6e85963f777edfff4 (diff) | |
| download | luasocket-920bc9762954454468d056a61391635cbd849406.tar.gz luasocket-920bc9762954454468d056a61391635cbd849406.tar.bz2 luasocket-920bc9762954454468d056a61391635cbd849406.zip | |
Build with Lua 5.2 without LUA_COMPAT_MODULE flag.
LUASOCKET_USE_GLOBAL flag enable create global variables when load socket/mime modules.
Diffstat (limited to 'src/ftp.lua')
| -rw-r--r-- | src/ftp.lua | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 5aa646b..714b388 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
| @@ -15,27 +15,27 @@ local socket = require("socket") | |||
| 15 | local url = require("socket.url") | 15 | local url = require("socket.url") |
| 16 | local tp = require("socket.tp") | 16 | local tp = require("socket.tp") |
| 17 | local ltn12 = require("ltn12") | 17 | local ltn12 = require("ltn12") |
| 18 | module("socket.ftp") | 18 | socket.ftp = {} |
| 19 | 19 | local _M = socket.ftp | |
| 20 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
| 21 | -- Program constants | 21 | -- Program constants |
| 22 | ----------------------------------------------------------------------------- | 22 | ----------------------------------------------------------------------------- |
| 23 | -- timeout in seconds before the program gives up on a connection | 23 | -- timeout in seconds before the program gives up on a connection |
| 24 | TIMEOUT = 60 | 24 | _M.TIMEOUT = 60 |
| 25 | -- default port for ftp service | 25 | -- default port for ftp service |
| 26 | PORT = 21 | 26 | _M.PORT = 21 |
| 27 | -- this is the default anonymous password. used when no password is | 27 | -- this is the default anonymous password. used when no password is |
| 28 | -- provided in url. should be changed to your e-mail. | 28 | -- provided in url. should be changed to your e-mail. |
| 29 | USER = "ftp" | 29 | _M.USER = "ftp" |
| 30 | PASSWORD = "anonymous@anonymous.org" | 30 | _M.PASSWORD = "anonymous@anonymous.org" |
| 31 | 31 | ||
| 32 | ----------------------------------------------------------------------------- | 32 | ----------------------------------------------------------------------------- |
| 33 | -- Low level FTP API | 33 | -- Low level FTP API |
| 34 | ----------------------------------------------------------------------------- | 34 | ----------------------------------------------------------------------------- |
| 35 | local metat = { __index = {} } | 35 | local metat = { __index = {} } |
| 36 | 36 | ||
| 37 | function open(server, port, create) | 37 | function _M.open(server, port, create) |
| 38 | local tp = socket.try(tp.connect(server, port or PORT, TIMEOUT, create)) | 38 | local tp = socket.try(tp.connect(server, port or _M.PORT, _M.TIMEOUT, create)) |
| 39 | local f = base.setmetatable({ tp = tp }, metat) | 39 | local f = base.setmetatable({ tp = tp }, metat) |
| 40 | -- make sure everything gets closed in an exception | 40 | -- make sure everything gets closed in an exception |
| 41 | f.try = socket.newtry(function() f:close() end) | 41 | f.try = socket.newtry(function() f:close() end) |
| @@ -43,22 +43,22 @@ function open(server, port, create) | |||
| 43 | end | 43 | end |
| 44 | 44 | ||
| 45 | function metat.__index:portconnect() | 45 | function metat.__index:portconnect() |
| 46 | self.try(self.server:settimeout(TIMEOUT)) | 46 | self.try(self.server:settimeout(_M.TIMEOUT)) |
| 47 | self.data = self.try(self.server:accept()) | 47 | self.data = self.try(self.server:accept()) |
| 48 | self.try(self.data:settimeout(TIMEOUT)) | 48 | self.try(self.data:settimeout(_M.TIMEOUT)) |
| 49 | end | 49 | end |
| 50 | 50 | ||
| 51 | function metat.__index:pasvconnect() | 51 | function metat.__index:pasvconnect() |
| 52 | self.data = self.try(socket.tcp()) | 52 | self.data = self.try(socket.tcp()) |
| 53 | self.try(self.data:settimeout(TIMEOUT)) | 53 | self.try(self.data:settimeout(_M.TIMEOUT)) |
| 54 | self.try(self.data:connect(self.pasvt.ip, self.pasvt.port)) | 54 | self.try(self.data:connect(self.pasvt.ip, self.pasvt.port)) |
| 55 | end | 55 | end |
| 56 | 56 | ||
| 57 | function metat.__index:login(user, password) | 57 | function metat.__index:login(user, password) |
| 58 | self.try(self.tp:command("user", user or USER)) | 58 | self.try(self.tp:command("user", user or _M.USER)) |
| 59 | local code, reply = self.try(self.tp:check{"2..", 331}) | 59 | local code, reply = self.try(self.tp:check{"2..", 331}) |
| 60 | if code == 331 then | 60 | if code == 331 then |
| 61 | self.try(self.tp:command("pass", password or PASSWORD)) | 61 | self.try(self.tp:command("pass", password or _M.PASSWORD)) |
| 62 | self.try(self.tp:check("2..")) | 62 | self.try(self.tp:check("2..")) |
| 63 | end | 63 | end |
| 64 | return 1 | 64 | return 1 |
| @@ -87,7 +87,7 @@ function metat.__index:port(ip, port) | |||
| 87 | ip, port = self.try(self.tp:getcontrol():getsockname()) | 87 | ip, port = self.try(self.tp:getcontrol():getsockname()) |
| 88 | self.server = self.try(socket.bind(ip, 0)) | 88 | self.server = self.try(socket.bind(ip, 0)) |
| 89 | ip, port = self.try(self.server:getsockname()) | 89 | ip, port = self.try(self.server:getsockname()) |
| 90 | self.try(self.server:settimeout(TIMEOUT)) | 90 | self.try(self.server:settimeout(_M.TIMEOUT)) |
| 91 | end | 91 | end |
| 92 | local pl = math.mod(port, 256) | 92 | local pl = math.mod(port, 256) |
| 93 | local ph = (port - pl)/256 | 93 | local ph = (port - pl)/256 |
| @@ -199,7 +199,7 @@ end | |||
| 199 | local function tput(putt) | 199 | local function tput(putt) |
| 200 | putt = override(putt) | 200 | putt = override(putt) |
| 201 | socket.try(putt.host, "missing hostname") | 201 | socket.try(putt.host, "missing hostname") |
| 202 | local f = open(putt.host, putt.port, putt.create) | 202 | local f = _M.open(putt.host, putt.port, putt.create) |
| 203 | f:greet() | 203 | f:greet() |
| 204 | f:login(putt.user, putt.password) | 204 | f:login(putt.user, putt.password) |
| 205 | if putt.type then f:type(putt.type) end | 205 | if putt.type then f:type(putt.type) end |
| @@ -234,7 +234,7 @@ local function sput(u, body) | |||
| 234 | return tput(putt) | 234 | return tput(putt) |
| 235 | end | 235 | end |
| 236 | 236 | ||
| 237 | put = socket.protect(function(putt, body) | 237 | _M.put = socket.protect(function(putt, body) |
| 238 | if base.type(putt) == "string" then return sput(putt, body) | 238 | if base.type(putt) == "string" then return sput(putt, body) |
| 239 | else return tput(putt) end | 239 | else return tput(putt) end |
| 240 | end) | 240 | end) |
| @@ -242,7 +242,7 @@ end) | |||
| 242 | local function tget(gett) | 242 | local function tget(gett) |
| 243 | gett = override(gett) | 243 | gett = override(gett) |
| 244 | socket.try(gett.host, "missing hostname") | 244 | socket.try(gett.host, "missing hostname") |
| 245 | local f = open(gett.host, gett.port, gett.create) | 245 | local f = _M.open(gett.host, gett.port, gett.create) |
| 246 | f:greet() | 246 | f:greet() |
| 247 | f:login(gett.user, gett.password) | 247 | f:login(gett.user, gett.password) |
| 248 | if gett.type then f:type(gett.type) end | 248 | if gett.type then f:type(gett.type) end |
| @@ -260,7 +260,7 @@ local function sget(u) | |||
| 260 | return table.concat(t) | 260 | return table.concat(t) |
| 261 | end | 261 | end |
| 262 | 262 | ||
| 263 | command = socket.protect(function(cmdt) | 263 | _M.command = socket.protect(function(cmdt) |
| 264 | cmdt = override(cmdt) | 264 | cmdt = override(cmdt) |
| 265 | socket.try(cmdt.host, "missing hostname") | 265 | socket.try(cmdt.host, "missing hostname") |
| 266 | socket.try(cmdt.command, "missing command") | 266 | socket.try(cmdt.command, "missing command") |
| @@ -273,8 +273,9 @@ command = socket.protect(function(cmdt) | |||
| 273 | return f:close() | 273 | return f:close() |
| 274 | end) | 274 | end) |
| 275 | 275 | ||
| 276 | get = socket.protect(function(gett) | 276 | _M.get = socket.protect(function(gett) |
| 277 | if base.type(gett) == "string" then return sget(gett) | 277 | if base.type(gett) == "string" then return sget(gett) |
| 278 | else return tget(gett) end | 278 | else return tget(gett) end |
| 279 | end) | 279 | end) |
| 280 | 280 | ||
| 281 | return _M \ No newline at end of file | ||
