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

Add support for the ssl parameter to fetch images using https #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 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 @@ -61,6 +63,9 @@ function photon (imageUrl, opts) {
} else {
params.pathname = url.format( parsedUrl ).substring(1);
params.hostname = serverFromPathname( params.pathname );
if ( wasSecure ) {
params.query.ssl = 1;
}
}

if (opts) {
Expand All @@ -78,13 +83,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 All @@ -108,4 +113,3 @@ function serverFromPathname( pathname ) {
debug('determined server "%s" to use with "%s"', server, pathname);
return server + '.wp.com';
}

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 });
});

});