diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ftp.lua | 51 | ||||
| -rw-r--r-- | src/http.lua | 10 | ||||
| -rw-r--r-- | src/mbox.lua | 42 | ||||
| -rw-r--r-- | src/smtp.lua | 21 | ||||
| -rw-r--r-- | src/url.lua | 18 |
5 files changed, 72 insertions, 70 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 1fa48f7..f6fffbb 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
| 9 | 9 | ||
| 10 | local Public, Private = {}, {} | 10 | local Public, Private = {}, {} |
| 11 | FTP = Public | 11 | socket.ftp = Public |
| 12 | 12 | ||
| 13 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
| 14 | -- Program constants | 14 | -- Program constants |
| @@ -47,7 +47,7 @@ end | |||
| 47 | ----------------------------------------------------------------------------- | 47 | ----------------------------------------------------------------------------- |
| 48 | function Private.try_receive(...) | 48 | function Private.try_receive(...) |
| 49 | local sock = arg[1] | 49 | local sock = arg[1] |
| 50 | local data, err = call(sock.receive, arg) | 50 | local data, err = sock.receive(unpack(arg)) |
| 51 | if err then sock:close() end | 51 | if err then sock:close() end |
| 52 | return data, err | 52 | return data, err |
| 53 | end | 53 | end |
| @@ -64,9 +64,9 @@ function Private.get_pasv(pasv) | |||
| 64 | local a, b, c, d, p1, p2, _ | 64 | local a, b, c, d, p1, p2, _ |
| 65 | local ip, port | 65 | local ip, port |
| 66 | _,_, a, b, c, d, p1, p2 = | 66 | _,_, a, b, c, d, p1, p2 = |
| 67 | strfind(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)") | 67 | string.find(pasv, "(%d*),(%d*),(%d*),(%d*),(%d*),(%d*)") |
| 68 | if not (a and b and c and d and p1 and p2) then return nil, nil end | 68 | if not (a and b and c and d and p1 and p2) then return nil, nil end |
| 69 | ip = format("%d.%d.%d.%d", a, b, c, d) | 69 | ip = string.format("%d.%d.%d.%d", a, b, c, d) |
| 70 | port = tonumber(p1)*256 + tonumber(p2) | 70 | port = tonumber(p1)*256 + tonumber(p2) |
| 71 | return ip, port | 71 | return ip, port |
| 72 | end | 72 | end |
| @@ -100,13 +100,13 @@ function Private.get_answer(control) | |||
| 100 | local line, err = Private.try_receive(control) | 100 | local line, err = Private.try_receive(control) |
| 101 | local answer = line | 101 | local answer = line |
| 102 | if err then return nil, err end | 102 | if err then return nil, err end |
| 103 | _,_, code, sep = strfind(line, "^(%d%d%d)(.)") | 103 | _,_, code, sep = string.find(line, "^(%d%d%d)(.)") |
| 104 | if not code or not sep then return nil, answer end | 104 | if not code or not sep then return nil, answer end |
| 105 | if sep == "-" then -- answer is multiline | 105 | if sep == "-" then -- answer is multiline |
| 106 | repeat | 106 | repeat |
| 107 | line, err = Private.try_receive(control) | 107 | line, err = Private.try_receive(control) |
| 108 | if err then return nil, err end | 108 | if err then return nil, err end |
| 109 | _,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)") | 109 | _,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)") |
| 110 | answer = answer .. "\n" .. line | 110 | answer = answer .. "\n" .. line |
| 111 | until code == lastcode and sep == " " -- answer ends with same code | 111 | until code == lastcode and sep == " " -- answer ends with same code |
| 112 | end | 112 | end |
| @@ -126,8 +126,8 @@ function Private.check_answer(control, success) | |||
| 126 | local answer, code = Private.get_answer(control) | 126 | local answer, code = Private.get_answer(control) |
| 127 | if not answer then return nil, code end | 127 | if not answer then return nil, code end |
| 128 | if type(success) ~= "table" then success = {success} end | 128 | if type(success) ~= "table" then success = {success} end |
| 129 | for i = 1, getn(success) do | 129 | for _, s in ipairs(success) do |
| 130 | if code == success[i] then | 130 | if code == s then |
| 131 | return code, answer | 131 | return code, answer |
| 132 | end | 132 | end |
| 133 | end | 133 | end |
| @@ -213,13 +213,13 @@ function Private.port(control) | |||
| 213 | local code, answer | 213 | local code, answer |
| 214 | local server, ctl_ip | 214 | local server, ctl_ip |
| 215 | ctl_ip, answer = control:getsockname() | 215 | ctl_ip, answer = control:getsockname() |
| 216 | server, answer = bind(ctl_ip, 0) | 216 | server, answer = socket.bind(ctl_ip, 0) |
| 217 | server:timeout(Public.TIMEOUT) | 217 | server:timeout(Public.TIMEOUT) |
| 218 | local ip, p, ph, pl | 218 | local ip, p, ph, pl |
| 219 | ip, p = server:getsockname() | 219 | ip, p = server:getsockname() |
| 220 | pl = mod(p, 256) | 220 | pl = math.mod(p, 256) |
| 221 | ph = (p - pl)/256 | 221 | ph = (p - pl)/256 |
| 222 | local arg = gsub(format("%s,%d,%d", ip, ph, pl), "%.", ",") | 222 | local arg = string.gsub(string.format("%s,%d,%d", ip, ph, pl), "%.", ",") |
| 223 | code, answer = Private.command(control, "port", arg, {200}) | 223 | code, answer = Private.command(control, "port", arg, {200}) |
| 224 | if not code then | 224 | if not code then |
| 225 | server:close() | 225 | server:close() |
| @@ -321,7 +321,7 @@ function Private.send_indirect(data, send_cb, chunk, size) | |||
| 321 | data:close() | 321 | data:close() |
| 322 | return err | 322 | return err |
| 323 | end | 323 | end |
| 324 | sent = sent + strlen(chunk) | 324 | sent = sent + string.len(chunk) |
| 325 | if sent >= size then break end | 325 | if sent >= size then break end |
| 326 | chunk, size = send_cb() | 326 | chunk, size = send_cb() |
| 327 | end | 327 | end |
| @@ -374,7 +374,7 @@ end | |||
| 374 | ----------------------------------------------------------------------------- | 374 | ----------------------------------------------------------------------------- |
| 375 | function Private.change_type(control, params) | 375 | function Private.change_type(control, params) |
| 376 | local type, _ | 376 | local type, _ |
| 377 | _, _, type = strfind(params or "", "type=(.)") | 377 | _, _, type = string.find(params or "", "type=(.)") |
| 378 | if type == "a" or type == "i" then | 378 | if type == "a" or type == "i" then |
| 379 | local code, err = Private.command(control, "type", type, {200}) | 379 | local code, err = Private.command(control, "type", type, {200}) |
| 380 | if not code then return err end | 380 | if not code then return err end |
| @@ -391,7 +391,7 @@ end | |||
| 391 | ----------------------------------------------------------------------------- | 391 | ----------------------------------------------------------------------------- |
| 392 | function Private.open(parsed) | 392 | function Private.open(parsed) |
| 393 | -- start control connection | 393 | -- start control connection |
| 394 | local control, err = connect(parsed.host, parsed.port) | 394 | local control, err = socket.connect(parsed.host, parsed.port) |
| 395 | if not control then return nil, err end | 395 | if not control then return nil, err end |
| 396 | -- make sure we don't block forever | 396 | -- make sure we don't block forever |
| 397 | control:timeout(Public.TIMEOUT) | 397 | control:timeout(Public.TIMEOUT) |
| @@ -423,7 +423,7 @@ end | |||
| 423 | -- err: error message if any | 423 | -- err: error message if any |
| 424 | ----------------------------------------------------------------------------- | 424 | ----------------------------------------------------------------------------- |
| 425 | function Private.change_dir(control, segment) | 425 | function Private.change_dir(control, segment) |
| 426 | local n = getn(segment) | 426 | local n = table.getn(segment) |
| 427 | for i = 1, n-1 do | 427 | for i = 1, n-1 do |
| 428 | local code, answer = Private.cwd(control, segment[i]) | 428 | local code, answer = Private.cwd(control, segment[i]) |
| 429 | if not code then return answer end | 429 | if not code then return answer end |
| @@ -443,7 +443,7 @@ end | |||
| 443 | function Private.upload(control, request, segment) | 443 | function Private.upload(control, request, segment) |
| 444 | local code, name, content_cb | 444 | local code, name, content_cb |
| 445 | -- get remote file name | 445 | -- get remote file name |
| 446 | name = segment[getn(segment)] | 446 | name = segment[table.getn(segment)] |
| 447 | if not name then | 447 | if not name then |
| 448 | control:close() | 448 | control:close() |
| 449 | return "Invalid file path" | 449 | return "Invalid file path" |
| @@ -472,7 +472,7 @@ function Private.download(control, request, segment) | |||
| 472 | is_directory = segment.is_directory | 472 | is_directory = segment.is_directory |
| 473 | content_cb = request.content_cb | 473 | content_cb = request.content_cb |
| 474 | -- get remote file name | 474 | -- get remote file name |
| 475 | name = segment[getn(segment)] | 475 | name = segment[table.getn(segment)] |
| 476 | if not name and not is_directory then | 476 | if not name and not is_directory then |
| 477 | control:close() | 477 | control:close() |
| 478 | return "Invalid file path" | 478 | return "Invalid file path" |
| @@ -498,7 +498,7 @@ end | |||
| 498 | -- parsed: a table with parsed components | 498 | -- parsed: a table with parsed components |
| 499 | ----------------------------------------------------------------------------- | 499 | ----------------------------------------------------------------------------- |
| 500 | function Private.parse_url(request) | 500 | function Private.parse_url(request) |
| 501 | local parsed = URL.parse_url(request.url, { | 501 | local parsed = socket.url.parse(request.url, { |
| 502 | host = "", | 502 | host = "", |
| 503 | user = "anonymous", | 503 | user = "anonymous", |
| 504 | port = 21, | 504 | port = 21, |
| @@ -521,9 +521,10 @@ end | |||
| 521 | -- Returns | 521 | -- Returns |
| 522 | -- dirs: a table with parsed directory components | 522 | -- dirs: a table with parsed directory components |
| 523 | ----------------------------------------------------------------------------- | 523 | ----------------------------------------------------------------------------- |
| 524 | function Private.parse_path(parsed) | 524 | function Private.parse_path(parsed_url) |
| 525 | local segment = URL.parse_path(parsed.path) | 525 | local segment = socket.url.parse_path(parsed_url.path) |
| 526 | segment.is_directory = segment.is_directory or (parsed.params == "type=d") | 526 | segment.is_directory = segment.is_directory or |
| 527 | (parsed_url.params == "type=d") | ||
| 527 | return segment | 528 | return segment |
| 528 | end | 529 | end |
| 529 | 530 | ||
| @@ -560,7 +561,7 @@ end | |||
| 560 | function Public.get_cb(request) | 561 | function Public.get_cb(request) |
| 561 | local parsed = Private.parse_url(request) | 562 | local parsed = Private.parse_url(request) |
| 562 | if parsed.scheme ~= "ftp" then | 563 | if parsed.scheme ~= "ftp" then |
| 563 | return format("unknown scheme '%s'", parsed.scheme) | 564 | return string.format("unknown scheme '%s'", parsed.scheme) |
| 564 | end | 565 | end |
| 565 | local control, err = Private.open(parsed) | 566 | local control, err = Private.open(parsed) |
| 566 | if not control then return err end | 567 | if not control then return err end |
| @@ -586,7 +587,7 @@ end | |||
| 586 | function Public.put_cb(request) | 587 | function Public.put_cb(request) |
| 587 | local parsed = Private.parse_url(request) | 588 | local parsed = Private.parse_url(request) |
| 588 | if parsed.scheme ~= "ftp" then | 589 | if parsed.scheme ~= "ftp" then |
| 589 | return format("unknown scheme '%s'", parsed.scheme) | 590 | return string.format("unknown scheme '%s'", parsed.scheme) |
| 590 | end | 591 | end |
| 591 | local control, err = Private.open(parsed) | 592 | local control, err = Private.open(parsed) |
| 592 | if not control then return err end | 593 | if not control then return err end |
| @@ -612,7 +613,7 @@ end | |||
| 612 | function Public.put(url_or_request, content) | 613 | function Public.put(url_or_request, content) |
| 613 | local request = Private.build_request(url_or_request) | 614 | local request = Private.build_request(url_or_request) |
| 614 | request.content_cb = function() | 615 | request.content_cb = function() |
| 615 | return content, strlen(content) | 616 | return content, string.len(content) |
| 616 | end | 617 | end |
| 617 | return Public.put_cb(request) | 618 | return Public.put_cb(request) |
| 618 | end | 619 | end |
| @@ -630,7 +631,7 @@ end | |||
| 630 | -- err: error message in case of error, nil otherwise | 631 | -- err: error message in case of error, nil otherwise |
| 631 | ----------------------------------------------------------------------------- | 632 | ----------------------------------------------------------------------------- |
| 632 | function Public.get(url_or_request) | 633 | function Public.get(url_or_request) |
| 633 | local cat = Concat.create() | 634 | local cat = socket.concat.create() |
| 634 | local request = Private.build_request(url_or_request) | 635 | local request = Private.build_request(url_or_request) |
| 635 | request.content_cb = function(chunk, err) | 636 | request.content_cb = function(chunk, err) |
| 636 | if chunk then cat:addstring(chunk) end | 637 | if chunk then cat:addstring(chunk) end |
diff --git a/src/http.lua b/src/http.lua index 9543d59..3275e3b 100644 --- a/src/http.lua +++ b/src/http.lua | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
| 9 | 9 | ||
| 10 | local Public, Private = {}, {} | 10 | local Public, Private = {}, {} |
| 11 | http = Public | 11 | socket.http = Public |
| 12 | 12 | ||
| 13 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
| 14 | -- Program constants | 14 | -- Program constants |
| @@ -427,7 +427,7 @@ end | |||
| 427 | ----------------------------------------------------------------------------- | 427 | ----------------------------------------------------------------------------- |
| 428 | function Private.authorize(request, parsed, response) | 428 | function Private.authorize(request, parsed, response) |
| 429 | request.headers["authorization"] = "Basic " .. | 429 | request.headers["authorization"] = "Basic " .. |
| 430 | Code.base64(parsed.user .. ":" .. parsed.password) | 430 | socket.code.base64(parsed.user .. ":" .. parsed.password) |
| 431 | local authorize = { | 431 | local authorize = { |
| 432 | redirects = request.redirects, | 432 | redirects = request.redirects, |
| 433 | method = request.method, | 433 | method = request.method, |
| @@ -471,7 +471,7 @@ function Private.redirect(request, response) | |||
| 471 | method = request.method, | 471 | method = request.method, |
| 472 | -- the RFC says the redirect URL has to be absolute, but some | 472 | -- the RFC says the redirect URL has to be absolute, but some |
| 473 | -- servers do not respect that | 473 | -- servers do not respect that |
| 474 | url = URL.absolute_url(request.url, response.headers["location"]), | 474 | url = socket.url.absolute(request.url, response.headers["location"]), |
| 475 | body_cb = request.body_cb, | 475 | body_cb = request.body_cb, |
| 476 | headers = request.headers | 476 | headers = request.headers |
| 477 | } | 477 | } |
| @@ -535,7 +535,7 @@ end | |||
| 535 | -- error: error message, or nil if successfull | 535 | -- error: error message, or nil if successfull |
| 536 | ----------------------------------------------------------------------------- | 536 | ----------------------------------------------------------------------------- |
| 537 | function Public.request_cb(request, response) | 537 | function Public.request_cb(request, response) |
| 538 | local parsed = URL.parse_url(request.url, { | 538 | local parsed = socket.url.parse(request.url, { |
| 539 | host = "", | 539 | host = "", |
| 540 | port = Public.PORT, | 540 | port = Public.PORT, |
| 541 | path ="/", | 541 | path ="/", |
| @@ -622,7 +622,7 @@ function Public.request(request) | |||
| 622 | return request.body, string.len(request.body) | 622 | return request.body, string.len(request.body) |
| 623 | end | 623 | end |
| 624 | end | 624 | end |
| 625 | local cat = Concat.create() | 625 | local cat = socket.concat.create() |
| 626 | response.body_cb = function(chunk, err) | 626 | response.body_cb = function(chunk, err) |
| 627 | if chunk then cat:addstring(chunk) end | 627 | if chunk then cat:addstring(chunk) end |
| 628 | return 1 | 628 | return 1 |
diff --git a/src/mbox.lua b/src/mbox.lua index 2969111..4a72331 100644 --- a/src/mbox.lua +++ b/src/mbox.lua | |||
| @@ -4,11 +4,11 @@ mbox = Public | |||
| 4 | 4 | ||
| 5 | function Public.split_message(message_s) | 5 | function Public.split_message(message_s) |
| 6 | local message = {} | 6 | local message = {} |
| 7 | message_s = gsub(message_s, "\r\n", "\n") | 7 | message_s = string.gsub(message_s, "\r\n", "\n") |
| 8 | gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end) | 8 | string.gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end) |
| 9 | gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end) | 9 | string.gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end) |
| 10 | if not message.body then | 10 | if not message.body then |
| 11 | gsub(message_s, "^\n(.*)", function (b) %message.body = b end) | 11 | string.gsub(message_s, "^\n(.*)", function (b) %message.body = b end) |
| 12 | end | 12 | end |
| 13 | if not message.headers and not message.body then | 13 | if not message.headers and not message.body then |
| 14 | message.headers = message_s | 14 | message.headers = message_s |
| @@ -18,26 +18,26 @@ end | |||
| 18 | 18 | ||
| 19 | function Public.split_headers(headers_s) | 19 | function Public.split_headers(headers_s) |
| 20 | local headers = {} | 20 | local headers = {} |
| 21 | headers_s = gsub(headers_s, "\r\n", "\n") | 21 | headers_s = string.gsub(headers_s, "\r\n", "\n") |
| 22 | headers_s = gsub(headers_s, "\n[ ]+", " ") | 22 | headers_s = string.gsub(headers_s, "\n[ ]+", " ") |
| 23 | gsub("\n" .. headers_s, "\n([^\n]+)", function (h) tinsert(%headers, h) end) | 23 | string.gsub("\n" .. headers_s, "\n([^\n]+)", function (h) table.insert(%headers, h) end) |
| 24 | return headers | 24 | return headers |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | function Public.parse_header(header_s) | 27 | function Public.parse_header(header_s) |
| 28 | header_s = gsub(header_s, "\n[ ]+", " ") | 28 | header_s = string.gsub(header_s, "\n[ ]+", " ") |
| 29 | header_s = gsub(header_s, "\n+", "") | 29 | header_s = string.gsub(header_s, "\n+", "") |
| 30 | local _, __, name, value = strfind(header_s, "([^%s:]-):%s*(.*)") | 30 | local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") |
| 31 | return name, value | 31 | return name, value |
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | function Public.parse_headers(headers_s) | 34 | function Public.parse_headers(headers_s) |
| 35 | local headers_t = %Public.split_headers(headers_s) | 35 | local headers_t = %Public.split_headers(headers_s) |
| 36 | local headers = {} | 36 | local headers = {} |
| 37 | for i = 1, getn(headers_t) do | 37 | for i = 1, table.getn(headers_t) do |
| 38 | local name, value = %Public.parse_header(headers_t[i]) | 38 | local name, value = %Public.parse_header(headers_t[i]) |
| 39 | if name then | 39 | if name then |
| 40 | name = strlower(name) | 40 | name = string.lower(name) |
| 41 | if headers[name] then | 41 | if headers[name] then |
| 42 | headers[name] = headers[name] .. ", " .. value | 42 | headers[name] = headers[name] .. ", " .. value |
| 43 | else headers[name] = value end | 43 | else headers[name] = value end |
| @@ -47,34 +47,34 @@ function Public.parse_headers(headers_s) | |||
| 47 | end | 47 | end |
| 48 | 48 | ||
| 49 | function Public.parse_from(from) | 49 | function Public.parse_from(from) |
| 50 | local _, __, name, address = strfind(from, "^%s*(.-)%s*%<(.-)%>") | 50 | local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") |
| 51 | if not address then | 51 | if not address then |
| 52 | _, __, address = strfind(from, "%s*(.+)%s*") | 52 | _, __, address = string.find(from, "%s*(.+)%s*") |
| 53 | end | 53 | end |
| 54 | name = name or "" | 54 | name = name or "" |
| 55 | address = address or "" | 55 | address = address or "" |
| 56 | if name == "" then name = address end | 56 | if name == "" then name = address end |
| 57 | name = gsub(name, '"', "") | 57 | name = string.gsub(name, '"', "") |
| 58 | return name, address | 58 | return name, address |
| 59 | end | 59 | end |
| 60 | 60 | ||
| 61 | function Public.split_mbox(mbox_s) | 61 | function Public.split_mbox(mbox_s) |
| 62 | mbox = {} | 62 | mbox = {} |
| 63 | mbox_s = gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" | 63 | mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" |
| 64 | local nj, i, j = 1, 1, 1 | 64 | local nj, i, j = 1, 1, 1 |
| 65 | while 1 do | 65 | while 1 do |
| 66 | i, nj = strfind(mbox_s, "\n\nFrom .-\n", j) | 66 | i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) |
| 67 | if not i then break end | 67 | if not i then break end |
| 68 | local message = strsub(mbox_s, j, i-1) | 68 | local message = string.sub(mbox_s, j, i-1) |
| 69 | tinsert(mbox, message) | 69 | table.insert(mbox, message) |
| 70 | j = nj+1 | 70 | j = nj+1 |
| 71 | end | 71 | end |
| 72 | return mbox | 72 | return mbox |
| 73 | end | 73 | end |
| 74 | 74 | ||
| 75 | function Public.parse_mbox(mbox_s) | 75 | function Public.parse(mbox_s) |
| 76 | local mbox = %Public.split_mbox(mbox_s) | 76 | local mbox = %Public.split_mbox(mbox_s) |
| 77 | for i = 1, getn(mbox) do | 77 | for i = 1, table.getn(mbox) do |
| 78 | mbox[i] = %Public.parse_message(mbox[i]) | 78 | mbox[i] = %Public.parse_message(mbox[i]) |
| 79 | end | 79 | end |
| 80 | return mbox | 80 | return mbox |
diff --git a/src/smtp.lua b/src/smtp.lua index 774dddb..5da9a6f 100644 --- a/src/smtp.lua +++ b/src/smtp.lua | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
| 9 | 9 | ||
| 10 | local Public, Private = {}, {} | 10 | local Public, Private = {}, {} |
| 11 | SMTP = Public | 11 | socket.smtp = Public |
| 12 | 12 | ||
| 13 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
| 14 | -- Program constants | 14 | -- Program constants |
| @@ -47,7 +47,7 @@ end | |||
| 47 | ----------------------------------------------------------------------------- | 47 | ----------------------------------------------------------------------------- |
| 48 | function Private.try_receive(...) | 48 | function Private.try_receive(...) |
| 49 | local sock = arg[1] | 49 | local sock = arg[1] |
| 50 | local data, err = call(sock.receive, arg) | 50 | local data, err = sock.receive(unpack(arg)) |
| 51 | if err then sock:close() end | 51 | if err then sock:close() end |
| 52 | return data, err | 52 | return data, err |
| 53 | end | 53 | end |
| @@ -81,13 +81,13 @@ function Private.get_answer(control) | |||
| 81 | local line, err = Private.try_receive(control) | 81 | local line, err = Private.try_receive(control) |
| 82 | local answer = line | 82 | local answer = line |
| 83 | if err then return nil, err end | 83 | if err then return nil, err end |
| 84 | _,_, code, sep = strfind(line, "^(%d%d%d)(.)") | 84 | _,_, code, sep = string.find(line, "^(%d%d%d)(.)") |
| 85 | if not code or not sep then return nil, answer end | 85 | if not code or not sep then return nil, answer end |
| 86 | if sep == "-" then -- answer is multiline | 86 | if sep == "-" then -- answer is multiline |
| 87 | repeat | 87 | repeat |
| 88 | line, err = Private.try_receive(control) | 88 | line, err = Private.try_receive(control) |
| 89 | if err then return nil, err end | 89 | if err then return nil, err end |
| 90 | _,_, lastcode, sep = strfind(line, "^(%d%d%d)(.)") | 90 | _,_, lastcode, sep = string.find(line, "^(%d%d%d)(.)") |
| 91 | answer = answer .. "\n" .. line | 91 | answer = answer .. "\n" .. line |
| 92 | until code == lastcode and sep == " " -- answer ends with same code | 92 | until code == lastcode and sep == " " -- answer ends with same code |
| 93 | end | 93 | end |
| @@ -108,7 +108,7 @@ function Private.check_answer(control, success) | |||
| 108 | local answer, code = Private.get_answer(control) | 108 | local answer, code = Private.get_answer(control) |
| 109 | if not answer then return nil, code end | 109 | if not answer then return nil, code end |
| 110 | if type(success) ~= "table" then success = {success} end | 110 | if type(success) ~= "table" then success = {success} end |
| 111 | for i = 1, getn(success) do | 111 | for i = 1, table.getn(success) do |
| 112 | if code == success[i] then | 112 | if code == success[i] then |
| 113 | return code, answer | 113 | return code, answer |
| 114 | end | 114 | end |
| @@ -157,7 +157,7 @@ end | |||
| 157 | -- answer: complete server reply or error message | 157 | -- answer: complete server reply or error message |
| 158 | ----------------------------------------------------------------------------- | 158 | ----------------------------------------------------------------------------- |
| 159 | function Private.send_mail(sock, sender) | 159 | function Private.send_mail(sock, sender) |
| 160 | local param = format("FROM:<%s>", sender or "") | 160 | local param = string.format("FROM:<%s>", sender or "") |
| 161 | local err = Private.send_command(sock, "MAIL", param) | 161 | local err = Private.send_command(sock, "MAIL", param) |
| 162 | if err then return nil, err end | 162 | if err then return nil, err end |
| 163 | return Private.check_answer(sock, 250) | 163 | return Private.check_answer(sock, 250) |
| @@ -198,7 +198,7 @@ function Private.send_data(sock, headers, body) | |||
| 198 | local code, answer = Private.check_answer(sock, 354) | 198 | local code, answer = Private.check_answer(sock, 354) |
| 199 | if not code then return nil, answer end | 199 | if not code then return nil, answer end |
| 200 | -- avoid premature end in message body | 200 | -- avoid premature end in message body |
| 201 | body = gsub(body or "", "\n%.", "\n%.%.") | 201 | body = string.gsub(body or "", "\n%.", "\n%.%.") |
| 202 | -- mark end of message body | 202 | -- mark end of message body |
| 203 | body = body .. "\r\n.\r\n" | 203 | body = body .. "\r\n.\r\n" |
| 204 | err = Private.send_headers(sock, headers) | 204 | err = Private.send_headers(sock, headers) |
| @@ -220,8 +220,9 @@ function Private.send_rcpt(sock, rcpt) | |||
| 220 | local err | 220 | local err |
| 221 | local code, answer = nil, "No recipient specified" | 221 | local code, answer = nil, "No recipient specified" |
| 222 | if type(rcpt) ~= "table" then rcpt = {rcpt} end | 222 | if type(rcpt) ~= "table" then rcpt = {rcpt} end |
| 223 | for i = 1, getn(rcpt) do | 223 | for i = 1, table.getn(rcpt) do |
| 224 | err = Private.send_command(sock, "RCPT", format("TO:<%s>", rcpt[i])) | 224 | err = Private.send_command(sock, "RCPT", |
| 225 | string.format("TO:<%s>", rcpt[i])) | ||
| 225 | if err then return nil, err end | 226 | if err then return nil, err end |
| 226 | code, answer = Private.check_answer(sock, {250, 251}) | 227 | code, answer = Private.check_answer(sock, {250, 251}) |
| 227 | if not code then return code, answer end | 228 | if not code then return code, answer end |
| @@ -242,7 +243,7 @@ function Private.open(server) | |||
| 242 | -- default server | 243 | -- default server |
| 243 | server = server or Public.SERVER | 244 | server = server or Public.SERVER |
| 244 | -- connect to server and make sure we won't hang | 245 | -- connect to server and make sure we won't hang |
| 245 | local sock, err = connect(server, Public.PORT) | 246 | local sock, err = socket.connect(server, Public.PORT) |
| 246 | if not sock then return nil, err end | 247 | if not sock then return nil, err end |
| 247 | sock:timeout(Public.TIMEOUT) | 248 | sock:timeout(Public.TIMEOUT) |
| 248 | -- initial server greeting | 249 | -- initial server greeting |
diff --git a/src/url.lua b/src/url.lua index 4d2bfa7..2cf9669 100644 --- a/src/url.lua +++ b/src/url.lua | |||
| @@ -8,7 +8,7 @@ | |||
| 8 | ---------------------------------------------------------------------------- | 8 | ---------------------------------------------------------------------------- |
| 9 | 9 | ||
| 10 | local Public, Private = {}, {} | 10 | local Public, Private = {}, {} |
| 11 | URL = Public | 11 | socket.url = Public |
| 12 | 12 | ||
| 13 | ----------------------------------------------------------------------------- | 13 | ----------------------------------------------------------------------------- |
| 14 | -- Parses a url and returns a table with all its parts according to RFC 2396 | 14 | -- Parses a url and returns a table with all its parts according to RFC 2396 |
| @@ -28,7 +28,7 @@ URL = Public | |||
| 28 | -- Obs: | 28 | -- Obs: |
| 29 | -- the leading '/' in {/<path>} is considered part of <path> | 29 | -- the leading '/' in {/<path>} is considered part of <path> |
| 30 | ----------------------------------------------------------------------------- | 30 | ----------------------------------------------------------------------------- |
| 31 | function Public.parse_url(url, default) | 31 | function Public.parse(url, default) |
| 32 | -- initialize default parameters | 32 | -- initialize default parameters |
| 33 | local parsed = default or {} | 33 | local parsed = default or {} |
| 34 | -- empty url is parsed to nil | 34 | -- empty url is parsed to nil |
| @@ -70,7 +70,7 @@ end | |||
| 70 | -- Returns | 70 | -- Returns |
| 71 | -- a stringing with the corresponding URL | 71 | -- a stringing with the corresponding URL |
| 72 | ----------------------------------------------------------------------------- | 72 | ----------------------------------------------------------------------------- |
| 73 | function Public.build_url(parsed) | 73 | function Public.build(parsed) |
| 74 | local url = parsed.path or "" | 74 | local url = parsed.path or "" |
| 75 | if parsed.params then url = url .. ";" .. parsed.params end | 75 | if parsed.params then url = url .. ";" .. parsed.params end |
| 76 | if parsed.query then url = url .. "?" .. parsed.query end | 76 | if parsed.query then url = url .. "?" .. parsed.query end |
| @@ -102,9 +102,9 @@ end | |||
| 102 | -- Returns | 102 | -- Returns |
| 103 | -- corresponding absolute url | 103 | -- corresponding absolute url |
| 104 | ----------------------------------------------------------------------------- | 104 | ----------------------------------------------------------------------------- |
| 105 | function Public.absolute_url(base_url, relative_url) | 105 | function Public.absolute(base_url, relative_url) |
| 106 | local base = Public.parse_url(base_url) | 106 | local base = Public.parse(base_url) |
| 107 | local relative = Public.parse_url(relative_url) | 107 | local relative = Public.parse(relative_url) |
| 108 | if not base then return relative_url | 108 | if not base then return relative_url |
| 109 | elseif not relative then return base_url | 109 | elseif not relative then return base_url |
| 110 | elseif relative.scheme then return relative_url | 110 | elseif relative.scheme then return relative_url |
| @@ -124,7 +124,7 @@ function Public.absolute_url(base_url, relative_url) | |||
| 124 | relative.path = Private.absolute_path(base.path,relative.path) | 124 | relative.path = Private.absolute_path(base.path,relative.path) |
| 125 | end | 125 | end |
| 126 | end | 126 | end |
| 127 | return Public.build_url(relative) | 127 | return Public.build(relative) |
| 128 | end | 128 | end |
| 129 | end | 129 | end |
| 130 | 130 | ||
| @@ -141,7 +141,7 @@ function Public.parse_path(path) | |||
| 141 | path = string.gsub(path, "%s", "") | 141 | path = string.gsub(path, "%s", "") |
| 142 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) | 142 | string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end) |
| 143 | for i = 1, table.getn(parsed) do | 143 | for i = 1, table.getn(parsed) do |
| 144 | parsed[i] = Code.unescape(parsed[i]) | 144 | parsed[i] = socket.code.unescape(parsed[i]) |
| 145 | end | 145 | end |
| 146 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end | 146 | if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end |
| 147 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end | 147 | if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end |
| @@ -201,7 +201,7 @@ function Private.protect_segment(s) | |||
| 201 | local segment_set = Private.segment_set | 201 | local segment_set = Private.segment_set |
| 202 | return string.gsub(s, "(%W)", function (c) | 202 | return string.gsub(s, "(%W)", function (c) |
| 203 | if segment_set[c] then return c | 203 | if segment_set[c] then return c |
| 204 | else return Code.escape(c) end | 204 | else return socket.code.escape(c) end |
| 205 | end) | 205 | end) |
| 206 | end | 206 | end |
| 207 | 207 | ||
