File: dualstack-fetch.rb

package info (click to toggle)
ruby1.8 1.8.7.302-2squeeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 26,640 kB
  • ctags: 38,616
  • sloc: ruby: 245,002; ansic: 144,156; yacc: 5,890; sh: 2,677; lisp: 1,626; tcl: 949; makefile: 358; sed: 129; xml: 122; awk: 36; cpp: 28; asm: 25; perl: 18; python: 6
file content (48 lines) | stat: -rw-r--r-- 1,044 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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