home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rbemx144.zip / ruby-1.4.4 / sample / dualstack-fetch.rb < prev    next >
Text File  |  1999-06-03  |  1KB  |  49 lines

  1. # simple webpage fetcher
  2.  
  3. # The code demonstrates how a multi-protocol client should be written.
  4. # TCPsocket is using getaddrinfo() internally, so there should be no problem.
  5.  
  6. require "socket"
  7.  
  8. if ARGV.size != 1
  9.   STDERR.print "requires URL\n"
  10.   exit
  11. end
  12.  
  13. url = ARGV[0]
  14. if url !~ /^http:\/\/([^\/]+)(\/.*)$/
  15.   STDERR.print "only http with full hostname is supported\n"
  16.   exit
  17. end
  18.  
  19. # split URL into host, port and path
  20. hostport = $1
  21. path = $2
  22. if (hostport =~ /^(.*):([0-9]+)$/)
  23.   host = $1
  24.   port = $2
  25. else
  26.   host = hostport
  27.   port = 80
  28. end
  29. if host =~ /^\[(.*)\]$/
  30.   host = $1
  31. end
  32.  
  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"
  37.  
  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"
  45. c.print "\n"
  46. while c.gets
  47.   print
  48. end
  49.