Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Add support for the ssl parameter to fetch images from https URLs
Browse files Browse the repository at this point in the history
Closes #5.
  • Loading branch information
blowery authored and TooTallNate committed Feb 2, 2016
1 parent 903adee commit cd64187
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ 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;
delete parsedUrl.port;

var params = {
slashes: true,
protocol: 'https:'
protocol: 'https:',
query: {}
};

if ( isAlreadyPhotoned( parsedUrl.host ) ) {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 });
});

});

0 comments on commit cd64187

Please sign in to comment.