mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-08-07 04:18:35 +02:00
refactor(server): centralize request HTTPS and origin detection
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Whether the request was made over HTTPS.
|
||||
* Uses Express `req.secure` and a strict `x-forwarded-proto === 'https'` check.
|
||||
*
|
||||
* @param {import('express').Request} req
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isRequestSecure(req) {
|
||||
return req.secure || req.get('x-forwarded-proto') === 'https'
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('express').Request} req
|
||||
* @returns {'https' | 'http'}
|
||||
*/
|
||||
function getRequestProtocol(req) {
|
||||
return isRequestSecure(req) ? 'https' : 'http'
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('express').Request} req
|
||||
* @returns {{ protocol: 'https' | 'http', host: string, origin: string }}
|
||||
*/
|
||||
function getRequestOrigin(req) {
|
||||
const protocol = getRequestProtocol(req)
|
||||
const host = req.get('host')
|
||||
return { protocol, host, origin: `${protocol}://${host}` }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isRequestSecure,
|
||||
getRequestProtocol,
|
||||
getRequestOrigin
|
||||
}
|
||||
Reference in New Issue
Block a user