diff --git a/server/controllers/assets/utils/determineResponseType.js b/server/controllers/assets/utils/determineResponseType.js index f32a0c5c..2592ffed 100644 --- a/server/controllers/assets/utils/determineResponseType.js +++ b/server/controllers/assets/utils/determineResponseType.js @@ -3,6 +3,22 @@ const logger = require('winston'); const SERVE = 'SERVE'; const SHOW = 'SHOW'; +function headersMatchesSocialBotList (headers) { + const socialBotList = [ + 'facebookexternalhit', + 'Twitterbot', + ]; + const userAgent = headers['user-agent']; + for (let i = 0; i < socialBotList.length; i++) { + const socialBot = socialBotList[i]; + if (userAgent.indexOf(socialBot) >= 0) { + logger.debug('headers on request matched this bot:', socialBot); + return true; + } + } + return false; +} + function clientAcceptsHtml ({accept}) { return accept && accept.match(/text\/html/); }; @@ -19,6 +35,12 @@ function clientWantsAsset ({accept, range}) { const determineResponseType = (hasFileExtension, headers) => { let responseType; + logger.debug('headers:', headers); + // return early with 'show' if headers match the list + if (headersMatchesSocialBotList(headers)) { + // return SHOW; + } + // fallback logic if not on the list if (hasFileExtension) { responseType = SERVE; // assume a serve request if file extension is present if (clientAcceptsHtml(headers)) { // if the request comes from a browser, change it to a show request diff --git a/server/routes/api/index.js b/server/routes/api/index.js index 0aabd472..97909fd8 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -28,7 +28,7 @@ module.exports = (app) => { app.get('/api/claim/data/:claimName/:claimId', claimData); app.get('/api/claim/get/:name/:claimId', claimGet); app.get('/api/claim/list/:name', claimList); - app.post('/api/claim/long-id', claimLongId); + app.post('/api/claim/long-id', claimLongId); // should be a get app.post('/api/claim/publish', multipartMiddleware, claimPublish); app.get('/api/claim/resolve/:name/:claimId', claimResolve); app.get('/api/claim/short-id/:longId/:name', claimShortId); diff --git a/test/test.html b/test/test.html new file mode 100644 index 00000000..01a5d622 --- /dev/null +++ b/test/test.html @@ -0,0 +1,21 @@ + + + + + + + Document + + + test embed +

no identifier, no ending

+ no identifier, no ending +

no identifier, yes ending

+ no identifier, yes ending +

yes identifier, no ending

+ yes identifier, no ending +

yes identifier, yes ending

+ yes identifier, yes ending + +