Skip to content

Commit

Permalink
handle local redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Oct 7, 2014
1 parent 4683ffd commit 598b8a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
30 changes: 22 additions & 8 deletions lib/image-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,40 @@ module.exports = function () {

var agent = parts.protocol === 'http:' ? http : https
// @see http://nodejs.org/api/http.html#http_http_get_options_callback
, req2 = agent.get(parts, function (res2) {
, request = agent.get(parts, function (response) {
// @see http://nodejs.org/api/http.html#http_response_statuscode
if ((res2.statusCode === 301 || res2.statusCode === 302) && res2.headers['location']) {
return retrieve(res2.headers['location']);
if ((response.statusCode === 301 || response.statusCode === 302) && response.headers['location']) {
var redirect = url.parse(response.headers['location']);
// @see https://tools.ietf.org/html/rfc7231#section-7.1.2
if (!redirect.protocol) {
redirect.protocol = parts.protocol;
}
if (!redirect.hostname) {
redirect.hostname = parts.hostname;
}
if (!redirect.port) {
redirect.port = parts.port;
}
if (!redirect.hash) {
redirect.hash = parts.hash;
}
return retrieve(url.format(redirect));
}

// The image must return status code 200.
if (res2.statusCode !== 200) {
return res.status(404).send('Expected response code 200, got ' + res2.statusCode);
if (response.statusCode !== 200) {
return res.status(404).send('Expected response code 200, got ' + response.statusCode);
}

// The image must be a valid content type.
// @see http://nodejs.org/api/http.html#http_request_headers
var mimeType = (res2.headers['content-type'] || '').replace(/;.*/, '');
var mimeType = (response.headers['content-type'] || '').replace(/;.*/, '');
if (mimeTypes.indexOf(mimeType) === -1) {
return res.status(404).send('Expected content type ' + mimeTypes.join(', ') + ', got ' + mimeType);
}

// @see https://github.com/aheckmann/gm#constructor
imageMagick(res2, 'image.' + mime.extension(mimeType))
imageMagick(response, 'image.' + mime.extension(mimeType))
// @see http://www.imagemagick.org/Usage/thumbnails/#cut
.resize(width, height + '^>')
.gravity('Center') // faces are most often near the center
Expand All @@ -75,7 +89,7 @@ module.exports = function () {
}).on('error', next);

// Timeout after five seconds. Better luck next time.
req2.setTimeout(delay, function () {
request.setTimeout(delay, function () {
return res.status(504).send();
});
};
Expand Down
14 changes: 7 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ describe('GET /:url/:width/:height', function () {
.expect('Cache-Control', 'max-age=31536000, public')
.expect(200, done);
});
// it('follows local redirects', function (done) {
// request(app)
// .get('/http%3A%2F%2Flocalhost:8080%2Flocation-relative/100/100')
// .expect('Content-Type', 'image/png')
// .expect('Cache-Control', 'max-age=31536000, public')
// .expect(200, done);
// });
it('follows local redirects', function (done) {
request(app)
.get('/http%3A%2F%2Flocalhost:8080%2Flocation-relative/100/100')
.expect('Content-Type', 'image/png')
.expect('Cache-Control', 'max-age=31536000, public')
.expect(200, done);
});
it('fails if the Location header is empty', function (done) {
request(app)
.get('/http%3A%2F%2Flocalhost:8080%2Flocation-empty/100/100')
Expand Down

0 comments on commit 598b8a5

Please sign in to comment.