var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
// The current contents of the file to hand out, and its last modified time.
-var json_contents = undefined;
-var json_contents_gz = undefined;
-var json_last_modified = undefined;
+var json = undefined;
// The list of clients that are waiting for new data to show up.
// Uniquely keyed by request_id so that we can take them out of
// hanging indefinitely (which might have them return errors).
var touch_timer = undefined;
+// If we are behind Varnish, we can't count the number of clients
+// ourselves, so some external log-tailing daemon needs to tell us.
+var viewer_count_override = undefined;
+
+var replace_json = function(new_json_contents, mtime) {
+ var new_json = {
+ parsed: JSON.parse(new_json_contents),
+ plain: new_json_contents,
+ last_modified: mtime
+ };
+
+ // gzip the new version, and put it into place.
+ zlib.gzip(new_json_contents, function(err, buffer) {
+ if (err) throw err;
+
+ new_json.gzip = buffer;
+ json = new_json;
+
+ // Finally, wake up any sleeping clients.
+ possibly_wakeup_clients();
+ });
+}
+
var reread_file = function(event, filename) {
if (filename != path.basename(json_filename)) {
return;
if (err) throw err;
fs.close(fd, function() {
var new_json_contents = buffer.toString('utf8', 0, bytesRead);
- zlib.gzip(new_json_contents, function(err, buffer) {
- if (err) throw err;
- json_contents = new_json_contents;
- json_contents_gz = buffer;
- json_last_modified = st.mtime.getTime();
- possibly_wakeup_clients();
- });
+ replace_json(new_json_contents, st.mtime.getTime());
});
});
});
response.write('Something went wrong. Sorry.');
response.end();
}
+var handle_viewer_override = function(request, u, response) {
+ // Only accept requests from localhost.
+ var peer = request.socket.localAddress;
+ if ((peer != '127.0.0.1' && peer != '::1') || request.headers['x-forwarded-for']) {
+ console.log("Refusing viewer override from " + peer);
+ send_404(response);
+ } else {
+ viewer_count_override = (u.query)['num'];
+ response.writeHead(200, {
+ 'Content-Type': 'text/plain',
+ });
+ response.write('OK.');
+ response.end();
+ }
+}
var send_json = function(response, accept_gzip, num_viewers) {
var headers = {
'Content-Type': 'text/json',
- 'X-Remoteglot-Last-Modified': json_last_modified,
+ 'X-Remoteglot-Last-Modified': json.last_modified,
'X-Remoteglot-Num-Viewers': num_viewers,
- 'Access-Control-Allow-Origin': 'http://analysis.sesse.net',
'Access-Control-Expose-Headers': 'X-Remoteglot-Last-Modified, X-Remoteglot-Num-Viewers',
'Expires': 'Mon, 01 Jan 1970 00:00:00 UTC',
+ 'Vary': 'Accept-Encoding',
};
if (accept_gzip) {
headers['Content-Encoding'] = 'gzip';
response.writeHead(200, headers);
- response.write(json_contents_gz);
+ response.write(json.gzip);
} else {
response.writeHead(200, headers);
- response.write(json_contents);
+ response.write(json.text);
}
response.end();
}
}
}
var count_viewers = function() {
+ if (viewer_count_override !== undefined) {
+ return viewer_count_override;
+ }
+
var now = (new Date).getTime();
// Go through and remove old viewers, and count them at the same time.
var unique = (u.query)['unique'];
console.log((new Date).getTime()*1e-3 + " " + request.url);
-
+ if (u.pathname === '/override-num-viewers') {
+ handle_viewer_override(request, u, response);
+ return;
+ }
if (u.pathname !== '/analysis.pl') {
// This is not the request you are looking for.
send_404(response);
// If we already have something newer than what the user has,
// just send it out and be done with it.
- if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) {
+ if (json !== undefined && (!ims || json.last_modified > ims)) {
send_json(response, accept_gzip, count_viewers());
return;
}