aboutsummaryrefslogtreecommitdiffstats
path: root/include/nms/web.pm
blob: 7c9339e1c7a76332a4c3079f7e1e0b9486d7adca (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/perl
# vim:ts=8:sw=8
use strict;
use warnings;
use utf8;
use DBI;
use Data::Dumper;
use JSON;
use nms;
use Digest::SHA;
use FreezeThaw;
use URI::Escape;
package nms::web;

use base 'Exporter';
our %get_params;
our %json;
our @EXPORT = qw(finalize_output now json $dbh db_safe_quote %get_params get_input %json);
our $dbh;
our $now;
our $when;
our %cc;

sub get_input {
	my $in = "";
	while(<STDIN>) { $in .= $_; }
	return $in;
}
# Print cache-control from %cc
sub printcc {
	my $line = "";
	my $first = "";
	foreach my $tmp (keys(%cc)) {
		$line .= $first . $tmp . "=" . $cc{$tmp};
		$first = ", ";
	}
	print 'Cache-Control: ' . $line . "\n";
}

sub db_safe_quote {
	my $word = $_[0];
	my $term = $get_params{$word};
	if (!defined($term)) {
		if(defined($_[1])) {
			$term = $_[1];
		} else {
			die "Missing CGI param $word";
		}
	}
	return $dbh->quote($term) || die;
}

# returns a valid $when statement
# Also sets cache-control headers if time is overridden
# This can be called explicitly to override the window of time we evaluate.
# Normally up to 15 minutes old data will be returned, but for some API
# endpoints it is better to return no data than old data (e.g.: ping).
sub setwhen {
	$now = "now()";
	my $window = '8m';
	my $offset = '0s';
	if (@_ > 0) {
		$window = $_[0];
	}
	if (@_ > 1) {
		$offset = $_[1];
	}
	if (defined($get_params{'now'})) {
		$now = db_safe_quote('now') . "::timestamp with time zone ";
		$cc{'max-age'} = "3600";
	}
	$now = "(" . $now . " - '" . $offset . "'::interval)";
	$when = " time > " . $now . " - '".$window."'::interval and time < " . $now . " ";
}

sub finalize_output {
	my $query;
	my $hash = Digest::SHA::sha512_base64(FreezeThaw::freeze(%json));
	$dbh->commit;
	$query = $dbh->prepare('select extract(epoch from date_trunc(\'seconds\', ' . $now . ')) as time;');
	$query->execute();

	$json{'time'} = int($query->fetchrow_hashref()->{'time'});
	$json{'hash'} = $hash;
	
	printcc;
	
	print "Etag: $hash\n";
	print "Access-Control-Allow-Origin: *\n";
	print "Access-Control-Allow-Methods: HEAD, GET\n";
	print "Content-Type: text/json; charset=utf-8\n\n";
	print JSON::XS::encode_json(\%json);
	print "\n";
}

sub populate_params {
	my $querystring = $ENV{'QUERY_STRING'} || "";
	foreach my $hdr (split("&",$querystring)) {
		my ($key, $value) = split("=",$hdr,"2");
		$get_params{$key} = URI::Escape::uri_unescape($value);
	}
}

BEGIN {
	$cc{'stale-while-revalidate'} = "3600";
	$cc{'max-age'} = "20";

	$dbh = nms::db_connect();
	populate_params();
	setwhen();
}
1;