diff --git a/packages/photon.js/index.js b/packages/photon.js/index.js index cc71adcfbd0ceb..1816970089dfb3 100644 --- a/packages/photon.js/index.js +++ b/packages/photon.js/index.js @@ -42,7 +42,8 @@ var mappings = { function photon (imageUrl, opts) { // parse the URL, assuming //host.com/path style URLs are ok and parse the querystring - var parsedUrl = url.parse( imageUrl, true, true ); + var parsedUrl = url.parse( imageUrl, true, true ), + wasSecure = parsedUrl.protocol === 'https:'; delete parsedUrl.protocol; delete parsedUrl.auth; @@ -50,7 +51,8 @@ function photon (imageUrl, opts) { var params = { slashes: true, - protocol: 'https:' + protocol: 'https:', + query: {} }; if ( isAlreadyPhotoned( parsedUrl.host ) ) { @@ -65,6 +67,9 @@ function photon (imageUrl, opts) { } params.pathname = url.format( parsedUrl ).substring(1); params.hostname = serverFromPathname( params.pathname ); + if ( wasSecure ) { + params.query.ssl = 1; + } } if (opts) { @@ -82,13 +87,13 @@ function photon (imageUrl, opts) { continue; } - // any other options just gets passed through as query-string parameters - if (!params.query) params.query = {}; - params.query[mappings[i] || i] = opts[i]; } } + // do this after so a passed opt can't override it + + var photonUrl = url.format(params); debug('generated Photon URL: %s', photonUrl); return photonUrl; diff --git a/packages/photon.js/test/test.js b/packages/photon.js/test/test.js index c358e72bba2a31..8cfdb9c6b04898 100644 --- a/packages/photon.js/test/test.js +++ b/packages/photon.js/test/test.js @@ -39,6 +39,11 @@ describe('photon()', function () { assert(RegExp('https://i[0-2].wp.com/example.com/image.png').test(photon(url))); }); + it('should Photon-ify a secure image URL', function () { + var url = 'https://example.com/image.png'; + assert(RegExp('https://i[0-2].wp.com/example.com/image.png\\\?ssl=1').test(photon(url))); + }); + it('should not Photon-ify an existing Photon URL, even if the host is wrong', function () { var photonedUrl = photon('http://www.gravatar.com/avatar/693307b4e0cb9366f34862c9dfacd7fc'); var alternateUrl = 'https://i1.wp.com/www.gravatar.com/avatar/693307b4e0cb9366f34862c9dfacd7fc'; @@ -106,4 +111,17 @@ describe('photon()', function () { assertHostedOnPhotonInsecurely(photonedUrl); }); + + it('should allow you to turn off ssl for fetching', function() { + var photonedUrl = photon('https://example.com/foo.png', { secure: false, ssl: 0 }); + + assertHostedOnPhotonInsecurely(photonedUrl); + assertQuery(photonedUrl, { ssl: 0 }); + + photonedUrl = photon('https://i0.wp.com/example.com/foo.png', { secure: false, ssl: 0 }); + + assertHostedOnPhotonInsecurely(photonedUrl); + assertQuery(photonedUrl, { ssl: 0 }); + }); + });