Changeset 163

Show
Ignore:
Timestamp:
03/11/08 14:29:06 (5 years ago)
Author:
Seldaek
Message:

Url: Should fix the choking bug on very slowly loading pages (thanks scoates for the proof of concept)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/Phergie/Plugin/Url.php

    r161 r163  
    2121    */ 
    2222    protected $format = '%nick%: %title% [ %link% ]'; 
     23    protected $titleOnlyFormat = '%title%'; 
    2324 
    2425        /** 
     
    5051        if ($format) { 
    5152            $this->format = $format; 
     53        } 
     54        $titleOnlyFormat = $this->getPluginIni('title_only_format'); 
     55        if ($titleOnlyFormat) { 
     56            $this->titleOnlyFormat = $titleOnlyFormat; 
    5257        } 
    5358    } 
     
    7681                $opts = array('http' => 
    7782                    array( 
    78                         'timeout' => 4, 
     83                        'timeout' => 3.5, 
    7984                        'method' => 'GET', 
    8085                        'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12' 
     
    9398                    if (!isset ($title)) { 
    9499                        $content = ''; 
    95                         while ($chunk = fread($page, 512)) { 
     100                        $tstamp = time()+4; 
     101                        while ($chunk = fread($page, 1)) { 
    96102                            $content .= $chunk; 
     103                            // Check for timeout 
     104                            if(time() > $tstamp) 
     105                                break; 
     106                            // Skip checks unless we read 100 bytes, no need to kill the cpu 
     107                            if (!(strlen($content) % 100)) { 
     108                                continue; 
     109                            } 
     110                            // Try to read title 
    97111                            if (preg_match('#<title[^>]*>([^<]*)#is', $content, $m)) { 
    98                                 $content .= fread($page, 256); 
     112                                // Start another loop to grab some more data in order to be sure we have the complete title 
     113                                $loop = 120; 
     114                                        while (($chunk = fread($page, 1)) && $loop--) { 
     115                                            $content .= $chunk; 
     116                                            // Check for timeout 
     117                                            if(time() > $tstamp) 
     118                                                break; 
     119                                        } 
    99120                                preg_match('#<title[^>]*>([^<]*)#is', $content, $m); 
    100121                                $title = preg_replace('#\s+#', ' ', $m[1]); 
    101                                 $title = $this->decode($title, $titleLength); 
     122                                $title = trim($this->decode($title, $titleLength)); 
    102123                                break; 
    103124                            } 
     125                            // Title won't appear beyond that point so stop parsing 
    104126                            if (preg_match('#</head>|<body#i', $content)) { 
    105127                                break; 
     
    110132                } 
    111133 
    112                 if (!isset($title)) { 
     134                if (!isset($title) || empty($title)) { 
    113135                        if ($tinyUrl === $url) { 
    114136                                unset($tinyUrl, $url); 
     
    118140                } 
    119141 
     142 
     143                // Send url and title 
    120144                $this->doPrivmsg( 
    121145                    $this->event->getSource(), 
     
    148172        // and minimize the size of the cache for less cache bloat. 
    149173        $url = dechex(crc32($url)); $tiny = dechex(crc32($tiny)); 
    150         $cache = array('url' => $this->urlCache[$source][$url], 'tiny' => $this->tinyCache[$source][$tiny]); 
     174        $cache = array 
     175        ( 
     176                        'url' => isset($this->urlCache[$source][$url]) ? $this->urlCache[$source][$url] : null, 
     177                'tiny' => isset($this->tinyCache[$source][$tiny]) ? $this->tinyCache[$source][$tiny] : null 
     178        ); 
    151179 
    152180        $expire = $this->expire;