-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsi-proxy.js
68 lines (60 loc) · 2.48 KB
/
clsi-proxy.js
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
67
68
/**
* CLSI Proxy for the Cloud9 IDE
*
* @copyright 2010, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
var Plugin = require("cloud9/plugin");
var sys = require("sys");
var url = require("url");
var http = require("http");
var ShellGitPlugin = module.exports = function(ide) {
this.ide = ide;
this.hooks = [];
this.name = "clsi-proxy"
this.establishProxy(ide, ide.options.clsiUrl, ide.options.clsiToken);
};
sys.inherits(ShellGitPlugin, Plugin);
(function() {
this.establishProxy = function(ide, clsiUrl, token) {
this.ide.httpServer.use(function(req, res, next) {
var mountPoint = ide.options.urlNamespace + "/clsi"
if (req.url.match("^" + mountPoint)) {
// Remove /clsi from beginning of url and any leading slash
var path = req.url.slice(mountPoint.length, req.url.length);
if (path[0] == "/")
path = path.slice(1, path.length);
var backendUrl = url.parse(clsiUrl + "/" + path + "?format=json&token=" + token);
var port = backendUrl.port;
if (!port) {
if (backendUrl.protocol == "http:") port = 80;
else if (backendUrl.protocol == "https:") port = 443;
else port = 80;
}
var client = http.createClient(port, backendUrl.hostname);
var backendReq = client.request(
req.method,
backendUrl.pathname + (backendUrl.search || ""),
req.headers
);
backendReq.on("response", function(backendRes) {
backendRes.on("data", function(chunk) {
res.write(chunk, "binary");
});
backendRes.on("end", function() {
res.end();
});
res.writeHead(backendRes.statusCode, backendRes.headers);
});
req.on("data", function(chunk) {
backendReq.write(chunk, "binary");
});
req.on("end", function() {
backendReq.end();
});
} else {
next();
}
});
};
}).call(ShellGitPlugin.prototype);