From dad7abd9cee2b91c32458de117832ef79c35b113 Mon Sep 17 00:00:00 2001 From: Bryan Davis Date: Mon, 15 Dec 2014 15:05:13 -0700 Subject: [PATCH] Create a generic UDP transport class Code moved from MWLoggerLegacyLogger::emit(), which was formerly in wfErrorLog(). Bug: T74572 Bug: T78599 Change-Id: I9e0e63d41bed6ccb468f3a9f1d52f156acc355a8 --- autoload.php | 1 + includes/debug/logger/legacy/Logger.php | 44 +--------- includes/libs/UDPTransport.php | 102 ++++++++++++++++++++++++ includes/rcfeed/UDPRCFeedEngine.php | 3 +- 4 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 includes/libs/UDPTransport.php diff --git a/autoload.php b/autoload.php index 107ab81da7..fd2781c2b4 100644 --- a/autoload.php +++ b/autoload.php @@ -1182,6 +1182,7 @@ $wgAutoloadLocalClasses = array( 'TransformTooBigImageAreaError' => __DIR__ . '/includes/media/MediaTransformOutput.php', 'TransformationalImageHandler' => __DIR__ . '/includes/media/TransformationalImageHandler.php', 'UDPRCFeedEngine' => __DIR__ . '/includes/rcfeed/UDPRCFeedEngine.php', + 'UDPTransport' => __DIR__ . '/includes/libs/UDPTransport.php', 'UIDGenerator' => __DIR__ . '/includes/utils/UIDGenerator.php', 'UcdXmlReader' => __DIR__ . '/maintenance/language/generateCollationData.php', 'UncategorizedCategoriesPage' => __DIR__ . '/includes/specials/SpecialUncategorizedcategories.php', diff --git a/includes/debug/logger/legacy/Logger.php b/includes/debug/logger/legacy/Logger.php index e7c69b88c6..69c3feb9a9 100644 --- a/includes/debug/logger/legacy/Logger.php +++ b/includes/debug/logger/legacy/Logger.php @@ -324,48 +324,8 @@ class MWLoggerLegacyLogger extends \Psr\Log\AbstractLogger { */ public static function emit( $text, $file ) { if ( substr( $file, 0, 4 ) == 'udp:' ) { - # Needs the sockets extension - if ( preg_match( '!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) { - // IPv6 bracketed host - $host = $m[1]; - $port = intval( $m[2] ); - $prefix = isset( $m[3] ) ? $m[3] : false; - $domain = AF_INET6; - } elseif ( preg_match( '!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) { - $host = $m[1]; - if ( !IP::isIPv4( $host ) ) { - $host = gethostbyname( $host ); - } - $port = intval( $m[2] ); - $prefix = isset( $m[3] ) ? $m[3] : false; - $domain = AF_INET; - } else { - throw new MWException( __METHOD__ . ': Invalid UDP specification' ); - } - - // Clean it up for the multiplexer - if ( strval( $prefix ) !== '' ) { - $text = preg_replace( '/^/m', $prefix . ' ', $text ); - - // Limit to 64KB - if ( strlen( $text ) > 65506 ) { - $text = substr( $text, 0, 65506 ); - } - - if ( substr( $text, -1 ) != "\n" ) { - $text .= "\n"; - } - } elseif ( strlen( $text ) > 65507 ) { - $text = substr( $text, 0, 65507 ); - } - - $sock = socket_create( $domain, SOCK_DGRAM, SOL_UDP ); - if ( !$sock ) { - return; - } - - socket_sendto( $sock, $text, strlen( $text ), 0, $host, $port ); - socket_close( $sock ); + $transport = UDPTransport::newFromString( $file ); + $transport->emit( $text ); } else { wfSuppressWarnings(); $exists = file_exists( $file ); diff --git a/includes/libs/UDPTransport.php b/includes/libs/UDPTransport.php new file mode 100644 index 0000000000..7fad882a24 --- /dev/null +++ b/includes/libs/UDPTransport.php @@ -0,0 +1,102 @@ +host = $host; + $this->port = $port; + $this->domain = $domain; + $this->prefix = $prefix; + } + + /** + * @param string $info In the format of "udp://host:port/prefix" + * @return UDPTransport + * @throws InvalidArgumentException + */ + public static function newFromString( $info ) { + if ( preg_match( '!^udp:(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $info, $m ) ) { + // IPv6 bracketed host + $host = $m[1]; + $port = intval( $m[2] ); + $prefix = isset( $m[3] ) ? $m[3] : false; + $domain = AF_INET6; + } elseif ( preg_match( '!^udp:(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $info, $m ) ) { + $host = $m[1]; + if ( !IP::isIPv4( $host ) ) { + $host = gethostbyname( $host ); + } + $port = intval( $m[2] ); + $prefix = isset( $m[3] ) ? $m[3] : false; + $domain = AF_INET; + } else { + throw new InvalidArgumentException( __METHOD__ . ': Invalid UDP specification' ); + } + + return new self( $host, $port, $domain, $prefix ); + } + + /** + * @param string $text + */ + public function emit( $text ) { + // Clean it up for the multiplexer + if ( $this->prefix !== false ) { + $text = preg_replace( '/^/m', $this->prefix . ' ', $text ); + + // Limit to 64KB + if ( strlen( $text ) > 65506 ) { + $text = substr( $text, 0, 65506 ); + } + + if ( substr( $text, -1 ) != "\n" ) { + $text .= "\n"; + } + } elseif ( strlen( $text ) > 65507 ) { + $text = substr( $text, 0, 65507 ); + } + + $sock = socket_create( $this->domain, SOCK_DGRAM, SOL_UDP ); + if ( !$sock ) { // @todo should this throw an exception? + return; + } + + socket_sendto( $sock, $text, strlen( $text ), 0, $this->host, $this->port ); + socket_close( $sock ); + } +} diff --git a/includes/rcfeed/UDPRCFeedEngine.php b/includes/rcfeed/UDPRCFeedEngine.php index 8554670e13..9afae661a0 100644 --- a/includes/rcfeed/UDPRCFeedEngine.php +++ b/includes/rcfeed/UDPRCFeedEngine.php @@ -29,6 +29,7 @@ class UDPRCFeedEngine implements RCFeedEngine { * @see RCFeedEngine::send */ public function send( array $feed, $line ) { - wfErrorLog( $line, $feed['uri'] ); + $transport = UDPTransport::newFromString( $feed['uri'] ); + $transport->emit( $line ); } } -- 2.20.1