mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
31 lines
690 B
JavaScript
31 lines
690 B
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const url = require('url');
|
||
|
|
|
||
|
|
// creates a slimmed down version of the request object
|
||
|
|
exports.buildReqObject = (req, payload) => {
|
||
|
|
req = req || {};
|
||
|
|
const headers = req.headers || {};
|
||
|
|
const encrypted = req.connection ? !!req.connection.encrypted : false;
|
||
|
|
let host = headers.host;
|
||
|
|
const referer = headers.referer || '';
|
||
|
|
|
||
|
|
if (!host) {
|
||
|
|
host = url.parse(referer).host || '';
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
uid: req.uid,
|
||
|
|
params: req.params,
|
||
|
|
method: req.method,
|
||
|
|
body: payload || req.body,
|
||
|
|
ip: req.ip,
|
||
|
|
host: host,
|
||
|
|
protocol: encrypted ? 'https' : 'http',
|
||
|
|
secure: encrypted,
|
||
|
|
url: referer,
|
||
|
|
path: referer.substr(referer.indexOf(host) + host.length),
|
||
|
|
headers: headers,
|
||
|
|
};
|
||
|
|
};
|