This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathserver.js
168 lines (138 loc) · 5.11 KB
/
server.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
/*
* Server for communication between the UI and the mobile library.
* Also serves UI-related files.
*/
var http = require('http');
var path = require('path');
var fs = require('fs');
var config = require('../config/config.defaults.js');
var util = require('./server-util.js');
function run() {
/* Server for web service ports and debugger UI */
http.createServer(AardwolfServer).listen(config.serverPort, null, function() {
console.log('Server listening for requests on port ' + config.serverPort + '.');
});
}
var mobileDispatcher = new Dispatcher();
var desktopDispatcher = new Dispatcher();
function AardwolfServer(req, res) {
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, Content-Type');
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.setHeader('Access-Control-Allow-Credentials', 'true');
var body = '';
if (req.method == 'OPTIONS') {
res.end();
return;
}
else if (req.method == 'POST') {
req.on('data', function (chunk) { body += chunk; });
req.on('end', function () { processPostedData(JSON.parse(body)); });
}
else {
processPostedData();
}
function processPostedData(data) {
switch (req.url) {
case '/mobile/init':
mobileDispatcher.end();
mobileDispatcher = new Dispatcher();
mobileDispatcher.setClient(res);
desktopDispatcher.clearMessages();
desktopDispatcher.addMessage(data);
break;
case '/mobile/console':
desktopDispatcher.addMessage(data);
ok200();
break;
case '/mobile/breakpoint':
desktopDispatcher.addMessage(data);
mobileDispatcher.setClient(res);
break;
case '/mobile/incoming':
mobileDispatcher.setClient(res);
break;
case '/desktop/outgoing':
mobileDispatcher.addMessage(data);
ok200();
break;
case '/desktop/incoming':
desktopDispatcher.setClient(res);
break;
case '/files/list':
ok200({ files: util.getFilesList() });
break;
case '/':
case '/ui':
case '/ui/':
res.writeHead(302, {'Location': '/ui/index.html'});
res.end();
break;
default:
/* check if we need to serve a UI file */
if (req.url.indexOf('/ui/') === 0) {
var requestedFile = req.url.substr(4);
var uiFilesDir = path.join(__dirname, '../ui/');
var fullRequestedFilePath = path.join(uiFilesDir, requestedFile);
/* File must exist and must be located inside the uiFilesDir */
if (fs.existsSync(fullRequestedFilePath) && fullRequestedFilePath.indexOf(uiFilesDir) === 0) {
util.serveStaticFile(res, fullRequestedFilePath);
break;
}
}
/* check if we need to serve a UI file */
if (req.url.indexOf('/files/data/') === 0) {
var requestedFile = req.url.substr(12);
var filesDir = path.normalize(config.fileServerBaseDir);
var fullRequestedFilePath = path.join(filesDir, requestedFile);
/* File must exist and must be located inside the filesDir */
if (fs.existsSync(fullRequestedFilePath) && fullRequestedFilePath.indexOf(filesDir) === 0) {
ok200({
data: fs.readFileSync(fullRequestedFilePath).toString(),
breakpoints: require('../rewriter/multirewriter.js').getRewrittenContent(requestedFile).breakpoints || []
});
break;
}
}
/* fallback... */
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('NOT FOUND');
}
}
function ok200(data) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data || {}));
}
}
function Dispatcher() {
var queue = [];
var client;
this.setClient = function(c) {
this.end();
client = c;
process();
};
this.addMessage = function(m) {
queue.push(m);
process();
};
this.end = function() {
if (client) {
client.end();
}
};
this.clearMessages = function() {
queue = [];
};
function process() {
if (client && queue.length > 0) {
client.writeHead(200, { 'Content-Type': 'application/json' });
var msg = queue.shift();
client.end(JSON.stringify(msg));
client = null;
}
}
}
module.exports.run = run;