content-typeを修正するCGIスクリプトをpolipoで使う

Yahoo!ブログの画像のcontent-typeがtext/plainになってやがるからw3mじゃ画像が見られん。なんという手抜きだ(怒) 以前もcontent-typeがおかしいと苦情を送ったのだがシカトされたし。
IEとかでtext/plainな画像が見られるのは、IEが間違っているということ。標準に照し合わせると見られないのが正しい。

しかし、わめいていても始まらないので速攻でCGI proxyを書いてみた。プライベートなローカル鯖に置いておくと吉。ここでは http://127.0.0.1:9998/cgiproxy/set-content-type.cgi に設置するものとする。

#!/usr/local/bin/ruby
require 'open-uri'

content_type, url = ENV['QUERY_STRING'].split(/:/, 2)
puts "Content-Type: #{content_type}"
puts
print URI(url).read

poliposquid*1ならばURL転送機能(redirector)があるのでこんなスクリプトを書いて(polipo-redirector)おく。

#!/usr/bin/env ruby
DEFINITION = lambda do
# ここに設定を書く。
  set_content_type %r!^http://img\d?.blogs.yahoo.co.jp!, 'image/jpg'
end

def set_content_type(regexp, content_type)
  $url.replace("http://127.0.0.1:9998/cgiproxy/set-content-type.cgi?#{content_type}:#$url") if $url =~ regexp
end
  

def doit(&block)
  STDOUT.sync = true
  while $url = ARGF.gets
    $stderr.puts $url
    begin
      next if $url =~ %r!http://127.0.0.1:8339/!
      block[]
    ensure
      print $url
    end
  end
end

doit(&DEFINITION)

そして、 ~/.polipo

redirector = /r/bin/polipo-redirector

のようにパスを書いてpolipo再起動。

こうすれば http://img2.blogs.yahoo.co.jp などにアクセスしたときに http://127.0.0.1:9998/cgiproxy/set-content-type.cgi?image/jpg:画像のURL というURLにredirectされる。

*1:使ってないので動作確認してない。