mirror of
https://github.com/d0zingcat/cors-anywhere.git
synced 2026-05-13 15:09:25 +00:00
0.2.1 - Update URL parsing logic (fixes #9)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 <rob@robwu.nl>",
|
||||
|
||||
Reference in New Issue
Block a user