Genderize Special:Preferences
[lhc/web/wiklou.git] / includes / SquidPurgeClient.php
index b0418ba..7d75f2c 100644 (file)
@@ -1,8 +1,29 @@
 <?php
 /**
- * An HTTP 1.0 client built for the purposes of purging Squid and Varnish. 
- * Uses asynchronous I/O, allowing purges to be done in a highly parallel 
- * manner. 
+ * Squid and Varnish cache purging.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * An HTTP 1.0 client built for the purposes of purging Squid and Varnish.
+ * Uses asynchronous I/O, allowing purges to be done in a highly parallel
+ * manner.
  *
  * Could be replaced by curl_multi_exec() or some such.
  */
@@ -23,7 +44,15 @@ class SquidPurgeClient {
         * The socket resource, or null for unconnected, or false for disabled due to error
         */
        var $socket;
-       
+
+       var $readBuffer;
+
+       var $bodyRemaining;
+
+       /**
+        * @param $server string
+        * @param $options array
+        */
        public function __construct( $server, $options = array() ) {
                $parts = explode( ':', $server, 2 );
                $this->host = $parts[0];
@@ -94,7 +123,7 @@ class SquidPurgeClient {
                return array( $socket );
        }
 
-       /** 
+       /**
         * Get the host's IP address.
         * Does not support IPv6 at present due to the lack of a convenient interface in PHP.
         */
@@ -147,11 +176,32 @@ class SquidPurgeClient {
         * @param $url string
         */
        public function queuePurge( $url ) {
+               global $wgSquidPurgeUseHostHeader;
                $url = SquidUpdate::expand( str_replace( "\n", '', $url ) );
-               $this->requests[] = "PURGE $url HTTP/1.0\r\n" .
-                       "Connection: Keep-Alive\r\n" .
-                       "Proxy-Connection: Keep-Alive\r\n" .
-                       "User-Agent: " . Http::userAgent() . ' ' . __CLASS__ . "\r\n\r\n";
+               $request = array();
+               if ( $wgSquidPurgeUseHostHeader ) {
+                       $url = wfParseUrl( $url );
+                       $host = $url['host'];
+                       if ( isset( $url['port'] ) && strlen( $url['port'] ) > 0 ) {
+                               $host .= ":" . $url['port'];
+                       }
+                       $path = $url['path'];
+                       if ( isset( $url['query'] ) && is_string( $url['query'] ) ) {
+                               $path = wfAppendQuery( $path, $url['query'] );
+                       }
+                       $request[] = "PURGE $path HTTP/1.1";
+                       $request[] = "Host: $host";
+               } else {
+                       $request[] = "PURGE $url HTTP/1.0";
+               }
+               $request[] = "Connection: Keep-Alive";
+               $request[] = "Proxy-Connection: Keep-Alive";
+               $request[] = "User-Agent: " . Http::userAgent() . ' ' . __CLASS__;
+               // Two ''s to create \r\n\r\n
+               $request[] = '';
+               $request[] = '';
+
+               $this->requests[] = implode( "\r\n", $request );
                if ( $this->currentRequestIndex === null ) {
                        $this->nextRequest();
                }
@@ -319,6 +369,9 @@ class SquidPurgeClient {
                $this->bodyRemaining = null;
        }
 
+       /**
+        * @param $msg string
+        */
        protected function log( $msg ) {
                wfDebugLog( 'squid', __CLASS__." ($this->host): $msg\n" );
        }
@@ -332,6 +385,9 @@ class SquidPurgeClientPool {
        var $clients = array();
        var $timeout = 5;
 
+       /**
+        * @param $options array
+        */
        function __construct( $options = array() ) {
                if ( isset( $options['timeout'] ) ) {
                        $this->timeout = $options['timeout'];
@@ -351,6 +407,9 @@ class SquidPurgeClientPool {
                $startTime = microtime( true );
                while ( !$done ) {
                        $readSockets = $writeSockets = array();
+                       /**
+                        * @var $client SquidPurgeClient
+                        */
                        foreach ( $this->clients as $clientIndex => $client ) {
                                $sockets = $client->getReadSocketsForSelect();
                                foreach ( $sockets as $i => $socket ) {
@@ -370,7 +429,7 @@ class SquidPurgeClientPool {
                        $numReady = socket_select( $readSockets, $writeSockets, $exceptSockets, $timeout );
                        wfRestoreWarnings();
                        if ( $numReady === false ) {
-                               wfDebugLog( 'squid', __METHOD__.': Error in stream_select: ' . 
+                               wfDebugLog( 'squid', __METHOD__.': Error in stream_select: ' .
                                        socket_strerror( socket_last_error() ) . "\n" );
                                break;
                        }