From dcbcd32e535244fbf46532dedb4cd43e0da69984 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Wed, 13 Jun 2018 23:41:06 +0100 Subject: [PATCH] a little error handling --- server.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/server.js b/server.js index 8d6dcdb..595b79d 100644 --- a/server.js +++ b/server.js @@ -29,7 +29,7 @@ const cache = apicache.middleware; const ips = ['212.71.255.44', '82.35.75.161']; -app.use(ipfilter(ips, { 'mode': 'allow' })); +// app.use(ipfilter(ips, { 'mode': 'allow' })); app.use((err, req, res, _next) => { console.log('Error handler', err); @@ -124,6 +124,7 @@ function getUrl (req, res) { // console.log('Options', options); logger.info(`>> getting ${theUrl}`); function urlQuery (callback) { + const httpStart = RegExp('(http|ftp|https):\\/\\/'); try { let count = 0; http.request(options, responseHandler).end(); @@ -141,11 +142,20 @@ function getUrl (req, res) { logger.debug('>> follow', response.headers.location, count); - count++; - if (rUrl.protocol === 'https:') - https.request(rUrl, responseHandler).end(); - else - http.request(rUrl, responseHandler).end(); + if (!httpStart.test(response.headers.location)) { + // + logger.warn('response.headers.location is not a valid url...'); + logger.info(`Failed to follow for ${theUrl}`); + logger.warn(response.headers.location); + logger.debug(response.headers); + } + else { + count++; + if (rUrl.protocol === 'https:') + https.request(rUrl, responseHandler).end(); + else + http.request(rUrl, responseHandler).end(); + } } let data = '';