From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/sample/dualstack-fetch.rb | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 jni/ruby/sample/dualstack-fetch.rb (limited to 'jni/ruby/sample/dualstack-fetch.rb') diff --git a/jni/ruby/sample/dualstack-fetch.rb b/jni/ruby/sample/dualstack-fetch.rb new file mode 100644 index 0000000..18d33cc --- /dev/null +++ b/jni/ruby/sample/dualstack-fetch.rb @@ -0,0 +1,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 -- cgit v1.2.3