- fix request errors hidden under the hood of the request pool
[m6w6/ext-http] / tests / HttpRequestPool_003.phpt
1 --TEST--
2 HttpRequestPool chain
3 --SKIPIF--
4 <?php
5 inlcude '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
17 class Pool extends HttpRequestPool
18 {
19 private $all;
20 private $rem;
21 private $dir;
22
23 public final function __construct($urls_file = 'urls.txt', $cache_dir = 'HttpRequestPool_cache')
24 {
25 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
26
27 foreach (array_map('trim', file($urls_file)) as $url) {
28 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
29 }
30
31 $this->send();
32 }
33
34 public final function send()
35 {
36 if (RMAX) {
37 $now = array_slice($this->all, 0, RMAX);
38 $this->rem = array_slice($this->all, RMAX);
39 } else {
40 $now = $urls;
41 $this->rem = array();
42 }
43
44 foreach ($now as $url => $file) {
45 $this->attach(
46 new HttpRequest(
47 $url,
48 HttpRequest::METH_GET,
49 array(
50 'redirect' => 5,
51 'timeout' => 50,
52 'connecttimeout' => 10,
53 'lastmodified' => is_file($file)?filemtime($file):0
54 )
55 )
56 );
57 }
58
59 while ($this->socketPerform()) {
60 if (!$this->socketSelect()) {
61 throw new HttpSocketException;
62 }
63 }
64 }
65
66 protected final function socketPerform()
67 {
68 try {
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";
74 }
75
76 foreach ($this->getFinishedRequests() as $r) {
77 $this->detach($r);
78
79 $u = $r->getUrl();
80 $c = $r->getResponseCode();
81 $b = $r->getResponseBody();
82
83 printf("%d %s %d\n", $c, $u, strlen($b));
84
85 if ($c == 200 && $this->dir) {
86 file_put_contents($this->all[$u], $b);
87 }
88
89 if ($a = each($this->rem)) {
90 list($url, $file) = $a;
91 $this->attach(
92 new HttpRequest(
93 $url,
94 HttpRequest::METH_GET,
95 array(
96 'redirect' => 3,
97 'timeout' => 25,
98 'compress' => true,
99 'connecttimeout' => 10,
100 'lastmodified' => is_file($file)?filemtime($file):0
101 )
102 )
103 );
104 }
105 }
106 return $rc;
107 }
108 }
109
110 define('RMAX', 10);
111 chdir(dirname(__FILE__));
112
113 $time = microtime(true);
114 new Pool();
115 printf("Elapsed: %0.3fs\n", microtime(true)-$time);
116
117 echo "Done\n";
118 ?>
119 --EXPECTF--
120 %sTEST
121 %d %s %d
122 %d %s %d
123 %d %s %d
124 %d %s %d
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 Elapsed: %fs
171 Done