- changelog
[m6w6/ext-http] / tests / HttpRequestPool_003.phpt
1 --TEST--
2 HttpRequestPool chain
3 --SKIPIF--
4 <?php
5 include 'skip.inc';
6 checkcls('HttpRequest');
7 checkcls('HttpRequestPool');
8 ?>
9 --FILE--
10 <?php
11
12 echo "-TEST\n";
13
14 set_time_limit(0);
15 ini_set('error_reporting', E_ALL);
16 ini_set('html_errors', 0);
17
18 class Pool extends HttpRequestPool
19 {
20 private $all;
21 private $rem;
22 private $dir;
23
24 public final function __construct($urls_file = 'urls.txt', $cache_dir = 'HttpRequestPool_cache')
25 {
26 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
27
28 foreach (array_map('trim', file($urls_file)) as $url) {
29 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
30 }
31
32 $this->send();
33 }
34
35 public final function send()
36 {
37 if (RMAX) {
38 $now = array_slice($this->all, 0, RMAX);
39 $this->rem = array_slice($this->all, RMAX);
40 } else {
41 $now = $urls;
42 $this->rem = array();
43 }
44
45 foreach ($now as $url => $file) {
46 $this->attach(
47 new HttpRequest(
48 $url,
49 HttpRequest::METH_GET,
50 array(
51 'redirect' => 5,
52 'compress' => GZIP,
53 'timeout' => TOUT,
54 'connecttimeout' => TOUT,
55 'lastmodified' => is_file($file)?filemtime($file):0
56 )
57 )
58 );
59 }
60
61 while ($this->socketPerform()) {
62 if (!$this->socketSelect()) {
63 throw new HttpSocketException;
64 }
65 }
66 }
67
68 protected final function socketPerform()
69 {
70 try {
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";
76 }
77
78 foreach ($this->getFinishedRequests() as $r) {
79 $this->detach($r);
80
81 $u = $r->getUrl();
82 $c = $r->getResponseCode();
83 $b = $r->getResponseBody();
84
85 printf("%d %s %d\n", $c, $u, strlen($b));
86
87 if ($c == 200 && $this->dir) {
88 file_put_contents($this->all[$u], $b);
89 }
90
91 if ($a = each($this->rem)) {
92 list($url, $file) = $a;
93 $this->attach(
94 new HttpRequest(
95 $url,
96 HttpRequest::METH_GET,
97 array(
98 'redirect' => 5,
99 'compress' => GZIP,
100 'timeout' => TOUT,
101 'connecttimeout' => TOUT,
102 'lastmodified' => is_file($file)?filemtime($file):0
103 )
104 )
105 );
106 }
107 }
108 return $rc;
109 }
110 }
111
112 define('GZIP', true);
113 define('TOUT', 50);
114 define('RMAX', 10);
115 chdir(dirname(__FILE__));
116
117 $time = microtime(true);
118 $pool = new Pool();
119 printf("Elapsed: %0.3fs\n", microtime(true)-$time);
120
121 echo "Done\n";
122 ?>
123 --EXPECTF--
124 %sTEST
125 %d %s %d
126 %d %s %d
127 %d %s %d
128 %d %s %d
129 %d %s %d
130 %d %s %d
131 %d %s %d
132 %d %s %d
133 %d %s %d
134 %d %s %d
135 %d %s %d
136 %d %s %d
137 %d %s %d
138 %d %s %d
139 %d %s %d
140 %d %s %d
141 %d %s %d
142 %d %s %d
143 %d %s %d
144 %d %s %d
145 %d %s %d
146 %d %s %d
147 %d %s %d
148 %d %s %d
149 %d %s %d
150 %d %s %d
151 %d %s %d
152 %d %s %d
153 %d %s %d
154 %d %s %d
155 %d %s %d
156 %d %s %d
157 %d %s %d
158 %d %s %d
159 %d %s %d
160 %d %s %d
161 %d %s %d
162 %d %s %d
163 %d %s %d
164 %d %s %d
165 %d %s %d
166 %d %s %d
167 %d %s %d
168 %d %s %d
169 %d %s %d
170 %d %s %d
171 %d %s %d
172 %d %s %d
173 %d %s %d
174 Elapsed: %fs
175 Done