mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-22 17:21:46 +00:00
refactor(server): centralize request HTTPS and origin detection
This commit is contained in:
parent
6f03467f35
commit
527745b54e
4 changed files with 79 additions and 4 deletions
34
server/utils/requestUtils.js
Normal file
34
server/utils/requestUtils.js
Normal file
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue