mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
be1d2c5f68
substring, slice! should return nil without exception. * array.c (rb_ary_insert): type fixed. * string.c (rb_str_split_m): accept separator value nil as well. * string.c (rb_str_become): was leaking memory. * class.c (rb_include_module): should not alter other classes/modules by inclusion. by this fix, local order may not be preserved for some cases. * class.c (include_class_new): module may be T_ICLASS; retrieve original module information. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2632 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
48 lines
1 KiB
Ruby
48 lines
1 KiB
Ruby
# simple webpage fetcher
|
|
|
|
# The code demonstrates how a multi-protocol client should be written.
|
|
# TCPSocket is using getaddrinfo() internally, so there should be no problem.
|
|
|
|
require "socket"
|
|
|
|
if ARGV.size != 1
|
|
STDERR.print "requires URL\n"
|
|
exit
|
|
end
|
|
|
|
url = ARGV[0]
|
|
if url !~ /^http:\/\/([^\/]+)(\/.*)$/
|
|
STDERR.print "only http with full hostname is supported\n"
|
|
exit
|
|
end
|
|
|
|
# split URL into host, port and path
|
|
hostport = $1
|
|
path = $2
|
|
if (hostport =~ /^(.*):([0-9]+)$/)
|
|
host = $1
|
|
port = $2
|
|
else
|
|
host = hostport
|
|
port = 80
|
|
end
|
|
if host =~ /^\[(.*)\]$/
|
|
host = $1
|
|
end
|
|
|
|
#STDERR.print "url=<#{ARGV[0]}>\n"
|
|
#STDERR.print "host=<#{host}>\n"
|
|
#STDERR.print "port=<#{port}>\n"
|
|
#STDERR.print "path=<#{path}>\n"
|
|
|
|
STDERR.print "conntecting to #{host} port #{port}\n"
|
|
c = TCPSocket.new(host, port)
|
|
dest = Socket.getnameinfo(c.getpeername,
|
|
Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)
|
|
STDERR.print "conntected to #{dest[0]} port #{dest[1]}\n"
|
|
c.print "GET #{path} HTTP/1.0\n"
|
|
c.print "Host: #{host}\n"
|
|
c.print "\n"
|
|
while c.gets
|
|
print
|
|
end
|