node-http-server – Diff between revs 37 and 38
?pathlinks?
Rev 37 | Rev 38 | |||
---|---|---|---|---|
Line 2... | Line 2... | |||
2 | |
2 | |
|
3 | /*************************************************************************/ |
3 | /*************************************************************************/ |
|
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
Line 5... | Line 5... | |||
5 | /*************************************************************************/ |
5 | /*************************************************************************/ |
|
6 | |
6 | |
|
7 | const fs = require('fs'); |
7 | const fs = require('fs') |
|
8 | const stream = require('stream'); |
8 | const stream = require('stream') |
|
9 | const util = require('util'); |
9 | const util = require('util') |
|
10 | const tz = require('moment-timezone'); |
10 | const tz = require('moment-timezone') |
|
Line 11... | Line 11... | |||
11 | const forge = require('node-forge'); |
11 | const forge = require('node-forge') |
|
12 | const EventEmitter = require('events').EventEmitter; |
12 | const EventEmitter = require('events').EventEmitter |
|
13 | |
13 | |
|
14 | // Cache constructor. |
14 | // Cache constructor. |
|
Line 15... | Line 15... | |||
15 | function Cache(config, client, request, response) { |
15 | function Cache(config, client, request, response) { |
|
16 | // Create events emitters for logging and data. |
16 | // Create events emitters for logging and data. |
|
17 | EventEmitter.call(this); |
17 | EventEmitter.call(this) |
|
18 | |
18 | |
|
19 | // Pass through objects needed for caching. |
19 | // Pass through objects needed for caching. |
|
20 | this.config = config; |
20 | this.config = config |
|
Line 21... | Line 21... | |||
21 | this.client = client; |
21 | this.client = client |
|
22 | this.request = request; |
22 | this.request = request |
|
23 | this.response = response; |
23 | this.response = response |
|
24 | }; |
24 | } |
|
Line 25... | Line 25... | |||
25 | |
25 | |
|
26 | // Cache handling. |
26 | // Cache handling. |
|
27 | Cache.prototype.process = function(resource, input, type) { |
27 | Cache.prototype.process = function(resource, input, type) { |
|
28 | EventEmitter.call(this); |
28 | EventEmitter.call(this) |
|
29 | const self = this; |
29 | const self = this |
|
30 | |
30 | |
|
31 | // Read the resource and cache or not depending on configuration settings. |
31 | // Read the resource and cache or not depending on configuration settings. |
|
32 | fs.stat(resource, (error, stats) => { |
32 | fs.stat(resource, (error, stats) => { |
|
33 | var expires = 0; |
33 | var expires = 0 |
|
34 | Object.keys(self.config.site.cache).forEach((key) => { |
34 | Object.keys(self.config.site.cache).forEach((key) => { |
|
Line 35... | Line 35... | |||
35 | self.config.site.cache[key].forEach((expire) => { |
35 | self.config.site.cache[key].forEach((expire) => { |
|
36 | if (expire.test(resource)) { |
36 | if (expire.test(resource)) { |
|
37 | expires = key; |
37 | expires = key |
|
38 | } |
38 | } |
|
39 | }); |
39 | }) |
|
40 | }); |
40 | }) |
|
41 | |
41 | |
|
42 | switch (self.request.httpVersion) { |
42 | switch (self.request.httpVersion) { |
|
43 | case '1.1': // HTTP 1.1 |
43 | case '1.1': // HTTP 1.1 |
|
44 | self.response.setHeader('Cache-Control', |
44 | self.response.setHeader('Cache-Control', |
|
45 | "max-age=" + expires + ", public" |
45 | "max-age=" + expires + ", public" |
|
46 | ); |
46 | ) |
|
47 | const sha1 = forge.md.sha1.create(); |
47 | const sha1 = forge.md.sha1.create() |
|
48 | const data = new stream.Readable({ |
48 | const data = new stream.Readable({ |
|
49 | objectMode: true, |
49 | objectMode: true, |
|
50 | read(size) {} |
50 | read(size) {} |
|
51 | }); |
51 | }) |
|
Line 52... | Line 52... | |||
52 | input |
52 | input |
|
53 | .on('data', (chunk) => { |
53 | .on('data', (chunk) => { |
|
Line 54... | Line 54... | |||
54 | sha1.update(chunk); |
54 | sha1.update(chunk) |
|
55 | data.push(chunk); |
55 | data.push(chunk) |
|
56 | }) |
56 | }) |
|
57 | .on('end', () => { |
57 | .on('end', () => { |
|
Line 58... | Line 58... | |||
58 | const etag = sha1.digest().toHex(); |
58 | const etag = sha1.digest().toHex() |
|
59 | |
59 | |
|
60 | // Set the ETag for the resource. |
60 | // Set the ETag for the resource. |
|
61 | self.response.setHeader('ETag', etag); |
61 | self.response.setHeader('ETag', etag) |
|
Line 62... | Line 62... | |||
62 | |
62 | |
|
63 | const ifNoneMatch = Object |
63 | const ifNoneMatch = Object |
|
64 | .getOwnPropertyNames(self.request.headers) |
64 | .getOwnPropertyNames(self.request.headers) |
|
65 | .filter((header) => header.toUpperCase() === |
65 | .filter((header) => header.toUpperCase() === |
|
Line 80... | Line 80... | |||
80 | < tz(stat.mtime).tz('UTC'))) { self.client.address + ':' +> |
80 | < tz(stat.mtime).tz('UTC'))) { self.client.address + ':' +> |
|
81 | < tz(stat.mtime).tz('UTC'))) { self.client.port +> |
81 | < tz(stat.mtime).tz('UTC'))) { self.client.port +> |
|
82 | < tz(stat.mtime).tz('UTC'))) { ' cached resource: ' +> |
82 | < tz(stat.mtime).tz('UTC'))) { ' cached resource: ' +> |
|
83 | < tz(stat.mtime).tz('UTC'))) { resource,> |
83 | < tz(stat.mtime).tz('UTC'))) { resource,> |
|
84 | < tz(stat.mtime).tz('UTC'))) { severity: 'info'> |
84 | < tz(stat.mtime).tz('UTC'))) { severity: 'info'> |
|
85 | < tz(stat.mtime).tz('UTC'))) { });> |
85 | < tz(stat.mtime).tz('UTC'))) { })> |
|
86 | < tz(stat.mtime).tz('UTC'))) { self.emit('data', {> |
86 | < tz(stat.mtime).tz('UTC'))) { self.emit('data', {> |
|
87 | < tz(stat.mtime).tz('UTC'))) { status: 304,> |
87 | < tz(stat.mtime).tz('UTC'))) { status: 304,> |
|
88 | < tz(stat.mtime).tz('UTC'))) { data: new stream.Readable({> |
88 | < tz(stat.mtime).tz('UTC'))) { data: new stream.Readable({> |
|
89 | < tz(stat.mtime).tz('UTC'))) { read(size) {> |
89 | < tz(stat.mtime).tz('UTC'))) { read(size) {> |
|
90 | < tz(stat.mtime).tz('UTC'))) { this.push(null);> |
90 | < tz(stat.mtime).tz('UTC'))) { this.push(null)> |
|
91 | < tz(stat.mtime).tz('UTC'))) { }> |
91 | < tz(stat.mtime).tz('UTC'))) { }> |
|
92 | < tz(stat.mtime).tz('UTC'))) { }),> |
92 | < tz(stat.mtime).tz('UTC'))) { }),> |
|
93 | < tz(stat.mtime).tz('UTC'))) { type: type> |
93 | < tz(stat.mtime).tz('UTC'))) { type: type> |
|
94 | < tz(stat.mtime).tz('UTC'))) { });> |
94 | < tz(stat.mtime).tz('UTC'))) { })> |
|
95 | < tz(stat.mtime).tz('UTC'))) { return;> |
95 | < tz(stat.mtime).tz('UTC'))) { return> |
|
96 | < tz(stat.mtime).tz('UTC'))) { }> |
96 | < tz(stat.mtime).tz('UTC'))) { }> |
|
Line 97... | Line 97... | |||
97 | < tz(stat.mtime).tz('UTC'))) {> |
97 | < tz(stat.mtime).tz('UTC'))) {> |
|
98 | < tz(stat.mtime).tz('UTC'))) { // Send the resource.> |
98 | < tz(stat.mtime).tz('UTC'))) { // Send the resource.> |
|
99 | < tz(stat.mtime).tz('UTC'))) { self.emit('log', {> |
99 | < tz(stat.mtime).tz('UTC'))) { self.emit('log', {> |
|
100 | < tz(stat.mtime).tz('UTC'))) { message: 'Client: ' +> |
100 | < tz(stat.mtime).tz('UTC'))) { message: 'Client: ' +> |
|
101 | < tz(stat.mtime).tz('UTC'))) { self.client.address + ':' +> |
101 | < tz(stat.mtime).tz('UTC'))) { self.client.address + ':' +> |
|
102 | < tz(stat.mtime).tz('UTC'))) { self.client.port +> |
102 | < tz(stat.mtime).tz('UTC'))) { self.client.port +> |
|
103 | < tz(stat.mtime).tz('UTC'))) { ' sent resource: ' +> |
103 | < tz(stat.mtime).tz('UTC'))) { ' sent resource: ' +> |
|
104 | < tz(stat.mtime).tz('UTC'))) { resource,> |
104 | < tz(stat.mtime).tz('UTC'))) { resource,> |
|
105 | < tz(stat.mtime).tz('UTC'))) { severity: 'info'> |
105 | < tz(stat.mtime).tz('UTC'))) { severity: 'info'> |
|
106 | < tz(stat.mtime).tz('UTC'))) { });> |
106 | < tz(stat.mtime).tz('UTC'))) { })> |
|
107 | < tz(stat.mtime).tz('UTC'))) { data.push(null);> |
107 | < tz(stat.mtime).tz('UTC'))) { data.push(null)> |
|
108 | < tz(stat.mtime).tz('UTC'))) { self.emit('data', {> |
108 | < tz(stat.mtime).tz('UTC'))) { self.emit('data', {> |
|
109 | < tz(stat.mtime).tz('UTC'))) { status: 200,> |
109 | < tz(stat.mtime).tz('UTC'))) { status: 200,> |
|
110 | < tz(stat.mtime).tz('UTC'))) { data: data,> |
110 | < tz(stat.mtime).tz('UTC'))) { data: data,> |
|
111 | < tz(stat.mtime).tz('UTC'))) { type: type> |
111 | < tz(stat.mtime).tz('UTC'))) { type: type> |
|
112 | < tz(stat.mtime).tz('UTC'))) { });> |
112 | < tz(stat.mtime).tz('UTC'))) { })> |
|
Line 113... | Line 113... | |||
113 | < tz(stat.mtime).tz('UTC'))) { });> |
113 | < tz(stat.mtime).tz('UTC'))) { })> |
|
114 | < tz(stat.mtime).tz('UTC'))) {> |
114 | < tz(stat.mtime).tz('UTC'))) {> |
|
115 | < tz(stat.mtime).tz('UTC'))) { return;> |
115 | < tz(stat.mtime).tz('UTC'))) { return> |
|
116 | < tz(stat.mtime).tz('UTC'))) { default:> |
116 | < tz(stat.mtime).tz('UTC'))) { default:> |
|
117 | < tz(stat.mtime).tz('UTC'))) { self.response.setHeader('Last-Modified',> |
117 | < tz(stat.mtime).tz('UTC'))) { self.response.setHeader('Last-Modified',> |
|
118 | < tz(stat.mtime).tz('UTC'))) { tz(stats.mtime)> |
118 | < tz(stat.mtime).tz('UTC'))) { tz(stats.mtime)> |
|
119 | < tz(stat.mtime).tz('UTC'))) { .tz('UTC')> |
119 | < tz(stat.mtime).tz('UTC'))) { .tz('UTC')> |
|
120 | < tz(stat.mtime).tz('UTC'))) { .format("ddd, DD MMM YYYY HH:mm:ss z")> |
120 | < tz(stat.mtime).tz('UTC'))) { .format("ddd, DD MMM YYYY HH:mm:ss z")> |
|
121 | < tz(stat.mtime).tz('UTC'))) { );> |
121 | < tz(stat.mtime).tz('UTC'))) { )> |
|
122 | < tz(stat.mtime).tz('UTC'))) { self.response.setHeader('Expires',> |
122 | < tz(stat.mtime).tz('UTC'))) { self.response.setHeader('Expires',> |
|
123 | < tz(stat.mtime).tz('UTC'))) { tz()> |
123 | < tz(stat.mtime).tz('UTC'))) { tz()> |
|
124 | < tz(stat.mtime).tz('UTC'))) { .tz('UTC')> |
124 | < tz(stat.mtime).tz('UTC'))) { .tz('UTC')> |
|
125 | < tz(stat.mtime).tz('UTC'))) { .add(expires, 'seconds')> |
125 | < tz(stat.mtime).tz('UTC'))) { .add(expires, 'seconds')> |
|
126 | < tz(stat.mtime).tz('UTC'))) { .format("ddd, DD MMM YYYY HH:mm:ss z")> |
126 | < tz(stat.mtime).tz('UTC'))) { .format("ddd, DD MMM YYYY HH:mm:ss z")> |
|
127 | < tz(stat.mtime).tz('UTC'))) { );> |
127 | < tz(stat.mtime).tz('UTC'))) { )> |
|
128 | < tz(stat.mtime).tz('UTC'))) { // Send the resource.> |
128 | < tz(stat.mtime).tz('UTC'))) { // Send the resource.> |
|
129 | < tz(stat.mtime).tz('UTC'))) { self.emit('log', {> |
129 | < tz(stat.mtime).tz('UTC'))) { self.emit('log', {> |
|
130 | < tz(stat.mtime).tz('UTC'))) { message: 'Client: ' +> |
130 | < tz(stat.mtime).tz('UTC'))) { message: 'Client: ' +> |
|
131 | < tz(stat.mtime).tz('UTC'))) { self.client.address + ':' +> |
131 | < tz(stat.mtime).tz('UTC'))) { self.client.address + ':' +> |
|
132 | < tz(stat.mtime).tz('UTC'))) { self.client.port +> |
132 | < tz(stat.mtime).tz('UTC'))) { self.client.port +> |
|
133 | < tz(stat.mtime).tz('UTC'))) { ' sent resource: ' +> |
133 | < tz(stat.mtime).tz('UTC'))) { ' sent resource: ' +> |
|
134 | < tz(stat.mtime).tz('UTC'))) { resource,> |
134 | < tz(stat.mtime).tz('UTC'))) { resource,> |
|
135 | < tz(stat.mtime).tz('UTC'))) { severity: 'info'> |
135 | < tz(stat.mtime).tz('UTC'))) { severity: 'info'> |
|
136 | < tz(stat.mtime).tz('UTC'))) { });> |
136 | < tz(stat.mtime).tz('UTC'))) { })> |
|
137 | < tz(stat.mtime).tz('UTC'))) { self.emit('data', {> |
137 | < tz(stat.mtime).tz('UTC'))) { self.emit('data', {> |
|
138 | < tz(stat.mtime).tz('UTC'))) { status: 200,> |
138 | < tz(stat.mtime).tz('UTC'))) { status: 200,> |
|
139 | < tz(stat.mtime).tz('UTC'))) { data: input,> |
139 | < tz(stat.mtime).tz('UTC'))) { data: input,> |
|
140 | < tz(stat.mtime).tz('UTC'))) { type: type> |
140 | < tz(stat.mtime).tz('UTC'))) { type: type> |
|
141 | < tz(stat.mtime).tz('UTC'))) { });> |
141 | < tz(stat.mtime).tz('UTC'))) { })> |
|
142 | < tz(stat.mtime).tz('UTC'))) { break;> |
142 | < tz(stat.mtime).tz('UTC'))) { break> |
|
Line 143... | Line 143... | |||
143 | < tz(stat.mtime).tz('UTC'))) { }> |
143 | < tz(stat.mtime).tz('UTC'))) { }> |
|
144 | < tz(stat.mtime).tz('UTC'))) { });> |
144 | < tz(stat.mtime).tz('UTC'))) { })> |
|
Line 145... | Line 145... | |||
145 | < tz(stat.mtime).tz('UTC'))) {> |
145 | < tz(stat.mtime).tz('UTC'))) {> |
|
146 | < tz(stat.mtime).tz('UTC'))) { return this;> |
146 | < tz(stat.mtime).tz('UTC'))) { return this> |