6 checkcls('HttpRequest');
7 checkcls('HttpRequestPool');
15 ini_set('error_reporting', E_ALL);
17 class Pool extends HttpRequestPool
23 public final function __construct($urls_file = 'urls.txt', $cache_dir = 'HttpRequestPool_cache')
25 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
27 foreach (array_map('trim', file($urls_file)) as $url) {
28 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
34 public final function send()
37 $now = array_slice($this->all, 0, RMAX);
38 $this->rem = array_slice($this->all, RMAX);
44 foreach ($now as $url => $file) {
48 HttpRequest::METH_GET,
53 'connecttimeout' => TOUT,
54 'lastmodified' => is_file($file)?filemtime($file):0
60 while ($this->socketPerform()) {
61 if (!$this->socketSelect()) {
62 throw new HttpSocketException;
67 protected final function socketPerform()
70 $rc = parent::socketPerform();
71 } catch (HttpRequestException $x) {
72 // a request may have thrown an exception,
73 // but it is still save to continue
74 echo $x->getMessage(), "\n";
77 foreach ($this->getFinishedRequests() as $r) {
81 $c = $r->getResponseCode();
82 $b = $r->getResponseBody();
84 printf("%d %s %d\n", $c, $u, strlen($b));
86 if ($c == 200 && $this->dir) {
87 file_put_contents($this->all[$u], $b);
90 if ($a = each($this->rem)) {
91 list($url, $file) = $a;
95 HttpRequest::METH_GET,
100 'connecttimeout' => TOUT,
101 'lastmodified' => is_file($file)?filemtime($file):0
111 define('GZIP', true);
114 chdir(dirname(__FILE__));
116 $time = microtime(true);
118 printf("Elapsed: %0.3fs\n", microtime(true)-$time);