Fix raster redirect and drop the legacy static .png route (#3659)

This commit is contained in:
Paul Melnikow
2019-07-07 11:49:21 -04:00
committed by GitHub
parent 66c7f13e38
commit eec904c262
3 changed files with 17 additions and 34 deletions

View File

@@ -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

View File

@@ -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')
@@ -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/ )
*/
@@ -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

View File

@@ -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'
)
})
@@ -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() {