updated response type decision tree
This commit is contained in:
parent
925d025295
commit
4cd3eb851a
4 changed files with 10 additions and 54 deletions
|
@ -1,9 +1,7 @@
|
||||||
const EMBED = 'EMBED';
|
const EMBED = 'EMBED';
|
||||||
const BROWSER = 'BROWSER';
|
const SHOW = 'SHOW';
|
||||||
const SOCIAL = 'SOCIAL';
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
EMBED,
|
EMBED,
|
||||||
BROWSER,
|
SHOW,
|
||||||
SOCIAL,
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,7 +6,7 @@ const lbryUri = require('../utils/lbryUri.js');
|
||||||
const determineRequestType = require('../utils/determineRequestType.js');
|
const determineRequestType = require('../utils/determineRequestType.js');
|
||||||
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
||||||
|
|
||||||
const { EMBED } = require('../constants/request_types.js');
|
const { SHOW } = require('../constants/request_types.js');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ const serveByClaim = (req, res) => {
|
||||||
}
|
}
|
||||||
// determine request type
|
// determine request type
|
||||||
let requestType = determineRequestType(hasFileExtension, headers);
|
let requestType = determineRequestType(hasFileExtension, headers);
|
||||||
if (requestType !== EMBED) {
|
if (requestType === SHOW) {
|
||||||
return handleShowRender(req, res);
|
return handleShowRender(req, res);
|
||||||
}
|
}
|
||||||
// parse the claim
|
// parse the claim
|
||||||
|
|
|
@ -7,7 +7,7 @@ const determineRequestType = require('../utils/determineRequestType.js');
|
||||||
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
||||||
const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js');
|
const flipClaimNameAndId = require('../utils/flipClaimNameAndId.js');
|
||||||
|
|
||||||
const { EMBED } = require('../constants/request_types.js');
|
const { SHOW } = require('../constants/request_types.js');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ const serverByIdentifierAndClaim = (req, res) => {
|
||||||
}
|
}
|
||||||
// determine request type
|
// determine request type
|
||||||
let requestType = determineRequestType(hasFileExtension, headers);
|
let requestType = determineRequestType(hasFileExtension, headers);
|
||||||
if (requestType !== EMBED) {
|
if (requestType === SHOW) {
|
||||||
return handleShowRender(req, res);
|
return handleShowRender(req, res);
|
||||||
}
|
}
|
||||||
// parse the claim
|
// parse the claim
|
||||||
|
|
|
@ -1,28 +1,5 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const { EMBED, BROWSER, SOCIAL } = require('../constants/request_types.js');
|
const { EMBED, SHOW } = require('../constants/request_types.js');
|
||||||
|
|
||||||
function headersMatchesSocialBotList (headers) {
|
|
||||||
const userAgent = headers['user-agent'];
|
|
||||||
const socialBotList = [
|
|
||||||
'facebookexternalhit',
|
|
||||||
'Twitterbot',
|
|
||||||
];
|
|
||||||
for (let i = 0; i < socialBotList.length; i++) {
|
|
||||||
if (userAgent.includes(socialBotList[i])) {
|
|
||||||
logger.debug('request is from social bot:', socialBotList[i]);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function clientAcceptsHtml ({accept}) {
|
|
||||||
return accept && accept.match(/text\/html/);
|
|
||||||
}
|
|
||||||
|
|
||||||
function requestIsFromBrowser (headers) {
|
|
||||||
return headers['user-agent'] && headers['user-agent'].match(/Mozilla/);
|
|
||||||
}
|
|
||||||
|
|
||||||
function clientWantsAsset ({accept, range}) {
|
function clientWantsAsset ({accept, range}) {
|
||||||
const imageIsWanted = accept && accept.match(/image\/.*/) && !accept.match(/text\/html/) && !accept.match(/text\/\*/);
|
const imageIsWanted = accept && accept.match(/image\/.*/) && !accept.match(/text\/html/) && !accept.match(/text\/\*/);
|
||||||
|
@ -31,30 +8,11 @@ function clientWantsAsset ({accept, range}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const determineRequestType = (hasFileExtension, headers) => {
|
const determineRequestType = (hasFileExtension, headers) => {
|
||||||
let responseType;
|
|
||||||
logger.debug('headers:', headers);
|
logger.debug('headers:', headers);
|
||||||
// return early with 'show' if headers match the list
|
if (hasFileExtension || clientWantsAsset(headers)) {
|
||||||
if (headersMatchesSocialBotList(headers)) {
|
return EMBED;
|
||||||
return SOCIAL;
|
|
||||||
}
|
}
|
||||||
// if request is not from a social bot...
|
return SHOW;
|
||||||
if (hasFileExtension) {
|
|
||||||
// assume embed,
|
|
||||||
responseType = EMBED;
|
|
||||||
// but change to browser if client accepts html.
|
|
||||||
if (clientAcceptsHtml(headers)) {
|
|
||||||
responseType = BROWSER;
|
|
||||||
}
|
|
||||||
// if request does not have file extentsion...
|
|
||||||
} else {
|
|
||||||
// assume browser,
|
|
||||||
responseType = BROWSER;
|
|
||||||
// but change to embed if someone embeded a show url...
|
|
||||||
if (clientWantsAsset(headers) && requestIsFromBrowser(headers)) {
|
|
||||||
responseType = EMBED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return responseType;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = determineRequestType;
|
module.exports = determineRequestType;
|
||||||
|
|
Loading…
Add table
Reference in a new issue