jenkins-bot has submitted this change and it was merged.

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, 90 insertions(+), 0 deletions(-)

Approvals:
  Spage: Looks good to me, approved
  jenkins-bot: Verified



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..95856bc
--- /dev/null
+++ b/modules/ext.coreEvents.httpsSupport.js
@@ -0,0 +1,59 @@
+/*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 = 
'//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.prop( '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: merged
Gerrit-Change-Id: I7629c8be4225dbee73c0182ac18359e89769c65b
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/CoreEvents
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Ryan Lane <[email protected]>
Gerrit-Reviewer: Spage <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to