Rename maxAge to corsMaxAge and set the default corsMaxAge to 0.

Set corsAnywhereRequestState before calling withCORS and use the state instead of a parameter to get corsMaxAge.
This commit is contained in:
Jack Tench
2017-07-14 12:29:15 +01:00
parent 881392cfaf
commit 10df7c9f4a
3 changed files with 24 additions and 17 deletions

View File

@@ -107,7 +107,8 @@ proxy requests. The following options are supported:
Example: `["cookie"]`
* dictionary of lowercase strings `setHeaders` - Set headers for the request (overwrites existing ones).
Example: `{"x-powered-by": "CORS Anywhere"}`
* number `maxAge` - If set, an Access-Control-Max-Age header with this value (in seconds) will be added.
* number `corsMaxAge` - If set, an Access-Control-Max-Age request header with this value (in seconds) will be added.
Defaults to 0.
Example: `600` - Allow CORS preflight request to be cached by the browser for 10 minutes.
* string `helpFile` - Set the help file (shown at the homepage).
Example: `"myCustomHelpText.txt"`

View File

@@ -50,10 +50,11 @@ function isValidHostName(hostname) {
* @param headers {object} Response headers
* @param request {ServerRequest}
*/
function withCORS(headers, request, maxAge) {
function withCORS(headers, request) {
headers['access-control-allow-origin'] = '*';
if (maxAge) {
headers['access-control-max-age'] = maxAge;
var corsMaxAge = request.corsAnywhereRequestState.corsMaxAge;
if (corsMaxAge != null) {
headers['access-control-max-age'] = corsMaxAge;
}
if (request.headers['access-control-request-method']) {
headers['access-control-allow-methods'] = request.headers['access-control-request-method'];
@@ -196,7 +197,7 @@ function onProxyResponse(proxy, proxyReq, proxyRes, req, res) {
delete proxyRes.headers['set-cookie2'];
proxyRes.headers['x-final-url'] = requestState.location.href;
withCORS(proxyRes.headers, req, requestState.maxAge);
withCORS(proxyRes.headers, req);
return true;
}
@@ -237,7 +238,7 @@ function getHandler(options, proxy) {
requireHeader: null, // Require a header to be set?
removeHeaders: [], // Strip these request headers.
setHeaders: {}, // Set these request headers.
maxAge: null, // If set, an Access-Control-Max-Age header with this value (in seconds) will be added.
corsMaxAge: 0, // If set, an Access-Control-Max-Age header with this value (in seconds) will be added.
helpFile: __dirname + '/help.txt',
};
@@ -266,7 +267,13 @@ function getHandler(options, proxy) {
};
return function(req, res) {
var cors_headers = withCORS({}, req, corsAnywhere.maxAge);
req.corsAnywhereRequestState = {
getProxyForUrl: corsAnywhere.getProxyForUrl,
maxRedirects: corsAnywhere.maxRedirects,
corsMaxAge: corsAnywhere.corsMaxAge,
};
var cors_headers = withCORS({}, req);
if (req.method === 'OPTIONS') {
// Pre-flight request. Reply successfully:
res.writeHead(200, cors_headers);
@@ -353,13 +360,8 @@ function getHandler(options, proxy) {
req.headers[header] = corsAnywhere.setHeaders[header];
});
req.corsAnywhereRequestState = {
location: location,
getProxyForUrl: corsAnywhere.getProxyForUrl,
maxRedirects: corsAnywhere.maxRedirects,
maxAge: corsAnywhere.maxAge,
proxyBaseUrl: proxyBaseUrl,
};
req.corsAnywhereRequestState.location = location;
req.corsAnywhereRequestState.proxyBaseUrl = proxyBaseUrl;
proxyRequest(req, res, proxy);
};

View File

@@ -50,6 +50,7 @@ describe('Basic functionality', function() {
.get('/')
.type('text/plain')
.expect('Access-Control-Allow-Origin', '*')
.expect('Access-Control-Max-Age', '0')
.expect(200, helpText, done);
});
@@ -91,6 +92,7 @@ describe('Basic functionality', function() {
request(cors_anywhere)
.get('/example.com')
.expect('Access-Control-Allow-Origin', '*')
.expect('Access-Control-Max-Age', '0')
.expect('x-request-url', 'http://example.com/')
.expect(200, 'Response from example.com', done);
});
@@ -813,7 +815,7 @@ describe('setHeaders + removeHeaders', function() {
describe('Access-Control-Max-Age set', function() {
before(function() {
cors_anywhere = createServer({
maxAge: 600,
corsMaxAge: 600,
});
cors_anywhere_port = cors_anywhere.listen(0).address().port;
});
@@ -833,13 +835,15 @@ describe('Access-Control-Max-Age set', function() {
.get('/example.com')
.expect('Access-Control-Allow-Origin', '*')
.expect('Access-Control-Max-Age', '600')
.expect(200, 'Response from example.com', done);;
.expect(200, 'Response from example.com', done);
});
});
describe('Access-Control-Max-Age not set', function() {
before(function() {
cors_anywhere = createServer();
cors_anywhere = createServer({
corsMaxAge: null,
});
cors_anywhere_port = cors_anywhere.listen(0).address().port;
});
after(stopServer);