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,
52 'connecttimeout' => 10,
53 'lastmodified' => is_file($file)?filemtime($file):0
59 while ($this->socketPerform()) {
60 if (!$this->socketSelect()) {
61 throw new HttpSocketException;
66 protected final function socketPerform()
69 $rc = parent::socketPerform();
70 } catch (HttpRequestException $x) {
71 // a request may have thrown an exception,
72 // but it is still save to continue
73 echo $x->getMessage(), "\n";
76 foreach ($this->getFinishedRequests() as $r) {
80 $c = $r->getResponseCode();
81 $b = $r->getResponseBody();
83 printf("%d %s %d\n", $c, $u, strlen($b));
85 if ($c == 200 && $this->dir) {
86 file_put_contents($this->all[$u], $b);
89 if ($a = each($this->rem)) {
90 list($url, $file) = $a;
94 HttpRequest::METH_GET,
99 'connecttimeout' => 10,
100 'lastmodified' => is_file($file)?filemtime($file):0
111 chdir(dirname(__FILE__));
113 $time = microtime(true);
115 printf("Elapsed: %0.3fs\n", microtime(true)-$time);