diff options
author | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-26 10:52:04 +0000 |
---|---|---|
committer | shugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-26 10:52:04 +0000 |
commit | 8c89d8ef727d7c7099ed92b21bf8de12ffbeb197 (patch) | |
tree | 47de66a9daeed890ea935cd9ca41209c9f36fb5a | |
parent | b9f67c4149f5f8888ac4681e15695deecd213136 (diff) | |
download | ruby-8c89d8ef727d7c7099ed92b21bf8de12ffbeb197.tar.gz |
* lib/net/ftp.rb (parse227, parse228, parse229): refactored.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | lib/net/ftp.rb | 71 |
2 files changed, 33 insertions, 42 deletions
@@ -1,3 +1,7 @@ +Mon Mar 26 19:37:27 2012 Shugo Maeda <shugo@ruby-lang.org> + + * lib/net/ftp.rb (parse227, parse228, parse229): refactored. + Mon Mar 26 11:46:23 2012 Shugo Maeda <shugo@ruby-lang.org> * enumerator.c (inspect_enumerator): show method arguments of diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb index 9abfbae421..4fc7fabee3 100644 --- a/lib/net/ftp.rb +++ b/lib/net/ftp.rb @@ -923,18 +923,11 @@ module Net if resp[0, 3] != "227" raise FTPReplyError, resp end - left = resp.index("(") - right = resp.index(")") - if left == nil or right == nil - raise FTPProtoError, resp - end - numbers = resp[left + 1 .. right - 1].split(",") - if numbers.length != 6 + if /\((?<host>\d+(,\d+){3}),(?<port>\d+,\d+)\)/ =~ resp + return parse_pasv_ipv4_host(host), parse_pasv_port(port) + else raise FTPProtoError, resp end - host = numbers[0, 4].join(".") - port = (numbers[4].to_i << 8) + numbers[5].to_i - return host, port end private :parse227 @@ -946,34 +939,35 @@ module Net if resp[0, 3] != "228" raise FTPReplyError, resp end - left = resp.index("(") - right = resp.index(")") - if left == nil or right == nil + if /\(4,4,(?<host>\d+(,\d+){3}),2,(?<port>\d+,\d+)\)/ =~ resp + return parse_pasv_ipv4_host(host), parse_pasv_port(port) + elsif /\(6,16,(?<host>\d+(,(\d+)){15}),2,(?<port>\d+,\d+)\)/ =~ resp + return parse_pasv_ipv6_host(host), parse_pasv_port(port) + else raise FTPProtoError, resp end - numbers = resp[left + 1 .. right - 1].split(",") - if numbers[0] == "4" - if numbers.length != 9 || numbers[1] != "4" || numbers[2 + 4] != "2" - raise FTPProtoError, resp - end - host = numbers[2, 4].join(".") - port = (numbers[7].to_i << 8) + numbers[8].to_i - elsif numbers[0] == "6" - if numbers.length != 21 || numbers[1] != "16" || numbers[2 + 16] != "2" - raise FTPProtoError, resp - end - v6 = ["", "", "", "", "", "", "", ""] - for i in 0 .. 7 - v6[i] = sprintf("%02x%02x", numbers[(i * 2) + 2].to_i, - numbers[(i * 2) + 3].to_i) - end - host = v6[0, 8].join(":") - port = (numbers[19].to_i << 8) + numbers[20].to_i - end return host, port end private :parse228 + def parse_pasv_ipv4_host(s) + return s.tr(",", ".") + end + private :parse_pasv_ipv4_host + + def parse_pasv_ipv6_host(s) + return s.split(/,/).map { |i| + "%02x" % i.to_i + }.each_slice(2).map(&:join).join(":") + end + private :parse_pasv_ipv6_host + + def parse_pasv_port(s) + high, low = s.split(/,/).map(&:to_i) + return (high << 8) + low + end + private :parse_pasv_port + # handler for response code 229 # (Extended Passive Mode Entered) # @@ -982,18 +976,11 @@ module Net if resp[0, 3] != "229" raise FTPReplyError, resp end - left = resp.index("(") - right = resp.index(")") - if left == nil or right == nil - raise FTPProtoError, resp - end - numbers = resp[left + 1 .. right - 1].split(resp[left + 1, 1]) - if numbers.length != 4 + if /\((?<d>[!-~])\k<d>\k<d>(?<port>\d+)\k<d>\)/ =~ resp + return @sock.peeraddr[3], port.to_i + else raise FTPProtoError, resp end - port = numbers[3].to_i - host = (@sock.peeraddr())[3] - return host, port end private :parse229 |