[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dennou-ruby:000992] Re: RubyNetCDF 0.2.0



In message "[dennou-ruby:000990] Re: RubyNetCDF 0.2.0"
    on Wed, 12 Dec 2001 17:27:00 +0900,
    Takeshi Horinouchi <horinout@xxxxxx> writes:

> ���ä����λפ��Ĥ��Ǥ������ƥѥå������ˤ�Ǥ�����󥹥ȡ��륳�ޥ�
> �ɤ�Ʊ���ե�����̾�Ǻ�äơ����Τ����Υǥ��쥯�ȥ꡼����������
> �ơ��ȥåץ�٥�Υ�����ץȤǡ��ƥѥå������� cd ���Ƽ¹Ԥ����
> �����Τ��������褦�ʵ������ޤ���¾�˲����ƤϤ���ޤ��󤫡�

�ե졼�������񤷤��ΤǤ���ä�ʬ����ޤ��󤬡��Ȥꤢ����
ɬ���פꤽ����fetch.rb�Ȥ����Τ�񤤤Ƥߤޤ�����

  $ ruby fetch <url>

�Ȥ����դ��˻Ȥ��ޤ���wget �� fetch �θ���Ū�ʤ�ΤǤ���
#! /usr/bin/env ruby
# $kNotwork: fetch/fetch.rb,v 1.2 2001/12/14 03:00:42 gotoken Exp $

require 'uri'
require 'net/http'
require 'net/ftp'
Net::HTTP.version_1_2

module NumRu
  module Fetch
    FetchError = Class.new(StandardError)
    
    def http_fetch(uri, header, proxy, out = STDOUT)
      proxyclass = Net::HTTP
      proxyclass = Net::HTTP::Proxy(proxy.host, proxy.port) if proxy

      while true
	host = uri.host
	port = uri.port
	request_uri = uri.request_uri
	http = proxyclass.new(host, port)
	res = http.get(request_uri, header)
	break if /3../ !~ res.code  # /3../ for redirection
	uri = uri.merge(URI::parse(res['location']))
      end

      out << res.body
    end
    module_function :http_fetch

    def ftp_fetch(uri, header, proxy, out = STDOUT)
      host = uri.host
      port = uri.port
      user = uri.user || "anonymous"
      pass = uri.password 
      remote_file = uri.path
      passive = header[:passive]
      ftp = Net::FTP.new(host)
      ftp.passive = passive
      ftp.login(user, pass)
      ftp.getbinaryfile(remote_file, "/dev/null"){|b|
	out << b
      }
    end
    module_function :ftp_fetch
    
    def fetch(uri, header, proxy, out)
      scheme = uri.scheme
      case scheme
      when "http"
	http_fetch(uri, header, proxy, out)
      when "ftp"
	ftp_fetch(uri, header, proxy, out)
      else
	raise NumRu::Fetch::FetchError, "unsupported scheme `#{scheme}'"
      end
    end
    module_function :fetch
  end
end


if __FILE__ == $0
  require 'getopts'
  getopts(nil, "lang:en")
  h = ENV['http_proxy']
  proxy = h ? URI::parse(h) : nil
  header = {"accept-language" => $OPT_lang}

  ARGV.each do |url|
    uri = URI::parse(url)
    local_file = uri.path.scan(%r|[^/]+$|).shift
    f = open(local_file, "w")
    begin
      NumRu::Fetch::fetch(uri, header, proxy, f)
    rescue
      f.close if f && !f.closed?
      File::delete(local_file)
    end
  end
end