HTCPPurge() early exit on socket error
[lhc/web/wiklou.git] / includes / cache / SquidUpdate.php
index 39bf4c9..6cc59b7 100644 (file)
@@ -125,11 +125,11 @@ class SquidUpdate {
        static function purge( $urlArr ) {
                global $wgSquidServers, $wgHTCPMulticastRouting;
 
-               if( !$urlArr ) {
+               if ( !$urlArr ) {
                        return;
                }
 
-               wfDebug( "Squid purge: " . implode( ' ', $urlArr ) . "\n" );
+               wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) . "\n" );
 
                if ( $wgHTCPMulticastRouting ) {
                        SquidUpdate::HTCPPurge( $urlArr );
@@ -172,7 +172,7 @@ class SquidUpdate {
                $htcpOpCLR = 4; // HTCP CLR
 
                // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
-               if( !defined( "IPPROTO_IP" ) ) {
+               if ( !defined( "IPPROTO_IP" ) ) {
                        define( "IPPROTO_IP", 0 );
                        define( "IP_MULTICAST_LOOP", 34 );
                        define( "IP_MULTICAST_TTL", 33 );
@@ -180,55 +180,63 @@ class SquidUpdate {
 
                // pfsockopen doesn't work because we need set_sock_opt
                $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
-               if ( $conn ) {
-                       // Set socket options
-                       socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
-                       if ( $wgHTCPMulticastTTL != 1 )
-                               socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
-                                       $wgHTCPMulticastTTL );
+               if ( ! $conn ) {
+                       $errstr = socket_strerror( socket_last_error() );
+                       wfDebugLog( 'squid', __METHOD__ .
+                               ": Error opening UDP socket: $errstr\n" );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
 
-                       $urlArr = array_unique( $urlArr ); // Remove duplicates
-                       foreach ( $urlArr as $url ) {
-                               if( !is_string( $url ) ) {
-                                       throw new MWException( 'Bad purge URL' );
-                               }
-                               $url = SquidUpdate::expand( $url );
-                               $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
-                               if ( !$conf ) {
-                                       wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
-                                       continue;
-                               }
-                               if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
-                                       throw new MWException( "Invalid HTCP rule for URL $url\n" );
-                               }
+               // Set socket options
+               socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
+               if ( $wgHTCPMulticastTTL != 1 ) {
+                       socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
+                               $wgHTCPMulticastTTL );
+               }
 
-                               // Construct a minimal HTCP request diagram
-                               // as per RFC 2756
-                               // Opcode 'CLR', no response desired, no auth
-                               $htcpTransID = rand();
+               $urlArr = array_unique( $urlArr ); // Remove duplicates
+               foreach ( $urlArr as $url ) {
+                       if ( !is_string( $url ) ) {
+                               wfProfileOut( __METHOD__ );
+                               throw new MWException( 'Bad purge URL' );
+                       }
+                       $url = SquidUpdate::expand( $url );
+                       $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
+                       if ( !$conf ) {
+                               wfDebugLog( 'squid', __METHOD__ .
+                                       "No HTCP rule configured for URL $url , skipping\n" );
+                               continue;
+                       }
+                       if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
+                               wfProfileOut( __METHOD__ );
+                               throw new MWException( "Invalid HTCP rule for URL $url\n" );
+                       }
 
-                               $htcpSpecifier = pack( 'na4na*na8n',
-                                       4, 'HEAD', strlen( $url ), $url,
-                                       8, 'HTTP/1.0', 0 );
+                       // Construct a minimal HTCP request diagram
+                       // as per RFC 2756
+                       // Opcode 'CLR', no response desired, no auth
+                       $htcpTransID = rand();
 
-                               $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
-                               $htcpLen = 4 + $htcpDataLen + 2;
+                       $htcpSpecifier = pack( 'na4na*na8n',
+                               4, 'HEAD', strlen( $url ), $url,
+                               8, 'HTTP/1.0', 0 );
 
-                               // Note! Squid gets the bit order of the first
-                               // word wrong, wrt the RFC. Apparently no other
-                               // implementation exists, so adapt to Squid
-                               $htcpPacket = pack( 'nxxnCxNxxa*n',
-                                       $htcpLen, $htcpDataLen, $htcpOpCLR,
-                                       $htcpTransID, $htcpSpecifier, 2);
+                       $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
+                       $htcpLen = 4 + $htcpDataLen + 2;
 
-                               // Send out
-                               wfDebug( "Purging URL $url via HTCP\n" );
-                               socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
-                                       $conf['host'], $conf['port'] );
-                       }
-               } else {
-                       $errstr = socket_strerror( socket_last_error() );
-                       wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
+                       // Note! Squid gets the bit order of the first
+                       // word wrong, wrt the RFC. Apparently no other
+                       // implementation exists, so adapt to Squid
+                       $htcpPacket = pack( 'nxxnCxNxxa*n',
+                               $htcpLen, $htcpDataLen, $htcpOpCLR,
+                               $htcpTransID, $htcpSpecifier, 2 );
+
+                       // Send out
+                       wfDebugLog( 'squid', __METHOD__ .
+                               "Purging URL $url via HTCP\n" );
+                       socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
+                               $conf['host'], $conf['port'] );
                }
                wfProfileOut( __METHOD__ );
        }