line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::TrafficAdvice; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: handle requests for /.well-known/traffic-advice |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
1629
|
use v5.8.5; |
|
3
|
|
|
|
|
16
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
14
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
49
|
|
8
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
62
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
13
|
use parent 'Plack::Middleware'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
12
|
|
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
218
|
use Plack::Util::Accessor qw/ data file /; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
18
|
|
13
|
|
|
|
|
|
|
|
14
|
3
|
|
|
3
|
|
173
|
use Cwd; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
207
|
|
15
|
3
|
|
|
3
|
|
1801
|
use File::Temp qw/ tempfile /; |
|
3
|
|
|
|
|
24172
|
|
|
3
|
|
|
|
|
154
|
|
16
|
3
|
|
|
3
|
|
1092
|
use HTTP::Date; |
|
3
|
|
|
|
|
9704
|
|
|
3
|
|
|
|
|
161
|
|
17
|
3
|
|
|
3
|
|
20
|
use HTTP::Status qw/ :constants /; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1000
|
|
18
|
3
|
|
|
3
|
|
19
|
use JSON::MaybeXS 1.004000; |
|
3
|
|
|
|
|
61
|
|
|
3
|
|
|
|
|
1302
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = 'v0.2.4'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub prepare_app { |
24
|
3
|
|
|
3
|
1
|
264
|
my ($self) = @_; |
25
|
|
|
|
|
|
|
|
26
|
3
|
100
|
|
|
|
12
|
if (my $data = $self->data) { |
|
|
50
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
2
|
50
|
|
|
|
97
|
if ($self->file) { |
29
|
0
|
|
|
|
|
0
|
die "Cannot specify both data and file"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
2
|
|
|
|
|
41
|
my ($fh, $filename) = tempfile('traffic-advice-XXXXXXXX', SUFFIX => '.json', UNLINK => 0, TMPDIR => 1); |
33
|
2
|
|
|
|
|
935
|
$self->file( $filename ); |
34
|
|
|
|
|
|
|
|
35
|
2
|
100
|
|
|
|
31
|
if (ref($data)) { |
36
|
1
|
|
|
|
|
8
|
my $encoder = JSON::MaybeXS->new( { utf8 => 1 } ); |
37
|
1
|
50
|
|
|
|
19
|
print {$fh} $encoder->encode($data) |
|
1
|
|
|
|
|
27
|
|
38
|
|
|
|
|
|
|
or die "Unable to write data"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
else { |
41
|
1
|
50
|
|
|
|
2
|
print {$fh} $data |
|
1
|
|
|
|
|
6
|
|
42
|
|
|
|
|
|
|
or die "Unable to write data"; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
100
|
close $fh; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
elsif (my $file = $self->file) { |
50
|
|
|
|
|
|
|
|
51
|
1
|
50
|
|
|
|
71
|
unless (-r $file) { |
52
|
0
|
|
|
|
|
0
|
die "Cannot read file: '$file'"; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else { |
57
|
0
|
|
|
|
|
0
|
die "Either data or file must be configured"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub call { |
63
|
5
|
|
|
5
|
1
|
67329
|
my ( $self, $env ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
5
|
50
|
|
|
|
22
|
unless ( $env->{REQUEST_URI} eq '/.well-known/traffic-advice' ) { |
66
|
0
|
|
|
|
|
0
|
return $self->app->($env); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
5
|
100
|
|
|
|
37
|
unless ( $env->{REQUEST_METHOD} =~ /^(GET|HEAD)$/ ) { |
70
|
1
|
|
|
|
|
4
|
return $self->error( HTTP_METHOD_NOT_ALLOWED, "Not Allowed" ); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
4
|
|
|
|
|
20
|
my $file = $self->file; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Some of this is based on Plack::App::File. |
76
|
|
|
|
|
|
|
|
77
|
4
|
50
|
|
|
|
173
|
open my $fh, "<:raw", $file |
78
|
|
|
|
|
|
|
or return $self->error( HTTP_INTERNAL_SERVER_ERROR, "Internal Error" ); |
79
|
|
|
|
|
|
|
|
80
|
4
|
|
|
|
|
51
|
my @stat = stat $file; |
81
|
|
|
|
|
|
|
|
82
|
4
|
|
|
|
|
131
|
Plack::Util::set_io_path($fh, Cwd::realpath($file)); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
[ |
85
|
4
|
|
|
|
|
136
|
HTTP_OK, |
86
|
|
|
|
|
|
|
[ |
87
|
|
|
|
|
|
|
'Content-Type' => 'application/trafficadvice+json', |
88
|
|
|
|
|
|
|
'Content-Length' => $stat[7], |
89
|
|
|
|
|
|
|
'Last-Modified' => HTTP::Date::time2str( $stat[9] ) |
90
|
|
|
|
|
|
|
], |
91
|
|
|
|
|
|
|
$fh, |
92
|
|
|
|
|
|
|
]; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub error { |
98
|
1
|
|
|
1
|
0
|
3
|
my ($self, $code, $message) = @_; |
99
|
1
|
|
|
|
|
9
|
return [ $code, [ 'Content-Type' => 'text/plain', 'Content-Length' => length($message) ], [ $message ] ]; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |