diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ftp.lua | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/src/ftp.lua b/src/ftp.lua index 98b08eb..1af4d30 100644 --- a/src/ftp.lua +++ b/src/ftp.lua | |||
| @@ -273,11 +273,11 @@ end | |||
| 273 | -- server: server socket bound to local address | 273 | -- server: server socket bound to local address |
| 274 | -- name: file name | 274 | -- name: file name |
| 275 | -- is_directory: is file a directory name? | 275 | -- is_directory: is file a directory name? |
| 276 | -- download_cb: callback to receive file contents | 276 | -- content_cb: callback to receive file contents |
| 277 | -- Returns | 277 | -- Returns |
| 278 | -- err: error message in case of error, nil otherwise | 278 | -- err: error message in case of error, nil otherwise |
| 279 | ----------------------------------------------------------------------------- | 279 | ----------------------------------------------------------------------------- |
| 280 | function Private.retrieve(control, server, name, is_directory, download_cb) | 280 | function Private.retrieve(control, server, name, is_directory, content_cb) |
| 281 | local code, answer | 281 | local code, answer |
| 282 | local data | 282 | local data |
| 283 | -- ask server for file or directory listing accordingly | 283 | -- ask server for file or directory listing accordingly |
| @@ -295,7 +295,7 @@ function Private.retrieve(control, server, name, is_directory, download_cb) | |||
| 295 | control:close() | 295 | control:close() |
| 296 | return answer | 296 | return answer |
| 297 | end | 297 | end |
| 298 | answer = %Private.receive_indirect(data, download_cb) | 298 | answer = %Private.receive_indirect(data, content_cb) |
| 299 | if answer then | 299 | if answer then |
| 300 | control:close() | 300 | control:close() |
| 301 | return answer | 301 | return answer |
| @@ -380,10 +380,9 @@ end | |||
| 380 | -- err: error message if any | 380 | -- err: error message if any |
| 381 | ----------------------------------------------------------------------------- | 381 | ----------------------------------------------------------------------------- |
| 382 | function Private.change_type(control, params) | 382 | function Private.change_type(control, params) |
| 383 | local type | 383 | local type, _ |
| 384 | if params == "type=i" then type = "i" | 384 | _, _, type = strfind(params or "", "type=(.)") |
| 385 | elseif params == "type=a" then type = "a" end | 385 | if type == "a" or type == "i" then |
| 386 | if type then | ||
| 387 | local code, err = %Private.command(control, "type", type, {200}) | 386 | local code, err = %Private.command(control, "type", type, {200}) |
| 388 | if not code then return err end | 387 | if not code then return err end |
| 389 | end | 388 | end |
| @@ -443,25 +442,25 @@ end | |||
| 443 | -- Input | 442 | -- Input |
| 444 | -- control: control connection with server | 443 | -- control: control connection with server |
| 445 | -- request: a table with the fields: | 444 | -- request: a table with the fields: |
| 446 | -- upload_cb: send callback to send file contents | 445 | -- content_cb: send callback to send file contents |
| 447 | -- segment: parsed URL path segments | 446 | -- segment: parsed URL path segments |
| 448 | -- Returns | 447 | -- Returns |
| 449 | -- err: error message if any | 448 | -- err: error message if any |
| 450 | ----------------------------------------------------------------------------- | 449 | ----------------------------------------------------------------------------- |
| 451 | function Private.upload(control, request, segment) | 450 | function Private.upload(control, request, segment) |
| 452 | local code, name, upload_cb | 451 | local code, name, content_cb |
| 453 | -- get remote file name | 452 | -- get remote file name |
| 454 | name = segment[getn(segment)] | 453 | name = segment[getn(segment)] |
| 455 | if not name then | 454 | if not name then |
| 456 | control:close() | 455 | control:close() |
| 457 | return "Invalid file path" | 456 | return "Invalid file path" |
| 458 | end | 457 | end |
| 459 | upload_cb = request.upload_cb | 458 | content_cb = request.content_cb |
| 460 | -- setup passive connection | 459 | -- setup passive connection |
| 461 | local server, answer = %Private.port(control) | 460 | local server, answer = %Private.port(control) |
| 462 | if not server then return answer end | 461 | if not server then return answer end |
| 463 | -- ask server to receive file | 462 | -- ask server to receive file |
| 464 | code, answer = %Private.store(control, server, name, upload_cb) | 463 | code, answer = %Private.store(control, server, name, content_cb) |
| 465 | if not code then return answer end | 464 | if not code then return answer end |
| 466 | end | 465 | end |
| 467 | 466 | ||
| @@ -470,15 +469,15 @@ end | |||
| 470 | -- Input | 469 | -- Input |
| 471 | -- control: control connection with server | 470 | -- control: control connection with server |
| 472 | -- request: a table with the fields: | 471 | -- request: a table with the fields: |
| 473 | -- download_cb: receive callback to receive file contents | 472 | -- content_cb: receive callback to receive file contents |
| 474 | -- segment: parsed URL path segments | 473 | -- segment: parsed URL path segments |
| 475 | -- Returns | 474 | -- Returns |
| 476 | -- err: error message if any | 475 | -- err: error message if any |
| 477 | ----------------------------------------------------------------------------- | 476 | ----------------------------------------------------------------------------- |
| 478 | function Private.download(control, request, segment) | 477 | function Private.download(control, request, segment) |
| 479 | local code, name, is_directory, download_cb | 478 | local code, name, is_directory, content_cb |
| 480 | is_directory = segment.is_directory | 479 | is_directory = segment.is_directory |
| 481 | download_cb = request.download_cb | 480 | content_cb = request.content_cb |
| 482 | -- get remote file name | 481 | -- get remote file name |
| 483 | name = segment[getn(segment)] | 482 | name = segment[getn(segment)] |
| 484 | if not name and not is_directory then | 483 | if not name and not is_directory then |
| @@ -490,7 +489,7 @@ function Private.download(control, request, segment) | |||
| 490 | if not server then return answer end | 489 | if not server then return answer end |
| 491 | -- ask server to send file or directory listing | 490 | -- ask server to send file or directory listing |
| 492 | code, answer = %Private.retrieve(control, server, name, | 491 | code, answer = %Private.retrieve(control, server, name, |
| 493 | is_directory, download_cb) | 492 | is_directory, content_cb) |
| 494 | if not code then return answer end | 493 | if not code then return answer end |
| 495 | end | 494 | end |
| 496 | 495 | ||
| @@ -511,7 +510,8 @@ function Private.parse_url(request) | |||
| 511 | user = "anonymous", | 510 | user = "anonymous", |
| 512 | port = 21, | 511 | port = 21, |
| 513 | path = "/", | 512 | path = "/", |
| 514 | password = %Public.EMAIL | 513 | password = %Public.EMAIL, |
| 514 | scheme = "ftp" | ||
| 515 | }) | 515 | }) |
| 516 | -- explicit login information overrides that given by URL | 516 | -- explicit login information overrides that given by URL |
| 517 | parsed.user = request.user or parsed.user | 517 | parsed.user = request.user or parsed.user |
| @@ -560,12 +560,15 @@ end | |||
| 560 | -- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory | 560 | -- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory |
| 561 | -- user: account user name | 561 | -- user: account user name |
| 562 | -- password: account password | 562 | -- password: account password |
| 563 | -- download_cb: receive callback to receive file contents | 563 | -- content_cb: receive callback to receive file contents |
| 564 | -- Returns | 564 | -- Returns |
| 565 | -- err: error message if any | 565 | -- err: error message if any |
| 566 | ----------------------------------------------------------------------------- | 566 | ----------------------------------------------------------------------------- |
| 567 | function Public.get_cb(request) | 567 | function Public.get_cb(request) |
| 568 | local parsed = %Private.parse_url(request) | 568 | local parsed = %Private.parse_url(request) |
| 569 | if parsed.scheme ~= "ftp" then | ||
| 570 | return format("unknown scheme '%s'", parsed.scheme) | ||
| 571 | end | ||
| 569 | local control, err = %Private.open(parsed) | 572 | local control, err = %Private.open(parsed) |
| 570 | if not control then return err end | 573 | if not control then return err end |
| 571 | local segment = %Private.parse_path(parsed) | 574 | local segment = %Private.parse_path(parsed) |
| @@ -583,12 +586,15 @@ end | |||
| 583 | -- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory | 586 | -- type: "i" for "image" mode, "a" for "ascii" mode or "d" for directory |
| 584 | -- user: account user name | 587 | -- user: account user name |
| 585 | -- password: account password | 588 | -- password: account password |
| 586 | -- upload_cb: send callback to send file contents | 589 | -- content_cb: send callback to send file contents |
| 587 | -- Returns | 590 | -- Returns |
| 588 | -- err: error message if any | 591 | -- err: error message if any |
| 589 | ----------------------------------------------------------------------------- | 592 | ----------------------------------------------------------------------------- |
| 590 | function Public.put_cb(request) | 593 | function Public.put_cb(request) |
| 591 | local parsed = %Private.parse_url(request) | 594 | local parsed = %Private.parse_url(request) |
| 595 | if parsed.scheme ~= "ftp" then | ||
| 596 | return format("unknown scheme '%s'", parsed.scheme) | ||
| 597 | end | ||
| 592 | local control, err = %Private.open(parsed) | 598 | local control, err = %Private.open(parsed) |
| 593 | if not control then return err end | 599 | if not control then return err end |
| 594 | local segment = %Private.parse_path(parsed) | 600 | local segment = %Private.parse_path(parsed) |
| @@ -612,7 +618,7 @@ end | |||
| 612 | ----------------------------------------------------------------------------- | 618 | ----------------------------------------------------------------------------- |
| 613 | function Public.put(url_or_request, content) | 619 | function Public.put(url_or_request, content) |
| 614 | local request = %Private.build_request(url_or_request) | 620 | local request = %Private.build_request(url_or_request) |
| 615 | request.upload_cb = function() | 621 | request.content_cb = function() |
| 616 | return %content, strlen(%content) | 622 | return %content, strlen(%content) |
| 617 | end | 623 | end |
| 618 | return %Public.put_cb(request) | 624 | return %Public.put_cb(request) |
| @@ -633,7 +639,7 @@ end | |||
| 633 | function Public.get(url_or_request) | 639 | function Public.get(url_or_request) |
| 634 | local cat = Concat.create() | 640 | local cat = Concat.create() |
| 635 | local request = %Private.build_request(url_or_request) | 641 | local request = %Private.build_request(url_or_request) |
| 636 | request.download_cb = function(chunk, err) | 642 | request.content_cb = function(chunk, err) |
| 637 | if chunk then %cat:addstring(chunk) end | 643 | if chunk then %cat:addstring(chunk) end |
| 638 | return 1 | 644 | return 1 |
| 639 | end | 645 | end |
