Merge "(bug 37755) Set robot meta tags for 'view source' pages"
[lhc/web/wiklou.git] / includes / SquidPurgeClient.php
1 <?php
2 /**
3 * Squid and Varnish cache purging.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * An HTTP 1.0 client built for the purposes of purging Squid and Varnish.
25 * Uses asynchronous I/O, allowing purges to be done in a highly parallel
26 * manner.
27 *
28 * Could be replaced by curl_multi_exec() or some such.
29 */
30 class SquidPurgeClient {
31 var $host, $port, $ip;
32
33 var $readState = 'idle';
34 var $writeBuffer = '';
35 var $requests = array();
36 var $currentRequestIndex;
37
38 const EINTR = 4;
39 const EAGAIN = 11;
40 const EINPROGRESS = 115;
41 const BUFFER_SIZE = 8192;
42
43 /**
44 * The socket resource, or null for unconnected, or false for disabled due to error
45 */
46 var $socket;
47
48 var $readBuffer;
49
50 var $bodyRemaining;
51
52 /**
53 * @param $server string
54 * @param $options array
55 */
56 public function __construct( $server, $options = array() ) {
57 $parts = explode( ':', $server, 2 );
58 $this->host = $parts[0];
59 $this->port = isset( $parts[1] ) ? $parts[1] : 80;
60 }
61
62 /**
63 * Open a socket if there isn't one open already, return it.
64 * Returns false on error.
65 *
66 * @return bool|resource
67 */
68 protected function getSocket() {
69 if ( $this->socket !== null ) {
70 return $this->socket;
71 }
72
73 $ip = $this->getIP();
74 if ( !$ip ) {
75 $this->log( "DNS error" );
76 $this->markDown();
77 return false;
78 }
79 $this->socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
80 socket_set_nonblock( $this->socket );
81 wfSuppressWarnings();
82 $ok = socket_connect( $this->socket, $ip, $this->port );
83 wfRestoreWarnings();
84 if ( !$ok ) {
85 $error = socket_last_error( $this->socket );
86 if ( $error !== self::EINPROGRESS ) {
87 $this->log( "connection error: " . socket_strerror( $error ) );
88 $this->markDown();
89 return false;
90 }
91 }
92
93 return $this->socket;
94 }
95
96 /**
97 * Get read socket array for select()
98 * @return array
99 */
100 public function getReadSocketsForSelect() {
101 if ( $this->readState == 'idle' ) {
102 return array();
103 }
104 $socket = $this->getSocket();
105 if ( $socket === false ) {
106 return array();
107 }
108 return array( $socket );
109 }
110
111 /**
112 * Get write socket array for select()
113 * @return array
114 */
115 public function getWriteSocketsForSelect() {
116 if ( !strlen( $this->writeBuffer ) ) {
117 return array();
118 }
119 $socket = $this->getSocket();
120 if ( $socket === false ) {
121 return array();
122 }
123 return array( $socket );
124 }
125
126 /**
127 * Get the host's IP address.
128 * Does not support IPv6 at present due to the lack of a convenient interface in PHP.
129 */
130 protected function getIP() {
131 if ( $this->ip === null ) {
132 if ( IP::isIPv4( $this->host ) ) {
133 $this->ip = $this->host;
134 } elseif ( IP::isIPv6( $this->host ) ) {
135 throw new MWException( '$wgSquidServers does not support IPv6' );
136 } else {
137 wfSuppressWarnings();
138 $this->ip = gethostbyname( $this->host );
139 if ( $this->ip === $this->host ) {
140 $this->ip = false;
141 }
142 wfRestoreWarnings();
143 }
144 }
145 return $this->ip;
146 }
147
148 /**
149 * Close the socket and ignore any future purge requests.
150 * This is called if there is a protocol error.
151 */
152 protected function markDown() {
153 $this->close();
154 $this->socket = false;
155 }
156
157 /**
158 * Close the socket but allow it to be reopened for future purge requests
159 */
160 public function close() {
161 if ( $this->socket ) {
162 wfSuppressWarnings();
163 socket_set_block( $this->socket );
164 socket_shutdown( $this->socket );
165 socket_close( $this->socket );
166 wfRestoreWarnings();
167 }
168 $this->socket = null;
169 $this->readBuffer = '';
170 // Write buffer is kept since it may contain a request for the next socket
171 }
172
173 /**
174 * Queue a purge operation
175 *
176 * @param $url string
177 */
178 public function queuePurge( $url ) {
179 global $wgSquidPurgeUseHostHeader;
180 $url = SquidUpdate::expand( str_replace( "\n", '', $url ) );
181 $request = array();
182 if ( $wgSquidPurgeUseHostHeader ) {
183 $url = wfParseUrl( $url );
184 $host = $url['host'];
185 if ( isset( $url['port'] ) && strlen( $url['port'] ) > 0 ) {
186 $host .= ":" . $url['port'];
187 }
188 $path = $url['path'];
189 if ( isset( $url['query'] ) && is_string( $url['query'] ) ) {
190 $path = wfAppendQuery( $path, $url['query'] );
191 }
192 $request[] = "PURGE $path HTTP/1.1";
193 $request[] = "Host: $host";
194 } else {
195 $request[] = "PURGE $url HTTP/1.0";
196 }
197 $request[] = "Connection: Keep-Alive";
198 $request[] = "Proxy-Connection: Keep-Alive";
199 $request[] = "User-Agent: " . Http::userAgent() . ' ' . __CLASS__;
200 // Two ''s to create \r\n\r\n
201 $request[] = '';
202 $request[] = '';
203
204 $this->requests[] = implode( "\r\n", $request );
205 if ( $this->currentRequestIndex === null ) {
206 $this->nextRequest();
207 }
208 }
209
210 /**
211 * @return bool
212 */
213 public function isIdle() {
214 return strlen( $this->writeBuffer ) == 0 && $this->readState == 'idle';
215 }
216
217 /**
218 * Perform pending writes. Call this when socket_select() indicates that writing will not block.
219 */
220 public function doWrites() {
221 if ( !strlen( $this->writeBuffer ) ) {
222 return;
223 }
224 $socket = $this->getSocket();
225 if ( !$socket ) {
226 return;
227 }
228
229 if ( strlen( $this->writeBuffer ) <= self::BUFFER_SIZE ) {
230 $buf = $this->writeBuffer;
231 $flags = MSG_EOR;
232 } else {
233 $buf = substr( $this->writeBuffer, 0, self::BUFFER_SIZE );
234 $flags = 0;
235 }
236 wfSuppressWarnings();
237 $bytesSent = socket_send( $socket, $buf, strlen( $buf ), $flags );
238 wfRestoreWarnings();
239
240 if ( $bytesSent === false ) {
241 $error = socket_last_error( $socket );
242 if ( $error != self::EAGAIN && $error != self::EINTR ) {
243 $this->log( 'write error: ' . socket_strerror( $error ) );
244 $this->markDown();
245 }
246 return;
247 }
248
249 $this->writeBuffer = substr( $this->writeBuffer, $bytesSent );
250 }
251
252 /**
253 * Read some data. Call this when socket_select() indicates that the read buffer is non-empty.
254 */
255 public function doReads() {
256 $socket = $this->getSocket();
257 if ( !$socket ) {
258 return;
259 }
260
261 $buf = '';
262 wfSuppressWarnings();
263 $bytesRead = socket_recv( $socket, $buf, self::BUFFER_SIZE, 0 );
264 wfRestoreWarnings();
265 if ( $bytesRead === false ) {
266 $error = socket_last_error( $socket );
267 if ( $error != self::EAGAIN && $error != self::EINTR ) {
268 $this->log( 'read error: ' . socket_strerror( $error ) );
269 $this->markDown();
270 return;
271 }
272 } elseif ( $bytesRead === 0 ) {
273 // Assume EOF
274 $this->close();
275 return;
276 }
277
278 $this->readBuffer .= $buf;
279 while ( $this->socket && $this->processReadBuffer() === 'continue' );
280 }
281
282 /**
283 * @throws MWException
284 * @return string
285 */
286 protected function processReadBuffer() {
287 switch ( $this->readState ) {
288 case 'idle':
289 return 'done';
290 case 'status':
291 case 'header':
292 $lines = explode( "\r\n", $this->readBuffer, 2 );
293 if ( count( $lines ) < 2 ) {
294 return 'done';
295 }
296 if ( $this->readState == 'status' ) {
297 $this->processStatusLine( $lines[0] );
298 } else { // header
299 $this->processHeaderLine( $lines[0] );
300 }
301 $this->readBuffer = $lines[1];
302 return 'continue';
303 case 'body':
304 if ( $this->bodyRemaining !== null ) {
305 if ( $this->bodyRemaining > strlen( $this->readBuffer ) ) {
306 $this->bodyRemaining -= strlen( $this->readBuffer );
307 $this->readBuffer = '';
308 return 'done';
309 } else {
310 $this->readBuffer = substr( $this->readBuffer, $this->bodyRemaining );
311 $this->bodyRemaining = 0;
312 $this->nextRequest();
313 return 'continue';
314 }
315 } else {
316 // No content length, read all data to EOF
317 $this->readBuffer = '';
318 return 'done';
319 }
320 default:
321 throw new MWException( __METHOD__.': unexpected state' );
322 }
323 }
324
325 /**
326 * @param $line
327 * @return
328 */
329 protected function processStatusLine( $line ) {
330 if ( !preg_match( '!^HTTP/(\d+)\.(\d+) (\d{3}) (.*)$!', $line, $m ) ) {
331 $this->log( 'invalid status line' );
332 $this->markDown();
333 return;
334 }
335 list( , , , $status, $reason ) = $m;
336 $status = intval( $status );
337 if ( $status !== 200 && $status !== 404 ) {
338 $this->log( "unexpected status code: $status $reason" );
339 $this->markDown();
340 return;
341 }
342 $this->readState = 'header';
343 }
344
345 /**
346 * @param $line string
347 */
348 protected function processHeaderLine( $line ) {
349 if ( preg_match( '/^Content-Length: (\d+)$/i', $line, $m ) ) {
350 $this->bodyRemaining = intval( $m[1] );
351 } elseif ( $line === '' ) {
352 $this->readState = 'body';
353 }
354 }
355
356 protected function nextRequest() {
357 if ( $this->currentRequestIndex !== null ) {
358 unset( $this->requests[$this->currentRequestIndex] );
359 }
360 if ( count( $this->requests ) ) {
361 $this->readState = 'status';
362 $this->currentRequestIndex = key( $this->requests );
363 $this->writeBuffer = $this->requests[$this->currentRequestIndex];
364 } else {
365 $this->readState = 'idle';
366 $this->currentRequestIndex = null;
367 $this->writeBuffer = '';
368 }
369 $this->bodyRemaining = null;
370 }
371
372 /**
373 * @param $msg string
374 */
375 protected function log( $msg ) {
376 wfDebugLog( 'squid', __CLASS__." ($this->host): $msg\n" );
377 }
378 }
379
380 class SquidPurgeClientPool {
381
382 /**
383 * @var array of SquidPurgeClient
384 */
385 var $clients = array();
386 var $timeout = 5;
387
388 /**
389 * @param $options array
390 */
391 function __construct( $options = array() ) {
392 if ( isset( $options['timeout'] ) ) {
393 $this->timeout = $options['timeout'];
394 }
395 }
396
397 /**
398 * @param $client SquidPurgeClient
399 * @return void
400 */
401 public function addClient( $client ) {
402 $this->clients[] = $client;
403 }
404
405 public function run() {
406 $done = false;
407 $startTime = microtime( true );
408 while ( !$done ) {
409 $readSockets = $writeSockets = array();
410 /**
411 * @var $client SquidPurgeClient
412 */
413 foreach ( $this->clients as $clientIndex => $client ) {
414 $sockets = $client->getReadSocketsForSelect();
415 foreach ( $sockets as $i => $socket ) {
416 $readSockets["$clientIndex/$i"] = $socket;
417 }
418 $sockets = $client->getWriteSocketsForSelect();
419 foreach ( $sockets as $i => $socket ) {
420 $writeSockets["$clientIndex/$i"] = $socket;
421 }
422 }
423 if ( !count( $readSockets ) && !count( $writeSockets ) ) {
424 break;
425 }
426 $exceptSockets = null;
427 $timeout = min( $startTime + $this->timeout - microtime( true ), 1 );
428 wfSuppressWarnings();
429 $numReady = socket_select( $readSockets, $writeSockets, $exceptSockets, $timeout );
430 wfRestoreWarnings();
431 if ( $numReady === false ) {
432 wfDebugLog( 'squid', __METHOD__.': Error in stream_select: ' .
433 socket_strerror( socket_last_error() ) . "\n" );
434 break;
435 }
436 // Check for timeout, use 1% tolerance since we aimed at having socket_select()
437 // exit at precisely the overall timeout
438 if ( microtime( true ) - $startTime > $this->timeout * 0.99 ) {
439 wfDebugLog( 'squid', __CLASS__.": timeout ({$this->timeout}s)\n" );
440 break;
441 } elseif ( !$numReady ) {
442 continue;
443 }
444
445 foreach ( $readSockets as $key => $socket ) {
446 list( $clientIndex, ) = explode( '/', $key );
447 $client = $this->clients[$clientIndex];
448 $client->doReads();
449 }
450 foreach ( $writeSockets as $key => $socket ) {
451 list( $clientIndex, ) = explode( '/', $key );
452 $client = $this->clients[$clientIndex];
453 $client->doWrites();
454 }
455
456 $done = true;
457 foreach ( $this->clients as $client ) {
458 if ( !$client->isIdle() ) {
459 $done = false;
460 }
461 }
462 }
463 foreach ( $this->clients as $client ) {
464 $client->close();
465 }
466 }
467 }