From 77e19f3e06d480d166350850d33cec79a63bbcfe Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Wed, 13 Jun 2018 23:51:27 +0100 Subject: [PATCH] a little error handling --- server.js | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/server.js b/server.js index 595b79d..85b5bf1 100644 --- a/server.js +++ b/server.js @@ -105,6 +105,8 @@ const bouncer = ['phpmyadmin', 'phpmyadmin2018', 'phpmanager']; +const httpStart = RegExp('(http|ftp|https):\\/\\/'); + function getUrl (req, res) { const theUrl = req.params.encoded_id; logger.debug('Want', theUrl); @@ -124,7 +126,6 @@ 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(); @@ -133,29 +134,32 @@ function getUrl (req, res) { response.setEncoding('utf8'); if (response.statusCode === 302 || response.statusCode === 301) { body = []; - const rUrl = URL.parse(response.headers.location); + logger.debug('>> follow', response.headers.location, count); + let rUrl; + + if (!httpStart.test(response.headers.location)) { + // + logger.warn('response.headers.location is not a valid url...'); + logger.info(`Trying to fix for ${theUrl}`); + logger.warn(response.headers.location); + logger.debug(response.headers); + + const tempUrl = new URL(theUrl); + rUrl = URL.parse(`${tempUrl.origin}${response.headers.location}`); + } + else + rUrl = URL.parse(response.headers.location); rUrl.followAllRedirects = true; rUrl.headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' }; - logger.debug('>> follow', response.headers.location, count); - - 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(); - } + count++; + if (rUrl.protocol === 'https:') + https.request(rUrl, responseHandler).end(); + else + http.request(rUrl, responseHandler).end(); } let data = '';