Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/78405


Change subject: Test for HTTPS support & log results.
......................................................................

Test for HTTPS support & log results.

Per request from Ryan Lane. This patch adds a JavaScript module that tests for
HTTPS support by dispatching two simultaneous and asyncronous requests (one via
HTTP, the other via HTTPS) for a pixel image on bits and then comparing the
results once both arrive. They response payload is validated by verifying the
image dimensions. A timeout ensures that hung requests are reported as
failures. A sample factor of 1:5,000 HTTP requests protects against a flood of
incoming events.

See schema at <https://meta.wikimedia.org/wiki/Schema:HttpsSupport>.

Change-Id: I7629c8be4225dbee73c0182ac18359e89769c65b
---
M CoreEvents.php
A modules/ext.coreEvents.httpsSupport.js
2 files changed, 88 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CoreEvents 
refs/changes/05/78405/1

diff --git a/CoreEvents.php b/CoreEvents.php
index 37aa5b6..99182e5 100644
--- a/CoreEvents.php
+++ b/CoreEvents.php
@@ -29,8 +29,39 @@
 
 $wgExtensionMessagesFiles['CoreEvents'] = __DIR__ . '/CoreEvents.i18n.php';
 
+// Configs
+
+/**
+ * @var int|bool: Conduct & log test for HTTPS support once per this
+ *                many (non-HTTPS) requests.
+ */
+$wgHttpsFeatureDetectionSamplingFactor = 5000;
+
+$wgResourceModules += array(
+       'schema.HttpsSupport' => array(
+               'class'  => 'ResourceLoaderSchemaModule',
+               'schema' => 'HttpsSupport',
+               'revision' => 5712722,
+       ),
+       'ext.coreEvents.httpsSupport' => array(
+               'scripts'       => 'ext.coreEvents.httpsSupport.js',
+               'localBasePath' => __DIR__ . '/modules',
+               'remoteExtPath' => 'CoreEvents/modules',
+       ),
+);
+
 // Hooks
 
+$wgHooks[ 'BeforePageDisplay' ][] = function ( &$out, &$skin ) {
+       $out->addModules( 'ext.coreEvents.httpsSupport' );
+       return true;
+};
+
+$wgHooks[ 'ResourceLoaderGetConfigVars' ][] = function ( &$vars ) {
+       global $wgHttpsFeatureDetectionSamplingFactor;
+       $vars[ 'wgHttpsFeatureDetectionSamplingFactor' ] = 
$wgHttpsFeatureDetectionSamplingFactor;
+};
+
 /**
  * Log server-side event on successful page edit.
  * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete
diff --git a/modules/ext.coreEvents.httpsSupport.js 
b/modules/ext.coreEvents.httpsSupport.js
new file mode 100644
index 0000000..43b1059
--- /dev/null
+++ b/modules/ext.coreEvents.httpsSupport.js
@@ -0,0 +1,57 @@
+/**
+ * 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 = '//upload.wikimedia.org/wikipedia/commons/c/c0/Blank.gif';
+
+    function inSample() {
+        var factor = mw.config.get( 'wgHttpsFeatureDetectionSamplingFactor' );
+        if ( !$.isNumeric( factor ) || factor < 1 ) {
+            return false;
+        }
+        return Math.floor( Math.random() * factor ) === 0;
+    }
+
+    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 ok = this.type === 'load' && $beacon.attr( 'width' ) === 1;
+            return ok ? 'success' : this.type;
+        } );
+    }
+
+    // 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 () {
+                       $.when(
+                               pingProtocol( 'http' ),
+                               pingProtocol( 'https' )
+                       ).done( function ( httpStatus, httpsStatus ) {
+                               var event = {
+                                       httpStatus  : httpStatus,
+                                       httpsStatus : httpsStatus,
+                                       userAgent   : navigator.userAgent,
+                               };
+                               if ( $.isPlainObject( window.Geo ) && typeof 
Geo.country === 'string' ) {
+                                       event.originCountry = Geo.country;
+                               }
+                               mw.eventLog.logEvent( 'HttpsSupport', event );
+                       } );
+               } );
+       }
+} ( mediaWiki, jQuery ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7629c8be4225dbee73c0182ac18359e89769c65b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CoreEvents
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>

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

Reply via email to