Skip to content

Commit

Permalink
0.2.1 - Update URL parsing logic (fixes Rob--W#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob--W committed Aug 29, 2014
1 parent c78854e commit 0745b89
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
33 changes: 13 additions & 20 deletions lib/cors-anywhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down

0 comments on commit 0745b89

Please sign in to comment.