From: Wakou Aoyama Date: 1999-06-08T14:28:18+09:00 Subject: [ruby-list:14916] Re: HTTP Client with Ruby 青山です。 Tue, Jun 08, 1999 at 12:50:02AM +0900 において Wakou Aoyama さん曰く: > こんな感じです。という事で、HTTP 1.0 の場合ではなく、データの取得後に > eof に達していたら close で(www2c.biglobe.ne.jp は)行けそうですね。 問題ありでした。この対応の仕方では HTTP 1.1 のホストで動かなくなります。 素直に status と connection の両方を見るのが良さそうですね。 #!/usr/local/bin/ruby require "socket" CR = "\015" LF = "\012" EOL = CR + LF def get_head(host, port, files) heads = {} sock = nil for i in 0 ... files.size if sock == nil or sock.closed? sock = TCPsocket.open(host, port) sock.binmode STDERR.print "server open.\n" end if i == files.size - 1 connection = "Close" else connection = "Keep-Alive" end sock << "HEAD " << files[i] << " HTTP/1.1" << EOL sock << "Host: " << host << EOL sock << "Connection: " << connection << EOL sock << EOL status = sock.gets(LF) if CR == status[status.size - 2].chr eol = EOL else eol = LF end head = sock.gets(eol + eol) if (/Connection:\s+Close/ni === head) or ((/HTTP\/1\.0/ni === status) and (not /Connection:\s+Keep-Alive/ni === head)) sock.close if not sock.closed? end heads[files[i]] = head end sock.close if not sock.closed? heads end port = "80" host = "www.jin.gr.jp" files = %w[ /~nahi/ /~nahi/Ruby/ /~nahi/link-Ruby.html ] # Keep-Alive を受け付けないホストの例 #host = "www.isc.meiji.ac.jp" #files = %w[ /~ee77038/ /~ee77038/ruby/index.html /~ee77038/ruby/program.htm ] # HTTP 1.0 オンリーなホストの例 #host = "www2c.biglobe.ne.jp" #files = %w[ / /~hosoda/ ] get_head(host, port, files).each do |file, head| print file, "\n" print head end -- 青山 和光 Wakou Aoyama