Skip to content

Commit

Permalink
Fix raster redirect and drop the legacy static .png route (badges#3659)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow committed Jul 7, 2019
1 parent 66c7f13 commit eec904c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 34 deletions.
4 changes: 2 additions & 2 deletions config/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public:

rateLimit: false

redirectUrl: 'http://badge-server.example.com'
redirectUrl: 'http://frontend.example.test'

rasterUrl: 'http://raster.example.com'
rasterUrl: 'http://raster.example.test'

handleInternalErrors: false
33 changes: 3 additions & 30 deletions core/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ const {
clearRequestCache,
} = require('../base-service/legacy-request-handler')
const { clearRegularUpdateCache } = require('../legacy/regular-update')
const {
staticBadgeUrl,
rasterRedirectUrl,
} = require('../badge-urls/make-badge-url')
const { rasterRedirectUrl } = require('../badge-urls/make-badge-url')
const log = require('./log')
const sysMonitor = require('./monitor')
const PrometheusMetrics = require('./prometheus-metrics')
Expand Down Expand Up @@ -259,8 +256,8 @@ module.exports = class Server {
/**
* Register Redirects
*
* Set up a couple of redirects which have to be registered last:
* One for the legacy static badge route.
* Set up a couple of redirects:
* One for the raster badges.
* Another to redirect the base URL /
* (we use this to redirect https://img.shields.io/ to https://shields.io/ )
*/
Expand All @@ -271,30 +268,6 @@ module.exports = class Server {
} = config

if (rasterUrl) {
// Any badge, old version.
camp.route(/^\/([^/]+)\/(.+).png$/, (queryParams, match, end, ask) => {
const [, label, message] = match
const { color } = queryParams

const redirectUrl = staticBadgeUrl({
baseUrl: rasterUrl,
label,
message,
// Fixes https://github.com/badges/shields/issues/3260
color: color ? color.toString() : undefined,
format: 'png',
})

ask.res.statusCode = 301
ask.res.setHeader('Location', redirectUrl)

// The redirect is permanent.
const cacheDuration = (365 * 24 * 3600) | 0 // 1 year
ask.res.setHeader('Cache-Control', `max-age=${cacheDuration}`)

ask.res.end()
})

// Redirect to the raster server for raster versions of modern badges.
camp.route(/\.png$/, (queryParams, match, end, ask) => {
ask.res.statusCode = 301
Expand Down
14 changes: 12 additions & 2 deletions core/server/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ describe('The server', function() {
)
expect(statusCode).to.equal(301)
expect(headers.location).to.equal(
'http://raster.example.com/:fruit-apple-green.png'
'http://raster.example.test/:fruit-apple-green.png'
)
})

it('should redirect modern PNG badges as configured', async function() {
const { statusCode, headers } = await got(`${baseUrl}npm/v/express.png`, {
followRedirect: false,
})
expect(statusCode).to.equal(301)
expect(headers.location).to.equal(
'http://raster.example.test/npm/v/express.png'
)
})

Expand Down Expand Up @@ -115,7 +125,7 @@ describe('The server', function() {

expect(statusCode).to.equal(302)
// This value is set in `config/test.yml`
expect(headers.location).to.equal('http://badge-server.example.com')
expect(headers.location).to.equal('http://frontend.example.test')
})

it('should return the 410 badge for obsolete formats', async function() {
Expand Down

0 comments on commit eec904c

Please sign in to comment.