-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
38 lines (32 loc) · 988 Bytes
/
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
var http = require('http');
var Message = require('./message.js');
var credentials = 'superusr1245!:passpasspass123';
var HttpServer = function(xmpp) {
var server = http.createServer();
server.on('request', function(request, response) {
var auth = request.headers.authorization;
if (auth === undefined || new Buffer(auth.replace(/Basic /, ''), 'base64').toString('utf-8') !== credentials) {
response.writeHead(401);
response.end('Not authorized.');
return;
}
if (request.method === 'GET') {
var projectName = require('url').parse(request.url).path.substring(1);
var level = xmpp.getLevel(projectName).toString();
response.writeHead(200);
response.end(level, 'utf-8');
} else {
var body = '';
request.on('data', function(chunk) {
body += chunk;
});
request.on('end', function() {
xmpp.log(new Message(request, body));
response.writeHead(200);
response.end();
});
}
});
return server;
}
module.exports = HttpServer;