fetus Diary
2007/12/22(土) - HTTP_Client の挙動がムカつく
追記: 実装がダメダメなので こっちへ。
HTTP_Client の meta refresh の挙動が気に入らないので継承して動作変更
<?php
require_once('HTTP/Client.php');
class HTTP_Client2 extends HTTP_Client {
var $_meta_refresh_threadshold = 5;
function setMetaRefreshThreadshold($second) {
$this->_meta_refresh_threadshold = max((int)$second, 0);
}
function _getMetaRedirect(&$request) {
if(substr($request->getResponseHeader('content-type'), 0, 9) !== 'text/html' &&
substr($request->getResponseHeader('content-type'), 0, 21) !== 'application/xhtml+xml')
{
return null;
}
if(($body = $request->getResponseBody()) == '') {
return null;
}
if(! preg_match_all('!<meta\s+[^>]*?>!is', $body, $matches)) {
return null;
}
foreach($matches[0] as $meta) {
if(preg_match('!\shttp-equiv\s*=\s*(["\']?)refresh\1(?:\s|>)!is', $meta)) {
if(preg_match('!\scontent\s*=\s*(["\'])\s*(\d+)?\s*;\s*URL=\s*(\S+)\s*\1(?:\s|>)!is', $meta, $match)) {
// match[2] = refresh sec
// match[3] = url
if($match[3] != '' && $match[2] <= $this->_meta_refresh_threadshold) {
$prev_url = $request->_url->getUrl();
$next_url = $this->_redirectUrl($request->_url, html_entity_decode($match[3]));
if($next_url && $next_url != $prev_url) {
return $next_url;
}
}
}
}
}
return null;
}
}
?>
もともとの動作は、<meta http-equiv="Refresh" content="5;URL=http://example.com/"> こんな要素を検出して、リダイレクト指示を返す。
このとき、オリジナルのコードだと転送開始秒数見てないのね。つまり、2 時間後なんて書いてあってもリダイレクトしちゃう。2 時間後なんて書いてある HTML が無いと思ったら大間違い。自動ログアウト機能を備えたサイトが意外とあったりする。
そんなわけで、転送閾値を設定してリダイレクトするか判定するようにしたのが件のコード。まともに動くかどうかは知らない。Content-Type とかで微妙に仕様が違うのは、キニスンナ。
07/12/23 2:5108/05/09 9:47
関係あるかも?
このエントリは次のエントリから参照されているみたいです
- 2007/12/24(月) - HiNa - HTTP_Client2
- 2007/12/24(月) - HiNa - 結果
コメント
コメントはありません。