mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Guard for the ftp protocol feature of OpenURI
This commit is contained in:
parent
b8dcad8d32
commit
350bc29107
Notes:
git
2021-05-27 14:42:36 +09:00
2 changed files with 134 additions and 123 deletions
|
@ -750,7 +750,12 @@ module URI
|
||||||
OpenURI.open_http(buf, self, proxy, options)
|
OpenURI.open_http(buf, self, proxy, options)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
require 'net/ftp'
|
|
||||||
|
begin
|
||||||
|
require 'net/ftp'
|
||||||
|
rescue LoadError
|
||||||
|
abort "net/ftp is not found. You may need to `gem install net-ftp` to install net/ftp."
|
||||||
|
end
|
||||||
|
|
||||||
path = self.path
|
path = self.path
|
||||||
path = path.sub(%r{\A/}, '%2F') # re-encode the beginning slash because uri library decodes it.
|
path = path.sub(%r{\A/}, '%2F') # re-encode the beginning slash because uri library decodes it.
|
||||||
|
|
|
@ -703,143 +703,149 @@ class TestOpenURI < Test::Unit::TestCase
|
||||||
|
|
||||||
# 192.0.2.0/24 is TEST-NET. [RFC3330]
|
# 192.0.2.0/24 is TEST-NET. [RFC3330]
|
||||||
|
|
||||||
def test_ftp_invalid_request
|
begin
|
||||||
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/").read }
|
require 'net/ftp'
|
||||||
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db").read }
|
|
||||||
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Ab").read }
|
|
||||||
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db/f").read }
|
|
||||||
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Ab/f").read }
|
|
||||||
assert_nothing_raised(URI::InvalidComponentError) { URI("ftp://127.0.0.1/d/f;type=x") }
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_ftp
|
def test_ftp_invalid_request
|
||||||
TCPServer.open("127.0.0.1", 0) {|serv|
|
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/").read }
|
||||||
_, port, _, host = serv.addr
|
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db").read }
|
||||||
th = Thread.new {
|
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Ab").read }
|
||||||
s = serv.accept
|
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Db/f").read }
|
||||||
|
assert_raise(ArgumentError) { URI("ftp://127.0.0.1/a%0Ab/f").read }
|
||||||
|
assert_nothing_raised(URI::InvalidComponentError) { URI("ftp://127.0.0.1/d/f;type=x") }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ftp
|
||||||
|
TCPServer.open("127.0.0.1", 0) {|serv|
|
||||||
|
_, port, _, host = serv.addr
|
||||||
|
th = Thread.new {
|
||||||
|
s = serv.accept
|
||||||
|
begin
|
||||||
|
s.print "220 Test FTP Server\r\n"
|
||||||
|
assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
|
||||||
|
assert_match(/\APASS .*\r\n\z/, s.gets); s.print "230 logged in\r\n"
|
||||||
|
assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
|
||||||
|
assert_equal("CWD foo\r\n", s.gets); s.print "250 CWD successful\r\n"
|
||||||
|
assert_equal("PASV\r\n", s.gets)
|
||||||
|
TCPServer.open("127.0.0.1", 0) {|data_serv|
|
||||||
|
_, data_serv_port, _, _ = data_serv.addr
|
||||||
|
hi = data_serv_port >> 8
|
||||||
|
lo = data_serv_port & 0xff
|
||||||
|
s.print "227 Entering Passive Mode (127,0,0,1,#{hi},#{lo}).\r\n"
|
||||||
|
assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
|
||||||
|
data_sock = data_serv.accept
|
||||||
|
begin
|
||||||
|
data_sock << "content"
|
||||||
|
ensure
|
||||||
|
data_sock.close
|
||||||
|
end
|
||||||
|
s.print "226 transfer complete\r\n"
|
||||||
|
assert_nil(s.gets)
|
||||||
|
}
|
||||||
|
ensure
|
||||||
|
s.close if s
|
||||||
|
end
|
||||||
|
}
|
||||||
begin
|
begin
|
||||||
s.print "220 Test FTP Server\r\n"
|
content = URI("ftp://#{host}:#{port}/foo/bar").read
|
||||||
assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
|
assert_equal("content", content)
|
||||||
assert_match(/\APASS .*\r\n\z/, s.gets); s.print "230 logged in\r\n"
|
|
||||||
assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
|
|
||||||
assert_equal("CWD foo\r\n", s.gets); s.print "250 CWD successful\r\n"
|
|
||||||
assert_equal("PASV\r\n", s.gets)
|
|
||||||
TCPServer.open("127.0.0.1", 0) {|data_serv|
|
|
||||||
_, data_serv_port, _, _ = data_serv.addr
|
|
||||||
hi = data_serv_port >> 8
|
|
||||||
lo = data_serv_port & 0xff
|
|
||||||
s.print "227 Entering Passive Mode (127,0,0,1,#{hi},#{lo}).\r\n"
|
|
||||||
assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
|
|
||||||
data_sock = data_serv.accept
|
|
||||||
begin
|
|
||||||
data_sock << "content"
|
|
||||||
ensure
|
|
||||||
data_sock.close
|
|
||||||
end
|
|
||||||
s.print "226 transfer complete\r\n"
|
|
||||||
assert_nil(s.gets)
|
|
||||||
}
|
|
||||||
ensure
|
ensure
|
||||||
s.close if s
|
Thread.kill(th)
|
||||||
|
th.join
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
begin
|
end
|
||||||
content = URI("ftp://#{host}:#{port}/foo/bar").read
|
|
||||||
assert_equal("content", content)
|
|
||||||
ensure
|
|
||||||
Thread.kill(th)
|
|
||||||
th.join
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_ftp_active
|
def test_ftp_active
|
||||||
TCPServer.open("127.0.0.1", 0) {|serv|
|
TCPServer.open("127.0.0.1", 0) {|serv|
|
||||||
_, port, _, host = serv.addr
|
_, port, _, host = serv.addr
|
||||||
th = Thread.new {
|
th = Thread.new {
|
||||||
s = serv.accept
|
s = serv.accept
|
||||||
|
begin
|
||||||
|
content = "content"
|
||||||
|
s.print "220 Test FTP Server\r\n"
|
||||||
|
assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
|
||||||
|
assert_match(/\APASS .*\r\n\z/, s.gets); s.print "230 logged in\r\n"
|
||||||
|
assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
|
||||||
|
assert_equal("CWD foo\r\n", s.gets); s.print "250 CWD successful\r\n"
|
||||||
|
assert(m = /\APORT 127,0,0,1,(\d+),(\d+)\r\n\z/.match(s.gets))
|
||||||
|
active_port = m[1].to_i << 8 | m[2].to_i
|
||||||
|
TCPSocket.open("127.0.0.1", active_port) {|data_sock|
|
||||||
|
s.print "200 data connection opened\r\n"
|
||||||
|
assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
|
||||||
|
begin
|
||||||
|
data_sock << content
|
||||||
|
ensure
|
||||||
|
data_sock.close
|
||||||
|
end
|
||||||
|
s.print "226 transfer complete\r\n"
|
||||||
|
assert_nil(s.gets)
|
||||||
|
}
|
||||||
|
ensure
|
||||||
|
s.close if s
|
||||||
|
end
|
||||||
|
}
|
||||||
begin
|
begin
|
||||||
content = "content"
|
content = URI("ftp://#{host}:#{port}/foo/bar").read(:ftp_active_mode=>true)
|
||||||
s.print "220 Test FTP Server\r\n"
|
assert_equal("content", content)
|
||||||
assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
|
|
||||||
assert_match(/\APASS .*\r\n\z/, s.gets); s.print "230 logged in\r\n"
|
|
||||||
assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
|
|
||||||
assert_equal("CWD foo\r\n", s.gets); s.print "250 CWD successful\r\n"
|
|
||||||
assert(m = /\APORT 127,0,0,1,(\d+),(\d+)\r\n\z/.match(s.gets))
|
|
||||||
active_port = m[1].to_i << 8 | m[2].to_i
|
|
||||||
TCPSocket.open("127.0.0.1", active_port) {|data_sock|
|
|
||||||
s.print "200 data connection opened\r\n"
|
|
||||||
assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
|
|
||||||
begin
|
|
||||||
data_sock << content
|
|
||||||
ensure
|
|
||||||
data_sock.close
|
|
||||||
end
|
|
||||||
s.print "226 transfer complete\r\n"
|
|
||||||
assert_nil(s.gets)
|
|
||||||
}
|
|
||||||
ensure
|
ensure
|
||||||
s.close if s
|
Thread.kill(th)
|
||||||
|
th.join
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
begin
|
end
|
||||||
content = URI("ftp://#{host}:#{port}/foo/bar").read(:ftp_active_mode=>true)
|
|
||||||
assert_equal("content", content)
|
|
||||||
ensure
|
|
||||||
Thread.kill(th)
|
|
||||||
th.join
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_ftp_ascii
|
def test_ftp_ascii
|
||||||
TCPServer.open("127.0.0.1", 0) {|serv|
|
TCPServer.open("127.0.0.1", 0) {|serv|
|
||||||
_, port, _, host = serv.addr
|
_, port, _, host = serv.addr
|
||||||
th = Thread.new {
|
th = Thread.new {
|
||||||
s = serv.accept
|
s = serv.accept
|
||||||
|
begin
|
||||||
|
content = "content"
|
||||||
|
s.print "220 Test FTP Server\r\n"
|
||||||
|
assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
|
||||||
|
assert_match(/\APASS .*\r\n\z/, s.gets); s.print "230 logged in\r\n"
|
||||||
|
assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
|
||||||
|
assert_equal("CWD /foo\r\n", s.gets); s.print "250 CWD successful\r\n"
|
||||||
|
assert_equal("TYPE A\r\n", s.gets); s.print "200 type set to A\r\n"
|
||||||
|
assert_equal("SIZE bar\r\n", s.gets); s.print "213 #{content.bytesize}\r\n"
|
||||||
|
assert_equal("PASV\r\n", s.gets)
|
||||||
|
TCPServer.open("127.0.0.1", 0) {|data_serv|
|
||||||
|
_, data_serv_port, _, _ = data_serv.addr
|
||||||
|
hi = data_serv_port >> 8
|
||||||
|
lo = data_serv_port & 0xff
|
||||||
|
s.print "227 Entering Passive Mode (127,0,0,1,#{hi},#{lo}).\r\n"
|
||||||
|
assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
|
||||||
|
data_sock = data_serv.accept
|
||||||
|
begin
|
||||||
|
data_sock << content
|
||||||
|
ensure
|
||||||
|
data_sock.close
|
||||||
|
end
|
||||||
|
s.print "226 transfer complete\r\n"
|
||||||
|
assert_nil(s.gets)
|
||||||
|
}
|
||||||
|
ensure
|
||||||
|
s.close if s
|
||||||
|
end
|
||||||
|
}
|
||||||
begin
|
begin
|
||||||
content = "content"
|
length = []
|
||||||
s.print "220 Test FTP Server\r\n"
|
progress = []
|
||||||
assert_equal("USER anonymous\r\n", s.gets); s.print "331 name ok\r\n"
|
content = URI("ftp://#{host}:#{port}/%2Ffoo/b%61r;type=a").read(
|
||||||
assert_match(/\APASS .*\r\n\z/, s.gets); s.print "230 logged in\r\n"
|
:content_length_proc => lambda {|n| length << n },
|
||||||
assert_equal("TYPE I\r\n", s.gets); s.print "200 type set to I\r\n"
|
:progress_proc => lambda {|n| progress << n })
|
||||||
assert_equal("CWD /foo\r\n", s.gets); s.print "250 CWD successful\r\n"
|
assert_equal("content", content)
|
||||||
assert_equal("TYPE A\r\n", s.gets); s.print "200 type set to A\r\n"
|
assert_equal([7], length)
|
||||||
assert_equal("SIZE bar\r\n", s.gets); s.print "213 #{content.bytesize}\r\n"
|
assert_equal(7, progress.inject(&:+))
|
||||||
assert_equal("PASV\r\n", s.gets)
|
|
||||||
TCPServer.open("127.0.0.1", 0) {|data_serv|
|
|
||||||
_, data_serv_port, _, _ = data_serv.addr
|
|
||||||
hi = data_serv_port >> 8
|
|
||||||
lo = data_serv_port & 0xff
|
|
||||||
s.print "227 Entering Passive Mode (127,0,0,1,#{hi},#{lo}).\r\n"
|
|
||||||
assert_equal("RETR bar\r\n", s.gets); s.print "150 file okay\r\n"
|
|
||||||
data_sock = data_serv.accept
|
|
||||||
begin
|
|
||||||
data_sock << content
|
|
||||||
ensure
|
|
||||||
data_sock.close
|
|
||||||
end
|
|
||||||
s.print "226 transfer complete\r\n"
|
|
||||||
assert_nil(s.gets)
|
|
||||||
}
|
|
||||||
ensure
|
ensure
|
||||||
s.close if s
|
Thread.kill(th)
|
||||||
|
th.join
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
begin
|
end
|
||||||
length = []
|
rescue LoadError
|
||||||
progress = []
|
# net-ftp is the bundled gems at Ruby 3.1
|
||||||
content = URI("ftp://#{host}:#{port}/%2Ffoo/b%61r;type=a").read(
|
|
||||||
:content_length_proc => lambda {|n| length << n },
|
|
||||||
:progress_proc => lambda {|n| progress << n })
|
|
||||||
assert_equal("content", content)
|
|
||||||
assert_equal([7], length)
|
|
||||||
assert_equal(7, progress.inject(&:+))
|
|
||||||
ensure
|
|
||||||
Thread.kill(th)
|
|
||||||
th.join
|
|
||||||
end
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ftp_over_http_proxy
|
def test_ftp_over_http_proxy
|
||||||
|
|
Loading…
Reference in a new issue