48f6adfe9bc7864f9e84063b582bddb252d3120b
[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
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 'compress' => GZIP,
52 'timeout' => TOUT,
53 'connecttimeout' => TOUT,
54 'lastmodified' => is_file($file)?filemtime($file):0
55 )
56 )
57 );
58 }
59
60 while ($this->socketPerform()) {
61 if (!$this->socketSelect()) {
62 throw new HttpSocketException;
63 }
64 }
65 }
66
67 protected final function socketPerform()
68 {
69 try {
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";
75 }
76
77 foreach ($this->getFinishedRequests() as $r) {
78 $this->detach($r);
79
80 $u = $r->getUrl();
81 $c = $r->getResponseCode();
82 $b = $r->getResponseBody();
83
84 printf("%d %s %d\n", $c, $u, strlen($b));
85
86 if ($c == 200 && $this->dir) {
87 file_put_contents($this->all[$u], $b);
88 }
89
90 if ($a = each($this->rem)) {
91 list($url, $file) = $a;
92 $this->attach(
93 new HttpRequest(
94 $url,
95 HttpRequest::METH_GET,
96 array(
97 'redirect' => 5,
98 'compress' => GZIP,
99 'timeout' => TOUT,
100 'connecttimeout' => TOUT,
101 'lastmodified' => is_file($file)?filemtime($file):0
102 )
103 )
104 );
105 }
106 }
107 return $rc;
108 }
109 }
110
111 define('GZIP', true);
112 define('TOUT', 50);
113 define('RMAX', 10);
114 chdir(dirname(__FILE__));
115
116 $time = microtime(true);
117 $p = new Pool();
118 printf("Elapsed: %0.3fs\n", microtime(true)-$time);
119
120 echo "Done\n";
121 ?>
122 --EXPECTF--
123 %sTEST
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 %d %s %d
171 %d %s %d
172 %d %s %d
173 Elapsed: %fs
174 Done