From 9cf0a3d81ffd77202450731f6b1d38e2fe129022 Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Tue, 2 Jul 2013 12:42:39 +0200 Subject: [PATCH] HTCPPurge() early exit on socket error I tend to dislike code style such as: if( $ok ) { // lot of code } else { return false; } The SquidUpdate::HTCPPurge() used that pattern to handle a socket_create() issue. I have simply moved the block above and inverted the condition. Ie: if( ! $ok ) { return false; } // lot of code That saves a level of indentation and makes the code a bit easier to follow IMHO. Change-Id: Ic3c6e22bbb637352fef740a0c1663a4b6f5a2b6a --- includes/cache/SquidUpdate.php | 98 +++++++++++++++++----------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/includes/cache/SquidUpdate.php b/includes/cache/SquidUpdate.php index 0ee41e52e7..6cc59b7202 100644 --- a/includes/cache/SquidUpdate.php +++ b/includes/cache/SquidUpdate.php @@ -180,61 +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 ) ) { - 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" ); - } + // 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 - wfDebugLog( 'squid', __METHOD__ . - "Purging URL $url via HTCP\n" ); - socket_sendto( $conn, $htcpPacket, $htcpLen, 0, - $conf['host'], $conf['port'] ); - } - } else { - $errstr = socket_strerror( socket_last_error() ); + // 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__ . - ": Error opening UDP socket: $errstr\n" ); + "Purging URL $url via HTCP\n" ); + socket_sendto( $conn, $htcpPacket, $htcpLen, 0, + $conf['host'], $conf['port'] ); } wfProfileOut( __METHOD__ ); } -- 2.20.1