[ruby-dev:38728] open-uri の content_type

View: New views
1 Messages — Rating Filter:   Alert me  

[ruby-dev:38728] open-uri の content_type

by Kazuhiro NISHIYAMA :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

西山和広です。

以下のように適当な FC2 のブログを open-uri で開くと content_type が
"application/octet-stream" になってしまいます。
"text/html" が返ってきてほしいと思ったのですが、どうでしょうか?


% cat a.rb
#!/usr/bin/env ruby
require 'open-uri'
open("http://(略).fc2.com/") do |f|
  p f.content_type
  p f.meta["content-type"]
end
% ruby a.rb
"application/octet-stream"
"text/html, text/html;charset=euc-jp"
% w3m -dump_head http://(略).fc2.com/ | grep Content-Type
Content-Type: text/html
Content-Type: text/html;charset=euc-jp
%


w3m -dump_head のように直接レスポンスヘッダをみてみると
Content-Type が2行返ってきていて、open-uri の

    resp.each {|name,value| buf.io.meta_add_field name, value }

という処理をみると後が優先されるのかと思ったのですが、
更に net/http.rb の方をみてみると、

    def [](key)
      a = @header[key.downcase] or return nil
      a.join(', ')
    end

となっていて、meta_add_field には既に join(', ') されてしまっている
文字列が渡ってきているようです。


--
|ZnZ(ゼット エヌ ゼット)
|西山和広(Kazuhiro NISHIYAMA)