From: TAKAHASHI Masayoshi Date: 1999-05-27T12:32:39+09:00 Subject: [ruby-dev:7002] Re: http-access 高橋です。http-accessについて。 Takahiro Maebashi さん: > いやあ、その後は何も変化はありません。 そうでしたか。 > > # あれ、URLってhostとportの書式は決まってないんでしたっけ? > > RFC1738 にあるみたいです。 > > //:@:/ 一応RFCこれってどのschemeで使えるか、というのは決まってないん ですよね? RFC 2396にはなさそうでした。 > > 試しに自分でも書きかけているのですが(^^;、すでにあるのなら > > それを使いたいです。 > そういうわけで書いて頂けると嬉しいです(^^; わかりました。(^^;; というわけで、とりあえず書いてみたurl-parse.rbの差分をつけておきます。 user, passwd, host, portというメソッドを加えてあります。 で、http-accessについてなのですが、使い方についての質問があります。 require 'http-access' require 'url-parse' $proxy= nil k = "www.nowhere-net.or.jp" url1 = "/~some/index.html" url2 = "/~another/index.html" h = HTTPAccess.new(k, HTTPAccess::HTTP_Port, $proxy) h.request_head(url1) h.get_header{|head| print head } h.request_head(url2) h.get_header{|head| print head } h.close といったようなことを書いても、どうも同じファイルを見に行ってしまっ ている(Etag, Last-modified,などみんな同じ)ようなのですが、これはど うしてなのでしょうか? 何か使い方がまずいのでしょうか。 *** url-parse.rb.org Tue May 25 09:15:41 1999 --- url-parse.rb Tue May 25 18:22:38 1999 *************** *** 1,10 **** --- 1,14 ---- # Parse URLs. class URL attr_reader :urlstr, :scheme, :netloc, :path, :params, :query, :fragment + attr_reader :user, :passwd, :host, :port Uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet', 'wais', 'https', 'shttp', 'snews', 'prospero', ''] + Uses_login = ['ftp','http','gopher','nntp','telnet','wais', + 'prospero'] + def initialize(urlstr, scheme=nil) @urlstr = urlstr parse(urlstr, scheme) *************** *** 15,20 **** --- 19,25 ---- def parse(url, scheme = nil, allow_fragments = true) @scheme = scheme @netloc = @params = @query = @fragment = nil + @host = @port = nil if url =~ /^([-+\.\w]+):/ @scheme, url = $1, $' #' *************** *** 29,34 **** --- 34,46 ---- @scheme = 'file' end + # parse netloc into userinfo and hostinfo + # for more detail, see RFC 2396 3.2.2 and Appendix A. + if /(([^:@]*)(:([^@]*))?@)?([^:]*)(:(\d*))?/ =~ @netloc && + Uses_login.include?(@scheme) + @user, @passwd, @host, @port = $2, $4, $5, $7 + end + if @scheme == 'http' i = url.rindex("#") if i *************** *** 48,51 **** --- 60,72 ---- @path = '/' if @path == '' end + end + + if $0 == __FILE__ + u = URL.new('http://foo:var@www.hogehoge.jp:8080/hoge/fu;ni?ha#un') + + ['urlstr', 'scheme', 'netloc', 'host', 'port', 'user', + 'passwd', 'path', 'params', 'query', 'fragment'].each{|m| + print m,": ",eval("u." + m),"\n" + } end