6 checkcls('HttpRequest');
7 checkcls('HttpRequestPool');
15 ini_set('error_reporting', E_ALL);
16 ini_set('html_errors', 0);
18 class Pool extends HttpRequestPool
24 public final function __construct($urls_file = 'urls.txt', $cache_dir = 'HttpRequestPool_cache')
26 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
28 foreach (array_map('trim', file($urls_file)) as $url) {
29 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
35 public final function send()
38 $now = array_slice($this->all, 0, RMAX);
39 $this->rem = array_slice($this->all, RMAX);
45 foreach ($now as $url => $file) {
49 HttpRequest::METH_GET,
54 'connecttimeout' => TOUT,
55 'lastmodified' => is_file($file)?filemtime($file):0
61 while ($this->socketPerform()) {
62 if (!$this->socketSelect()) {
63 throw new HttpSocketException;
68 protected final function socketPerform()
71 $rc = parent::socketPerform();
72 } catch (HttpRequestException $x) {
73 // a request may have thrown an exception,
74 // but it is still save to continue
75 echo $x->getMessage(), "\n";
78 foreach ($this->getFinishedRequests() as $r) {
82 $c = $r->getResponseCode();
83 $b = $r->getResponseBody();
85 printf("%d %s %d\n", $c, $u, strlen($b));
87 if ($c == 200 && $this->dir) {
88 file_put_contents($this->all[$u], $b);
91 if ($a = each($this->rem)) {
92 list($url, $file) = $a;
96 HttpRequest::METH_GET,
101 'connecttimeout' => TOUT,
102 'lastmodified' => is_file($file)?filemtime($file):0
112 define('GZIP', true);
115 chdir(dirname(__FILE__));
117 $time = microtime(true);
119 printf("Elapsed: %0.3fs\n", microtime(true)-$time);