mirror of
https://github.com/d0zingcat/cors-anywhere.git
synced 2026-05-20 23:16:53 +00:00
Merge pull request #40 from Rise-Vision/feature/custom-help-text
Feature/custom help text
This commit is contained in:
@@ -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.
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
7
test/customHelp.html
Normal file
7
test/customHelp.html
Normal file
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Custom HTML help!!</h2>
|
||||
</body>
|
||||
</html>
|
||||
1
test/customHelp.txt
Normal file
1
test/customHelp.txt
Normal file
@@ -0,0 +1 @@
|
||||
Server is OK!!
|
||||
37
test/test.js
37
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user