updated response type decision tree

This commit is contained in:
bill bittner 2018-07-17 14:44:15 -07:00
parent 925d025295
commit 4cd3eb851a
4 changed files with 10 additions and 54 deletions

View file

@ -1,9 +1,7 @@
const EMBED = 'EMBED';
const BROWSER = 'BROWSER';
const SOCIAL = 'SOCIAL';
const SHOW = 'SHOW';
module.exports = {
EMBED,
BROWSER,
SOCIAL,
SHOW,
};

View file

@ -6,7 +6,7 @@ const lbryUri = require('../utils/lbryUri.js');
const determineRequestType = require('../utils/determineRequestType.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
let requestType = determineRequestType(hasFileExtension, headers);
if (requestType !== EMBED) {
if (requestType === SHOW) {
return handleShowRender(req, res);
}
// parse the claim

View file

@ -7,7 +7,7 @@ const determineRequestType = require('../utils/determineRequestType.js');
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.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
let requestType = determineRequestType(hasFileExtension, headers);
if (requestType !== EMBED) {
if (requestType === SHOW) {
return handleShowRender(req, res);
}
// parse the claim

View file

@ -1,28 +1,5 @@
const logger = require('winston');
const { EMBED, BROWSER, SOCIAL } = 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/);
}
const { EMBED, SHOW } = require('../constants/request_types.js');
function clientWantsAsset ({accept, range}) {
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) => {
let responseType;
logger.debug('headers:', headers);
// return early with 'show' if headers match the list
if (headersMatchesSocialBotList(headers)) {
return SOCIAL;
if (hasFileExtension || clientWantsAsset(headers)) {
return EMBED;
}
// if request is not from a social bot...
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;
return SHOW;
};
module.exports = determineRequestType;