Ori.livneh has submitted this change and it was merged.

Change subject: Re-introduce HTTPS support detection
......................................................................


Re-introduce HTTPS support detection

Bug: T88361
Change-Id: I992514bd468f797430a753ddf18393d8d6b0b985
(cherry picked from commit f93146d14aa98533d4db62704feda3214fbb7bea)
---
M WikimediaEvents.php
M WikimediaEventsHooks.php
A modules/ext.wikimediaEvents.httpsSupport.js
3 files changed, 100 insertions(+), 2 deletions(-)

Approvals:
  Ori.livneh: Verified; Looks good to me, approved



diff --git a/WikimediaEvents.php b/WikimediaEvents.php
index 89e6f0b..20e5a08 100644
--- a/WikimediaEvents.php
+++ b/WikimediaEvents.php
@@ -28,6 +28,9 @@
 
 // Configuration
 
+/** @var int|bool: Logs once per this many requests. */
+$wgHttpsFeatureDetectionSamplingFactor = 1000;
+
 /**
  * @var bool|string: Full URI or false if not set.
  * Data is logged to this end point as key-value pairs in the query
@@ -71,8 +74,16 @@
                'remoteExtPath' => 'WikimediaEvents/modules',
                'targets' => array( 'desktop', 'mobile' ),
        ),
+       'schema.HttpsSupport' => array(
+               'class'    => 'ResourceLoaderSchemaModule',
+               'schema'   => 'HttpsSupport',
+               'revision' => 11437897,
+       ),
        'ext.wikimediaEvents.statsd' => array(
-               'scripts'       => 'ext.wikimediaEvents.statsd.js',
+               'scripts'       => array(
+                       'ext.wikimediaEvents.statsd.js',
+                       'ext.wikimediaEvents.httpsSupport.js',
+               ),
                'localBasePath' => __DIR__ . '/modules',
                'remoteExtPath' => 'WikimediaEvents/modules',
                'targets'       => array( 'desktop', 'mobile' ),
diff --git a/WikimediaEventsHooks.php b/WikimediaEventsHooks.php
index fcd03ca..5d02356 100644
--- a/WikimediaEventsHooks.php
+++ b/WikimediaEventsHooks.php
@@ -320,7 +320,8 @@
        }
 
        public static function onResourceLoaderGetConfigVars( &$vars ) {
-               global $wgWMEStatsdBaseUri;
+               global $wgWMEStatsdBaseUri, 
$wgHttpsFeatureDetectionSamplingFactor;
+               $vars['wgHttpsFeatureDetectionSamplingFactor'] = 
$wgHttpsFeatureDetectionSamplingFactor;
                $vars['wgWMEStatsdBaseUri'] = $wgWMEStatsdBaseUri;
        }
 
diff --git a/modules/ext.wikimediaEvents.httpsSupport.js 
b/modules/ext.wikimediaEvents.httpsSupport.js
new file mode 100644
index 0000000..d73927a
--- /dev/null
+++ b/modules/ext.wikimediaEvents.httpsSupport.js
@@ -0,0 +1,86 @@
+/*global Geo */
+/**
+ * JavaScript module for HTTPS feature detection.
+ * Detects HTTPS support by firing two requests for the same resource
+ * using HTTP for one and HTTPS by other and logs results.
+ *
+ * @licence GNU GPL v2 or later
+ * @author Ori Livneh <[email protected]>
+ */
+( function ( mw, $ ) {
+       'use strict';
+
+       var pixelSrc = '//performance.wikimedia.org/blank.gif';
+
+       function inSample() {
+               var factor = mw.config.get( 
'wgHttpsFeatureDetectionSamplingFactor' );
+               if ( !$.isNumeric( factor ) || factor < 1 ) {
+                       return false;
+               }
+               return Math.floor( Math.random() * factor ) === 0;
+       }
+
+       // Return a deferred object that is resolved after `ms` milliseconds.
+       function sleep( ms ) {
+               var defer = $.Deferred();
+               setTimeout( function () {
+                       defer.resolve();
+               }, ms );
+               return defer;
+       }
+
+       function pingProtocol( proto, timeout ) {
+               var $beacon = $( '<img />' ),
+                       defer = $.Deferred();
+
+               $beacon.on( 'load error abort timeout', defer.resolveWith );
+               setTimeout( function () {
+                       $beacon.trigger( $.Event( 'timeout' ) );
+               }, timeout || 5000 );
+               $beacon.attr( 'src', proto + ':' + pixelSrc + '?' + new Date() 
);
+
+               return defer.then( function () {
+                       var status = {}, ok = this.type === 'load' && 
$beacon.prop( 'width' ) === 1;
+                       status[proto + 'Status'] = ok ? 'success' : this.type;
+                       return status;
+               } );
+       }
+
+
+       // Log only if user is using HTTP and is included in the random sample.
+       if ( window.location.protocol !== 'https:' && inSample() ) {
+               mw.loader.using( 'schema.HttpsSupport', function () {
+                       var protocols = [ 'http', 'https' ];
+
+                       // Flip the order of tests 50% of the time.
+                       if ( Math.floor( Math.random() * 2 ) ) {
+                               protocols.reverse();
+                       }
+
+                       $.when(
+                               pingProtocol( protocols.pop() ),
+                               pingProtocol( protocols.pop() ),
+                               sleep( 6000 )
+                       ).done( function ( firstStatus, secondStatus ) {
+                               var event = $.extend( {
+                                       isAnon      : mw.config.get( 'wgUserId' 
) === null,
+                                       userAgent   : navigator.userAgent
+                               }, firstStatus, secondStatus );
+
+                               if ( mw.mobileFrontend && mw.config.exists( 
'wgMFMode' ) ) {
+                                       event.mobileMode = mw.config.get( 
'wgMFMode' );
+                               }
+                               if ( $.isPlainObject( window.Geo ) ) {
+                                       if ( typeof Geo.country === 'string' && 
Geo.country.length ) {
+                                               event.originCountry = 
Geo.country;
+                                       }
+                                       if ( typeof Geo.city === 'string' && 
Geo.city.length ) {
+                                               event.originCity = Geo.city;
+                                       }
+                               }
+                               mw.eventLog.logEvent( 'HttpsSupport', event );
+                       } );
+               } );
+       }
+
+} ( mediaWiki, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/195823
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I992514bd468f797430a753ddf18393d8d6b0b985
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikimediaEvents
Gerrit-Branch: wmf/1.25wmf19
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to