diff --git a/README.md b/README.md index 9e87534..97dd5b6 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,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"}` +* string `helpFile` - Set the help file (shown at the homepage). + Example: `"myCustomHelpText.txt"` For advanced users, the following options are also provided. diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index 57cbb50..d745d55 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -9,13 +9,13 @@ var url = require('url'); var regexp_tld = require('./regexp-top-level-domain'); var getProxyForUrl = require('proxy-from-env').getProxyForUrl; -var help_file = __dirname + '/help.txt'; -var help_text; -function showUsage(headers, response) { - headers['content-type'] = 'text/plain'; - if (help_text != null) { +var help_text = {}; +function showUsage(help_file, headers, response) { + var isHtml = /\.html$/.test(help_file); + headers['content-type'] = isHtml ? 'text/html' : 'text/plain'; + if (help_text[help_file] != null) { response.writeHead(200, headers); - response.end(help_text); + response.end(help_text[help_file]); } else { require('fs').readFile(help_file, 'utf8', function(err, data) { if (err) { @@ -23,8 +23,8 @@ function showUsage(headers, response) { response.writeHead(500, headers); response.end(); } else { - help_text = data; - showUsage(headers, response); // Recursive call, but since data is a string, the recursion will end + help_text[help_file] = data; + showUsage(help_file, headers, response); // Recursive call, but since data is a string, the recursion will end } }); } @@ -210,12 +210,13 @@ function parseURL(req_url) { function getHandler(options, proxy) { var corsAnywhere = { getProxyForUrl: getProxyForUrl, // Function that specifies the proxy to use - maxRedirects: 5, // Maximum number of redirects to be followed. - originBlacklist: [], // Requests from these origins will be blocked. - originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked. - requireHeader: null, // Require a header to be set? - removeHeaders: [], // Strip these request headers. - setHeaders: {}, // Set these request headers. + maxRedirects: 5, // Maximum number of redirects to be followed. + originBlacklist: [], // Requests from these origins will be blocked. + originWhitelist: [], // If non-empty, requests not from an origin in this list will be blocked. + requireHeader: null, // Require a header to be set? + removeHeaders: [], // Strip these request headers. + setHeaders: {}, // Set these request headers. + helpFile: __dirname + '/help.txt', }; Object.keys(corsAnywhere).forEach(function(option) { @@ -255,7 +256,7 @@ function getHandler(options, proxy) { if (!location) { // Invalid API call. Show how to correctly use the API - showUsage(cors_headers, res); + showUsage(corsAnywhere.helpFile, cors_headers, res); return; } diff --git a/test/customHelp.html b/test/customHelp.html new file mode 100644 index 0000000..2ced07a --- /dev/null +++ b/test/customHelp.html @@ -0,0 +1,7 @@ + + + + +

Custom HTML help!!

+ + \ No newline at end of file diff --git a/test/customHelp.txt b/test/customHelp.txt new file mode 100644 index 0000000..5d61f64 --- /dev/null +++ b/test/customHelp.txt @@ -0,0 +1 @@ +Server is OK!! diff --git a/test/test.js b/test/test.js index ca67257..35ac0cf 100644 --- a/test/test.js +++ b/test/test.js @@ -798,3 +798,40 @@ describe('httpProxyOptions.getProxyForUrl', function() { .expect(200, 'Response from https://example.com', done); }); }); + +describe('helpFile', function() { + + afterEach(stopServer); + + it('GET / with custom text helpFile', function(done) { + var customHelpTextPath = path.join(__dirname, './customHelp.txt'); + var customHelpText = fs.readFileSync(customHelpTextPath, {encoding: 'utf8'}); + + cors_anywhere = createServer({ + helpFile: customHelpTextPath, + }); + cors_anywhere_port = cors_anywhere.listen(0).address().port; + + request(cors_anywhere) + .get('/') + .type('text/plain') + .expect('Access-Control-Allow-Origin', '*') + .expect(200, customHelpText, done); + }); + + it('GET / with custom HTML helpFile', function(done) { + var customHelpTextPath = path.join(__dirname, './customHelp.html'); + var customHelpText = fs.readFileSync(customHelpTextPath, {encoding: 'utf8'}); + + cors_anywhere = createServer({ + helpFile: customHelpTextPath, + }); + cors_anywhere_port = cors_anywhere.listen(0).address().port; + + request(cors_anywhere) + .get('/') + .type('text/html') + .expect('Access-Control-Allow-Origin', '*') + .expect(200, customHelpText, done); + }); +});