This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexplorer.js
110 lines (88 loc) · 2.41 KB
/
explorer.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var http = require('http');
var https = require('https');
var fs = require('fs');
var util = require('util');
var url = require('url');
var explorer = {};
var home = process.env.SIDE_BY_SIDE_HOME || "";
/*
* serve html file
*/
explorer.html = function (req, rsp, name, statusCode) {
var content = fs.readFileSync(home + "public/" + name + ".html");
rsp.writeHead(statusCode || 200, {'Content-Type': 'text/html; charset=utf-8'});
rsp.end(content);
};
/*
* serve JSON content
*/
explorer.json = function (req, rsp, content) {
rsp.writeHead(200, {'Content-Type': 'application/json'});
rsp.end(JSON.stringify(content));
}
/*
* use redirector to present mapping information as json
*/
explorer.head = function (req, rsp, path, info) {
// default/fallthrough action: hit the redirector and let the browser do the
// rest.
var fallthrough = function () {
util.log(':fallthrough: ' + path);
return explorer.json(req, rsp, {
'location': info.upstream_protocol + '://' + info.redirector + path
});
};
var redirectorOpts = {
'method': 'HEAD',
'host': info.redirector,
'path': path,
'headers': {
'host': info.upstream
}
};
var req = http.request(redirectorOpts, function(res) {
util.log(':head: ' + res.statusCode + " " + path + " " + res.headers.location);
if (res.statusCode != 301) {
return fallthrough();
}
// follow redirect one hop, to see status
var nextLoc = url.parse(res.headers.location);
var nextOpts = {
'method': 'HEAD',
'host': nextLoc.host,
'path': nextLoc.pathname
};
var proto = (nextLoc.protocol === "https:") ? https : http;
proto.request(nextOpts, function(res) {
util.log(':follow: ' + res.statusCode + ' ' + nextLoc.host + ' ' + nextLoc.pathname);
return fallthrough();
}).end();
})
req.on('error', function() {
console.log(arguments);
});
req.end();
};
/*
* explorer
*/
explorer.request = function (req, rsp, path, info) {
/*
* serve the single-page app
*/
if (path.match(/^\/$/)) {
return explorer.html(req, rsp, 'index');
}
if (path.match(/^\/info.json$/)) {
return explorer.json(req, rsp, info);
}
/*
* proxy the redirector
*/
if (path.match(/^\/head\//)) {
path = path.replace(/^\/head/, "");
return explorer.head(req, rsp, path, info);
}
return explorer.html(req, rsp, '404', 404);
}
module.exports = explorer;