続:Mechanize2.0 ではまる

今度は mixi にログインできない。ログを見ると同じくCookieが送られていない。Mechanize 1.0.0 を使うと問題ない。今回はドメインの問題では無いので、また別の原因があると思われる。

Mechanize2ではCookieは鬼門か?!

2011-11-12 追記

原因は判明。Set-Cookie で domain=.mixi.jp のCookieが送られてくるが、これが、mixi.jp に対してのドメイン検査に引っかかって送られていない。これはバグだ。

class Mechanize::CookieJar
  (中略)
  def cookies(url)
      (中略)
      if cookie_domain.start_with?('.')
        url.host =~ /#{Regexp.escape cookie_domain}$/i
      else
        url.host =~ /^#{Regexp.escape cookie_domain}$/i
      end
      (後略)

これを、

class Mechanize::CookieJar
  (中略)
  def cookies(url)
      (中略)
      if cookie_domain.start_with?('.')
        "."+url.host =~ /#{Regexp.escape cookie_domain}$/i
      else
        url.host =~ /^#{Regexp.escape cookie_domain}$/i
      end
      (後略)

に少し修正すればOK。


キタモモンガさんのブログ*1にも、Cookieがおかしいという話が書いてあるが、これは、上記の話じゃ無くて「Mechanize2.0 ではまる」id:otn:20110924 の話だ。

*1:id:kitamomonga:20111011:ruby_mechanize_2_0_cookie_failure