/** * Add array of URLs to the purger queue * * @param Array $urlArr list of URLs to purge * @throws WikiaException */ static function purge($urlArr) { global $wgEnableScribeReport; wfProfileIn(__METHOD__); if (empty($wgEnableScribeReport)) { wfProfileOut(__METHOD__); return; } foreach ($urlArr as $url) { if (!is_string($url)) { throw new WikiaException('Bad purge URL'); } $url = SquidUpdate::expand($url); $method = self::getPurgeCaller(); wfDebug("Purging URL {$url} from {$method} via Scribe\n"); wfDebug("Purging backtrace: " . wfGetAllCallers(false) . "\n"); // add to the queue, will be sent by onRestInPeace method self::$urls[$url] = ['url' => $url, 'time' => time(), 'method' => $method]; self::$urlsCount++; } wfProfileOut(__METHOD__); }
/** * Queue a purge operation * * @param string $url */ public function queuePurge($url) { global $wgSquidPurgeUseHostHeader; $url = SquidUpdate::expand(str_replace("\n", '', $url)); $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(); } }
/** * wikia function * @ * @access public * @static */ static function ScribePurge($urlArr) { global $wgEnableScribeReport; wfProfileIn(__METHOD__); $key = 'varnish_purges'; if (empty($wgEnableScribeReport)) { wfProfileOut(__METHOD__); return true; } try { foreach ($urlArr as $url) { if (!is_string($url)) { throw new MWException('Bad purge URL'); } $url = SquidUpdate::expand($url); wfDebug("Purging URL {$url} via Scribe\n"); $data = json_encode(array('url' => $url, 'time' => time())); WScribeClient::singleton($key)->send($data); } } catch (TException $e) { Wikia::log(__METHOD__, 'scribeClient exception', $e->getMessage()); } wfProfileOut(__METHOD__); }
/** * @throws MWException * @param $urlArr array */ static function HTCPPurge($urlArr) { global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL; wfProfileIn(__METHOD__); $htcpOpCLR = 4; // HTCP CLR // @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h) if (!defined("IPPROTO_IP")) { define("IPPROTO_IP", 0); define("IP_MULTICAST_LOOP", 34); define("IP_MULTICAST_TTL", 33); } // 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); } $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) { wfDebug("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"); } // Construct a minimal HTCP request diagram // as per RFC 2756 // Opcode 'CLR', no response desired, no auth $htcpTransID = rand(); $htcpSpecifier = pack('na4na*na8n', 4, 'HEAD', strlen($url), $url, 8, 'HTTP/1.0', 0); $htcpDataLen = 8 + 2 + strlen($htcpSpecifier); $htcpLen = 4 + $htcpDataLen + 2; // 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 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"); } wfProfileOut(__METHOD__); }
static function HTCPPurge($urlArr) { global $wgHTCPMulticastAddress, $wgHTCPMulticastTTL, $wgHTCPPort; $fname = 'SquidUpdate::HTCPPurge'; wfProfileIn($fname); $htcpOpCLR = 4; // HTCP CLR // FIXME PHP doesn't support these socket constants (include/linux/in.h) if (!defined("IPPROTO_IP")) { define("IPPROTO_IP", 0); define("IP_MULTICAST_LOOP", 34); define("IP_MULTICAST_TTL", 33); } // 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); } foreach ($urlArr as $url) { if (!is_string($url)) { wfDebugDieBacktrace('Bad purge URL'); } $url = SquidUpdate::expand($url); // Construct a minimal HTCP request diagram // as per RFC 2756 // Opcode 'CLR', no response desired, no auth $htcpTransID = rand(); $htcpSpecifier = pack('na4na*na8n', 4, 'HEAD', strlen($url), $url, 8, 'HTTP/1.0', 0); $htcpDataLen = 8 + 2 + strlen($htcpSpecifier); $htcpLen = 4 + $htcpDataLen + 2; // 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 wfDebug("Purging URL {$url} via HTCP\n"); socket_sendto($conn, $htcpPacket, $htcpLen, 0, $wgHTCPMulticastAddress, $wgHTCPPort); } } else { $errstr = socket_strerror(socket_last_error()); wfDebug("SquidUpdate::HTCPPurge(): Error opening UDP socket: {$errstr}\n"); } wfProfileOut($fname); }
/** * Queue a purge operation * * @param $url string */ public function queuePurge($url) { $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"; if ($this->currentRequestIndex === null) { $this->nextRequest(); } }