From 0745b894c65ec78d5e4f5bf3964dd8bbd477f1ff Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Fri, 29 Aug 2014 16:28:26 +0200 Subject: [PATCH] 0.2.1 - Update URL parsing logic (fixes #9) --- lib/cors-anywhere.js | 33 +++++++++++++-------------------- package.json | 2 +- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index 2948afd..9846afd 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -81,7 +81,7 @@ function withCORS(headers, request) { function proxyRequest(req, res, proxy) { var location = req.corsAnywhereRequestState.location; - req.url = location.pathAndQueryString; + req.url = location.path; // Let the "Host" header be the host part of the path (including port, if specified). req.headers.host = location.host; @@ -118,13 +118,13 @@ function onProxyResponse(response, req, res) { var statusCode = response.statusCode; if (!requestState.redirectCount_) { - res.setHeader('x-request-url', requestState.location.full_url); + res.setHeader('x-request-url', requestState.location.href); } // Handle redirects if (statusCode === 301 || statusCode === 302 || statusCode === 303 || statusCode === 307 || statusCode === 308) { var locationHeader = response.headers['location']; if (locationHeader) { - locationHeader = url.resolve(requestState.location.full_url, locationHeader); + locationHeader = url.resolve(requestState.location.href, locationHeader); if (statusCode === 301 || statusCode === 302 || statusCode === 303) { // Exclude 307 & 308, because they are rare, and require preserving the method + request body @@ -171,15 +171,13 @@ function onProxyResponse(response, req, res) { delete response.headers['set-cookie']; delete response.headers['set-cookie2']; - response.headers['x-final-url'] = requestState.location.full_url; + response.headers['x-final-url'] = requestState.location.href; } /** * @param req_url {string} The requested URL (scheme is optional). - * @return {object} Strings: full_url, host, hostname, pathAndQueryString - * Number: port - * boolean: isHttps + * @return {object} URL parsed using url.parse */ function parseURL(req_url) { var match = req_url.match(/^(?:(https?:)?\/\/)?(([^\/?]+?)(?::(\d{0,5})(?=[\/?]|$))?)([\/?][\S\s]*|$)/i); @@ -190,20 +188,15 @@ function parseURL(req_url) { if (!match) { return null; } - var isHttps = (match[1] && match[1].toLowerCase()) === 'https:'; - var location = { - full_url: match[0], - isHttps: isHttps, - host: match[2], - hostname: match[3], - port: match[4] ? +match[4] : (isHttps ? 443 : 80), - pathAndQueryString: match[5] - }; - - if (!match[1]) { // Scheme is omitted. - location.full_url = (location.port === 443 ? 'https:' : 'http:') + location.full_url.replace(/^(?!\/)/, '//'); + if (!match[1]) { + // scheme is omitted. + if (req_url.lastIndexOf('//', 0) === -1) { + // "//" is omitted. + req_url = '//' + req_url; + } + req_url = (match[4] == '443' ? 'https:' : 'http:') + req_url; } - return location; + return url.parse(req_url); } // Request handler factory diff --git a/package.json b/package.json index 3ac83c3..730b117 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cors-anywhere", - "version": "0.2.0", + "version": "0.2.1", "description": "CORS Anywhere is a reverse proxy which adds CORS headers to the proxied request. Request URL is taken from the path", "license": "MIT", "author": "Rob Wu ",