|
2 | 2 |
|
3 | 3 | var http = require('http');
|
4 | 4 |
|
5 |
| -function HttpWrap(accessControl) { |
6 |
| - // do not expose accessControl through 'this.accessControl' |
7 |
| - var _accessControl = accessControl; |
| 5 | +function isLocalAccess(reqOptions) |
| 6 | +{ |
| 7 | + if (((reqOptions.hostname) && ((reqOptions.hostname === "localhost") || (reqOptions.hostname === "127.0.0.1"))) || ((reqOptions.host) && ((reqOptions.host === "localhost") || (reqOptions.host === "127.0.0.1")))) |
| 8 | + { |
| 9 | + return true; |
| 10 | + } |
| 11 | + else if (((reqOptions.hostname) && ((reqOptions.hostname === "[::1]") || (reqOptions.hostname === "[0:0:0:0:0:0:0:1]"))) || ((reqOptions.host) && ((reqOptions.host === "[::1]") || (reqOptions.host === "[0:0:0:0:0:0:0:1]")))) |
| 12 | + { |
| 13 | + return true; |
| 14 | + } |
| 15 | + else if (((reqOptions.hostname) && ((reqOptions.hostname === "::1") || (reqOptions.hostname === "0:0:0:0:0:0:0:1"))) || ((reqOptions.host) && ((reqOptions.host === "::1") || (reqOptions.host === "0:0:0:0:0:0:0:1")))) |
| 16 | + { |
| 17 | + return true; |
| 18 | + } |
| 19 | + return false; |
| 20 | +} |
| 21 | + |
| 22 | +function HttpWrap() |
| 23 | +{ |
| 24 | + this.localApp = false; |
| 25 | +} |
| 26 | + |
| 27 | +HttpWrap.prototype.IncomingMessage = http.IncomingMessage; |
| 28 | +HttpWrap.prototype.METHODS = http.METHODS; |
| 29 | +HttpWrap.prototype.OutgoingMessage = http.OutgoingMessage; |
8 | 30 |
|
9 |
| - HttpWrap.prototype.IncomingMessage = http.IncomingMessage; |
10 |
| - HttpWrap.prototype.METHODS = http.METHODS; |
11 |
| - HttpWrap.prototype.OutgoingMessage = http.OutgoingMessage; |
12 |
| - HttpWrap.prototype.globalAgent = http.globalAgent; |
| 31 | +HttpWrap.prototype.setLocalApp = function(isLocalApp) { |
| 32 | + this.localApp = isLocalApp; |
| 33 | +}; |
13 | 34 |
|
14 |
| - // Server functionality needs to be disabled. |
15 |
| - //HttpWrap.prototype.ServerResponse = http.ServerResponse; |
16 |
| - //HttpWrap.prototype.STATUS_CODES = http.STATUS_CODES; |
17 |
| - //HttpWrap.prototype.Server = http.Server; |
18 |
| - //HttpWrap.prototype.createServer = http.createServer; |
| 35 | +HttpWrap.prototype.getLocalApp = function() { |
| 36 | + return this.localApp; |
| 37 | +}; |
19 | 38 |
|
20 |
| - HttpWrap.prototype.request = function (options, cb) { |
21 |
| - if (_accessControl) { |
22 |
| - if (_accessControl.isLocalAccessFromRemote(options)) { |
23 |
| - console.log("localhost urls cannot be accessed by remote applications"); |
24 |
| - return; |
25 |
| - } |
26 |
| - options = _accessControl.wrapHttpRequestOptions(options); |
27 |
| - cb = _accessControl.wrapHttpResponseCallback(cb); |
| 39 | +HttpWrap.prototype.request = function(options, cb) { |
| 40 | + if (true == isLocalAccess(options)) |
| 41 | + { |
| 42 | + if (false == this.localApp) |
| 43 | + { |
| 44 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 45 | + return; |
28 | 46 | }
|
29 |
| - return http.request(options, cb); |
30 |
| - }; |
| 47 | + } |
| 48 | + return http.request(options, cb); |
| 49 | +}; |
31 | 50 |
|
32 |
| - // http.request == new http.ClientRequest |
33 |
| - HttpWrap.prototype.ClientRequest = function (options, cb) { |
34 |
| - if (_accessControl) { |
35 |
| - if (_accessControl.isLocalAccessFromRemote(options)) { |
36 |
| - console.log("localhost urls cannot be accessed by remote applications"); |
37 |
| - return; |
38 |
| - } |
39 |
| - options = _accessControl.wrapHttpRequestOptions(options); |
40 |
| - cb = _accessControl.wrapHttpResponseCallback(cb); |
| 51 | +HttpWrap.prototype.ClientRequest = function(options, cb) { |
| 52 | + if (true == isLocalAccess(options)) |
| 53 | + { |
| 54 | + if (false == this.localApp) |
| 55 | + { |
| 56 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 57 | + return; |
41 | 58 | }
|
42 |
| - return http.ClientRequest(options, cb); |
43 |
| - }; |
| 59 | + } |
| 60 | + return http.ClientRequest(options, cb); |
| 61 | +}; |
44 | 62 |
|
45 |
| - // http.get == http.request (+end) |
46 |
| - HttpWrap.prototype.get = function (options, cb) { |
47 |
| - if (_accessControl) { |
48 |
| - if (_accessControl.isLocalAccessFromRemote(options)) { |
49 |
| - console.log("localhost urls cannot be accessed by remote applications"); |
50 |
| - return; |
51 |
| - } |
52 |
| - options = _accessControl.wrapHttpRequestOptions(options); |
53 |
| - cb = _accessControl.wrapHttpResponseCallback(cb); |
| 63 | +HttpWrap.prototype.get = function(options, cb) { |
| 64 | + if (true == isLocalAccess(options)) |
| 65 | + { |
| 66 | + if (false == this.localApp) |
| 67 | + { |
| 68 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 69 | + return; |
54 | 70 | }
|
55 |
| - return http.get(options, cb); |
56 |
| - }; |
| 71 | + } |
| 72 | + return http.get(options, cb); |
| 73 | +}; |
57 | 74 |
|
58 |
| - HttpWrap.prototype.Agent = function (options) { |
59 |
| - if (_accessControl) { |
60 |
| - if (_accessControl.isLocalAccessFromRemote(options)) { |
61 |
| - console.log("localhost urls cannot be accessed by remote applications"); |
62 |
| - return; |
63 |
| - } |
| 75 | +HttpWrap.prototype.Agent = function(options) { |
| 76 | + if (true == isLocalAccess(options)) |
| 77 | + { |
| 78 | + if (false == this.localApp) |
| 79 | + { |
| 80 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 81 | + return; |
64 | 82 | }
|
65 |
| - return http.Agent(options); |
66 |
| - }; |
| 83 | + } |
| 84 | + return http.Agent(options); |
| 85 | +}; |
67 | 86 |
|
68 |
| - // TODO CORS? |
69 |
| - // deprecated |
70 |
| - HttpWrap.prototype.Client = function (port, host) { |
71 |
| - if (_accessControl) { |
72 |
| - if (_accessControl.isLocalAccessFromRemote(host)) { |
73 |
| - console.log("localhost urls cannot be accessed by remote applications"); |
74 |
| - return; |
75 |
| - } |
| 87 | +HttpWrap.prototype.globalAgent = function(options) { |
| 88 | + if (true == isLocalAccess(options)) |
| 89 | + { |
| 90 | + if (false == this.localApp) |
| 91 | + { |
| 92 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 93 | + return; |
76 | 94 | }
|
77 |
| - return http.Client(port, host); |
78 |
| - }; |
| 95 | + } |
| 96 | + return http.globalAgent(options); |
| 97 | +}; |
79 | 98 |
|
80 |
| - // TODO CORS? |
81 |
| - // deprecated |
82 |
| - HttpWrap.prototype.createClient = function (port, host) { |
83 |
| - if (_accessControl) { |
84 |
| - if (_accessControl.isLocalAccessFromRemote(host)) { |
85 |
| - console.log("localhost urls cannot be accessed by remote applications"); |
86 |
| - return; |
87 |
| - } |
| 99 | +HttpWrap.prototype.Client = function(port, host) { |
| 100 | + if ((host === "localhost") || (host === "127.0.0.1")) |
| 101 | + { |
| 102 | + if (false == this.localApp) |
| 103 | + { |
| 104 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 105 | + return; |
88 | 106 | }
|
89 |
| - return http.createClient(port, host); |
90 |
| - }; |
91 |
| -} |
| 107 | + } |
| 108 | + return http.Client(port,host); |
| 109 | +}; |
| 110 | + |
| 111 | +HttpWrap.prototype.createClient = function(port, host) { |
| 112 | + if ((host === "localhost") || (host === "127.0.0.1")) |
| 113 | + { |
| 114 | + if (false == this.localApp) |
| 115 | + { |
| 116 | + console.log("localhost urls cannot be accessed by remote applications"); |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + return http.createClient(port,host); |
| 121 | +}; |
92 | 122 |
|
93 | 123 | module.exports = HttpWrap;
|
0 commit comments