1 # simple webpage fetcher
3 # The code demonstrates how a multi-protocol client should be written.
4 # TCPSocket is using getaddrinfo() internally, so there should be no problem.
9 STDERR.print "requires URL\n"
14 if url !~ /^http:\/\/([^\/]+)(\/.*)$/
15 STDERR.print "only http with full hostname is supported\n"
19 # split URL into host, port and path
22 if (hostport =~ /^(.*):([0-9]+)$/)
29 if host =~ /^\[(.*)\]$/
33 #STDERR.print "url=<#{ARGV[0]}>\n"
34 #STDERR.print "host=<#{host}>\n"
35 #STDERR.print "port=<#{port}>\n"
36 #STDERR.print "path=<#{path}>\n"
38 STDERR.print "conntecting to #{host} port #{port}\n"
39 c = TCPSocket.new(host, port)
40 dest = Socket.getnameinfo(c.getpeername,
41 Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)
42 STDERR.print "conntected to #{dest[0]} port #{dest[1]}\n"
43 c.print "GET #{path} HTTP/1.0\n"
44 c.print "Host: #{host}\n"