| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package CGI::MakeItStatic; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
838
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings (); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
12
|
|
|
5
|
1
|
|
|
1
|
|
436
|
use IO::Scalar (); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Cwd (); |
|
7
|
|
|
|
|
|
|
use Carp (); |
|
8
|
|
|
|
|
|
|
use overload '""' => sub { my($self) = @_; return $self->has_static ? 0 : $self }; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$| = 1; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub check{ |
|
15
|
|
|
|
|
|
|
my($class, $q, $attr) = @_; |
|
16
|
|
|
|
|
|
|
my $self = {}; |
|
17
|
|
|
|
|
|
|
bless $self => $class; |
|
18
|
|
|
|
|
|
|
$attr ||= {}; |
|
19
|
|
|
|
|
|
|
$attr->{dir} or Carp::croak('usage: CGI::MakeItStatic->check($q, {dir => "data_dir"})'); |
|
20
|
|
|
|
|
|
|
$attr->{renew_key} ||= "renew"; |
|
21
|
|
|
|
|
|
|
$attr->{keys} ||= []; |
|
22
|
|
|
|
|
|
|
$attr->{forbid} ||= sub {return 0}; |
|
23
|
|
|
|
|
|
|
$attr->{forbidrenew} ||= sub {return 0}; |
|
24
|
|
|
|
|
|
|
$attr->{noprint} ||= 0; |
|
25
|
|
|
|
|
|
|
$self->{attr} = $attr; |
|
26
|
|
|
|
|
|
|
$self->{has_static} = 0; |
|
27
|
|
|
|
|
|
|
$self->{output} = ''; |
|
28
|
|
|
|
|
|
|
return if ref $attr->{forbid} eq 'CODE' and $attr->{forbid}->($q); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $static_file = $self->_file_name($q); |
|
31
|
|
|
|
|
|
|
return unless $static_file; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$self->has_static($self->_check($q, $static_file)); |
|
34
|
|
|
|
|
|
|
return $self; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _check{ |
|
38
|
|
|
|
|
|
|
my($self, $q, $static_file) = @_; |
|
39
|
|
|
|
|
|
|
my $attr = $self->attr; |
|
40
|
|
|
|
|
|
|
my $renew = $attr->{forbidrenew}->($q) ? 0 : $q->param($attr->{renew_key}); |
|
41
|
|
|
|
|
|
|
if(not $renew and -e $static_file){ |
|
42
|
|
|
|
|
|
|
# create new static file |
|
43
|
|
|
|
|
|
|
open my $in, "<", $static_file or die("cannot open file to read: " . $static_file); |
|
44
|
|
|
|
|
|
|
seek($in, 0, 0); |
|
45
|
|
|
|
|
|
|
local $/ = undef; |
|
46
|
|
|
|
|
|
|
my $text = <$in>; |
|
47
|
|
|
|
|
|
|
close $in; |
|
48
|
|
|
|
|
|
|
print $text unless $self->attr->{noprint}; |
|
49
|
|
|
|
|
|
|
$self->{output} = $text; |
|
50
|
|
|
|
|
|
|
return 1; |
|
51
|
|
|
|
|
|
|
}else{ |
|
52
|
|
|
|
|
|
|
my $data = ""; |
|
53
|
|
|
|
|
|
|
$self->{stdout} = IO::Scalar->new(\$data); |
|
54
|
|
|
|
|
|
|
$self->{static_file} = $static_file; |
|
55
|
|
|
|
|
|
|
$self->{original_select} = select(); |
|
56
|
|
|
|
|
|
|
select($self->{stdout}); |
|
57
|
|
|
|
|
|
|
return 0; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _file_name{ |
|
62
|
|
|
|
|
|
|
my($self, $q) = @_; |
|
63
|
|
|
|
|
|
|
my $attr = $self->attr; |
|
64
|
|
|
|
|
|
|
my @str; |
|
65
|
|
|
|
|
|
|
$attr->{dir} =~ s|/+|/|; |
|
66
|
|
|
|
|
|
|
my $file = Cwd::abs_path($0); |
|
67
|
|
|
|
|
|
|
my $name; |
|
68
|
|
|
|
|
|
|
if(ref(my $name_code = $attr->{name}) eq 'CODE'){ |
|
69
|
|
|
|
|
|
|
$name = $q->escape($name_code->($q, $file)); |
|
70
|
|
|
|
|
|
|
}else{ |
|
71
|
|
|
|
|
|
|
foreach my $key (sort{$a cmp $b} (@{$attr->{keys}} || $q->param)){ |
|
72
|
|
|
|
|
|
|
next if $key eq $attr->{renew_key}; |
|
73
|
|
|
|
|
|
|
my @value = $q->param($key); |
|
74
|
|
|
|
|
|
|
foreach my $v (sort {$a cmp $b} @value){ |
|
75
|
|
|
|
|
|
|
push @str, "$key=$v"; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
return unless @str; |
|
79
|
|
|
|
|
|
|
$name = $q->escape($file . '?' . join "&", @str); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
return $attr->{dir} . '/' . $name; |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub attr{ my $self = shift; @_ ? $self->{attr}->{shift()} : $self->{attr}} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub has_static{ my $self = shift; return @_ ? $self->{has_static} = shift : $self->{has_static}; } |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub output{ my($self) = @_; return my $output = $self->{output}; } |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub end{ my($self) = @_; $self->DESTROY; } |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub DESTROY{ |
|
93
|
|
|
|
|
|
|
my($self) = @_; |
|
94
|
|
|
|
|
|
|
unless($self->{destroy}++){ |
|
95
|
|
|
|
|
|
|
my $s = $self->{stdout}; |
|
96
|
|
|
|
|
|
|
if(ref $s){ |
|
97
|
|
|
|
|
|
|
my $output = $s; |
|
98
|
|
|
|
|
|
|
die "no output" unless $output; |
|
99
|
|
|
|
|
|
|
open my $out, ">", $self->{static_file} or die "cannot open file to write: " . $self->{static_file}; |
|
100
|
|
|
|
|
|
|
seek($out, 0,0); |
|
101
|
|
|
|
|
|
|
print $out $output; |
|
102
|
|
|
|
|
|
|
close $out; |
|
103
|
|
|
|
|
|
|
select($self->{original_select}); |
|
104
|
|
|
|
|
|
|
print $output unless $self->attr->{noprint}; |
|
105
|
|
|
|
|
|
|
$self->{output} = $output; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
1; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |