forked from erwintoni/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_http.js
77 lines (70 loc) · 2.18 KB
/
app_http.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
var url = require('url');
var fs = require('fs');
var crypto = require('crypto');
/*
max-age is set to one year in seconds (31536000)
expires is set to now plus one year in milliseconds (31536000000)
*/
exports.redirect = function(res, path) {
res.writeHead(302, { 'Location': path });
res.end();
};
exports.etag = function(buffer) {
var shasum = crypto.createHash('sha1');
shasum.update(buffer, 'binary');
return shasum.digest('hex');
}
exports.replyNotCached = function(res, buffer) {
res.writeHead(200, {
'Content-Type' : 'text/html',
'Content-Length' : buffer.length,
'Connection' : 'keep-alive',
'Proxy-Connection' : 'keep-alive',
'Pragma' : 'no-cache',
'Cache-Control' : 'no-cache, no-store'
});
res.end(buffer);
};
exports.replyCached = function(res, buffer, contentType, etag, contentEncoding) {
if(contentEncoding) {
res.writeHead(200, {
'Content-Type' : contentType,
'Content-Length' : buffer.length,
'Connection' : 'keep-alive',
'Proxy-Connection' : 'keep-alive',
'Pragma' : 'public',
'Cache-Control' : 'max-age=31536000',
'Vary' : 'Accept-Encoding',
'Expires' : new Date(Date.now() + 31536000000).toUTCString(),
'ETag' : etag,
'Content-Encoding' : contentEncoding
});
} else {
res.writeHead(200, {
'Content-Type' : contentType,
'Content-Length' : buffer.length,
'Connection' : 'keep-alive',
'Proxy-Connection' : 'keep-alive',
'Pragma' : 'public',
'Cache-Control' : 'max-age=31536000',
'Vary' : 'Accept-Encoding',
'Expires' : new Date(Date.now() + 31536000000).toUTCString(),
'ETag' : etag
});
}
res.end(buffer);
};
exports.replyNotModified = function(res) {
res.writeHead(304, {
'Connection' : 'keep-alive',
'Proxy-Connection' : 'keep-alive',
'Cache-Control' : 'max-age=31536000',
'Expires' : new Date(Date.now() + 31536000000).toUTCString()
});
res.end();
};
exports.replyNotFound = function(res) {
res.writeHead(404, {
});
res.end();
};