-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetRequest.js
55 lines (45 loc) · 1.89 KB
/
assetRequest.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
var url = require('url'),
http = require('http');
var mod = function(options) {
return function(request, response) {
var start = new Date();
var that = this;
that.getSubDomain = function() {
var subDomainRegex = new RegExp("(.*)\\." + options.domain + ".*"),
results = null;
results = request.headers.host.match(subDomainRegex);
if (results && results[1]) {
return results[1];
} else {
return null;
}
};
that.getAssetHost = function() {
return that.getSubDomain() + ".soup.io";
};
that.getAssetPath = function() {
return request.url;
};
that.respondeWithFile = function(buffer, contentType, httpStatusCode) {
try {
response.writeHead(httpStatusCode, {
'Content-Type': contentType,
'Content-Length': buffer.length,
'Cache-Control': 'max-age=2592000, public' //30 days
});
options.stats.dataCount[request.connection.remoteAddress] += buffer.length;
response.end(buffer, 'binary');
options.stats.responseTime += new Date() - start;
options.logger.access(request, httpStatusCode, buffer.length);
} catch (e) {
// client missing or something
console.error("assetDownload", e);
}
};
that.fetchFileAndResponde = function() {
options.assetLoader.download(that.getAssetHost(), that.getAssetPath(), that.respondeWithFile);
};
that.fetchFileAndResponde();
};
};
module.exports = mod;