2017-08-12 03:27:27 +00:00
|
|
|
const PackageURL = require('../PackageURL')
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-12 16:26:06 +00:00
|
|
|
const ValidQueryKeys = {
|
|
|
|
main: true,
|
|
|
|
meta: true,
|
|
|
|
json: true // deprecated
|
|
|
|
}
|
|
|
|
|
|
|
|
function queryIsValid(query) {
|
|
|
|
return Object.keys(query).every(function (key) {
|
|
|
|
return ValidQueryKeys[key]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:12:50 +00:00
|
|
|
/**
|
|
|
|
* Parse and validate the URL.
|
|
|
|
*/
|
2017-08-11 05:11:55 +00:00
|
|
|
function parseURL(req, res, next) {
|
2017-08-12 03:27:27 +00:00
|
|
|
const url = PackageURL.parse(req.url)
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
if (url == null)
|
2017-08-13 00:23:40 +00:00
|
|
|
return res.status(403).type('text').send(`Invalid URL: ${req.url}`)
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-12 16:26:06 +00:00
|
|
|
// Do not allow unrecognized query parameters because
|
|
|
|
// some people use them to bust the cache.
|
|
|
|
if (!queryIsValid(url.query))
|
2017-08-13 00:23:40 +00:00
|
|
|
return res.status(403).type('text').send(`Invalid query: ${JSON.stringify(url.query)}`)
|
2017-08-12 16:26:06 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
req.packageName = url.packageName
|
|
|
|
req.packageVersion = url.packageVersion
|
|
|
|
req.packageSpec = `${req.packageName}@${req.packageVersion}`
|
|
|
|
req.pathname = url.pathname
|
|
|
|
req.filename = url.filename
|
|
|
|
req.search = url.search
|
|
|
|
req.query = url.query
|
2017-08-10 17:12:50 +00:00
|
|
|
|
2017-08-11 05:11:55 +00:00
|
|
|
next()
|
2017-08-10 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseURL
|