From 47ae09467576f23b72292ba64694a9a4a36b221d Mon Sep 17 00:00:00 2001 From: Antoine Vandecreme Date: Tue, 15 Apr 2014 12:55:32 -0400 Subject: [PATCH] Fix ajax call for file: and ftp: #73 --- src/openseadragon.js | 22 ++++++++++++++++++++-- test/basic.js | 2 +- test/utils.js | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/openseadragon.js b/src/openseadragon.js index 3be339af..8b8c6a14 100644 --- a/src/openseadragon.js +++ b/src/openseadragon.js @@ -1725,6 +1725,21 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){ return value ? value : null; }, + /** + * Retrieves the protocol used by the url. The url can either be absolute + * or relative. + * @function + * @param {String} url The url to retrieve the protocol from. + * @return {String} The protocol (http:, https:, file:, ftp: ...) + */ + getUrlProtocol: function( url ) { + var match = url.match(/^([a-z]+:)\/\//i); + if ( match === null ) { + // Relative URL, retrive the protocol from window.location + return window.location.protocol; + } + return match[1].toLowerCase(); + }, createAjaxRequest: function(){ var request; @@ -1778,10 +1793,13 @@ window.OpenSeadragon = window.OpenSeadragon || function( options ){ if ( request.readyState == 4 ) { request.onreadystatechange = function(){}; - if ( request.status == 200 ) { + var protocol = $.getUrlProtocol( url ); + var successStatus = + protocol === "http:" || protocol === "https:" ? 200 : 0; + if ( request.status === successStatus ) { onSuccess( request ); } else { - $.console.log( "AJAX request returned %s: %s", request.status, url ); + $.console.log( "AJAX request returned %d: %s", request.status, url ); if ( $.isFunction( onError ) ) { onError( request ); diff --git a/test/basic.js b/test/basic.js index 946a8ff3..969f492f 100644 --- a/test/basic.js +++ b/test/basic.js @@ -56,7 +56,7 @@ equal($(".openseadragon-message").length, 1, "Open failures should display a message"); - ok(testLog.log.contains('["AJAX request returned %s: %s",404,"/test/data/not-a-real-file"]'), + ok(testLog.log.contains('["AJAX request returned %d: %s",404,"/test/data/not-a-real-file"]'), "AJAX failures should be logged to the console"); start(); diff --git a/test/utils.js b/test/utils.js index f46f9a84..92538375 100644 --- a/test/utils.js +++ b/test/utils.js @@ -87,6 +87,30 @@ ); }); + test("getUrlProtocol", function() { + + equal(OpenSeadragon.getUrlProtocol("test"), window.location.protocol, + "'test' url protocol should be window.location.protocol"); + + equal(OpenSeadragon.getUrlProtocol("/test"), window.location.protocol, + "'/test' url protocol should be window.location.protocol"); + + equal(OpenSeadragon.getUrlProtocol("//test"), window.location.protocol, + "'//test' url protocol should be window.location.protocol"); + + equal(OpenSeadragon.getUrlProtocol("http://test"), "http:", + "'http://test' url protocol should be http:"); + + equal(OpenSeadragon.getUrlProtocol("https://test"), "https:", + "'https://test' url protocol should be https:"); + + equal(OpenSeadragon.getUrlProtocol("file://test"), "file:", + "'file://test' url protocol should be file:"); + + equal(OpenSeadragon.getUrlProtocol("FTP://test"), "ftp:", + "'FTP://test' url protocol should be ftp:"); + }); + // ---------- asyncTest("requestAnimationFrame", function() { var timeWatcher = Util.timeWatcher();