forked from gr2m/CORS-Proxy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcorsproxy.js
More file actions
66 lines (62 loc) · 2.11 KB
/
corsproxy.js
File metadata and controls
66 lines (62 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Generated by CoffeeScript 1.4.0
var http, httpProxy, url;
url = require('url');
http = require('http');
httpProxy = require('http-proxy');
module.exports = function(req, res, proxy) {
var cors_headers, header, headers, host, hostname, ignore, key, path, port, target, target0, value, _i, _len, _ref, _ref1, _ref2;
if (req.headers['access-control-request-headers']) {
headers = req.headers['access-control-request-headers'];
} else {
headers = 'accept, accept-charset, accept-encoding, accept-language, authorization, content-length, content-type, host, origin, proxy-connection, referer, user-agent, x-requested-with';
_ref = req.headers;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
header = _ref[_i];
if (req.indexOf('x-') === 0) {
headers += ", " + header;
}
}
}
cors_headers = {
'access-control-allow-methods': 'HEAD, POST, GET, PUT, PATCH, DELETE',
'access-control-max-age': '86400',
'access-control-allow-headers': headers,
'access-control-allow-credentials': 'true',
'access-control-allow-origin': req.headers.origin || '*'
};
if (req.method === 'OPTIONS') {
console.log('responding to OPTIONS request');
res.writeHead(200, cors_headers);
res.end();
} else {
module.exports.options = module.exports.options || {};
if (module.exports.options.target) {
target0 = url.parse(module.exports.options.target);
target = {
host: target0.hostname,
port: target0.port
};
path = req.url;
req.headers.host = target0.hostname;
} else {
_ref1 = req.url.match(/\/([^\/]+)(.*)/), ignore = _ref1[0], hostname = _ref1[1], path = _ref1[2];
_ref2 = hostname.split(':'), host = _ref2[0], port = _ref2[1];
target = {
host: host,
port: port
};
req.headers.host = hostname;
}
if (!target) {
res.write("Cannot determine target host\n");
res.end();
return;
}
for (key in cors_headers) {
value = cors_headers[key];
res.setHeader(key, value);
}
req.url = path;
return proxy.proxyRequest(req, res, target);
}
};