line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cake::Utils;
|
2
|
8
|
|
|
8
|
|
41
|
use warnings;
|
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
204
|
|
3
|
8
|
|
|
8
|
|
39
|
use strict;
|
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
268
|
|
4
|
8
|
|
|
8
|
|
42
|
use Carp;
|
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
481
|
|
5
|
8
|
|
|
8
|
|
4511
|
use Cake::Utils::Serializer;
|
|
8
|
|
|
|
|
19
|
|
|
8
|
|
|
|
|
244
|
|
6
|
8
|
|
|
8
|
|
43
|
use base 'Exporter';
|
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
12434
|
|
7
|
|
|
|
|
|
|
our @EXPORT = qw(
|
8
|
|
|
|
|
|
|
run_once
|
9
|
|
|
|
|
|
|
get_file
|
10
|
|
|
|
|
|
|
create_file
|
11
|
|
|
|
|
|
|
create_folder
|
12
|
|
|
|
|
|
|
crlf
|
13
|
|
|
|
|
|
|
serialize
|
14
|
|
|
|
|
|
|
);
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.004';
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# copied from Plack which in return copied from HTTP::Status
|
19
|
|
|
|
|
|
|
my %StatusCode = (
|
20
|
|
|
|
|
|
|
100 => 'Continue',
|
21
|
|
|
|
|
|
|
101 => 'Switching Protocols',
|
22
|
|
|
|
|
|
|
102 => 'Processing', # RFC 2518 (WebDAV)
|
23
|
|
|
|
|
|
|
200 => 'OK',
|
24
|
|
|
|
|
|
|
201 => 'Created',
|
25
|
|
|
|
|
|
|
202 => 'Accepted',
|
26
|
|
|
|
|
|
|
203 => 'Non-Authoritative Information',
|
27
|
|
|
|
|
|
|
204 => 'No Content',
|
28
|
|
|
|
|
|
|
205 => 'Reset Content',
|
29
|
|
|
|
|
|
|
206 => 'Partial Content',
|
30
|
|
|
|
|
|
|
207 => 'Multi-Status', # RFC 2518 (WebDAV)
|
31
|
|
|
|
|
|
|
300 => 'Multiple Choices',
|
32
|
|
|
|
|
|
|
301 => 'Moved Permanently',
|
33
|
|
|
|
|
|
|
302 => 'Found',
|
34
|
|
|
|
|
|
|
303 => 'See Other',
|
35
|
|
|
|
|
|
|
304 => 'Not Modified',
|
36
|
|
|
|
|
|
|
305 => 'Use Proxy',
|
37
|
|
|
|
|
|
|
307 => 'Temporary Redirect',
|
38
|
|
|
|
|
|
|
400 => 'Bad Request',
|
39
|
|
|
|
|
|
|
401 => 'Unauthorized',
|
40
|
|
|
|
|
|
|
402 => 'Payment Required',
|
41
|
|
|
|
|
|
|
403 => 'Forbidden',
|
42
|
|
|
|
|
|
|
404 => 'Not Found',
|
43
|
|
|
|
|
|
|
405 => 'Method Not Allowed',
|
44
|
|
|
|
|
|
|
406 => 'Not Acceptable',
|
45
|
|
|
|
|
|
|
407 => 'Proxy Authentication Required',
|
46
|
|
|
|
|
|
|
408 => 'Request Timeout',
|
47
|
|
|
|
|
|
|
409 => 'Conflict',
|
48
|
|
|
|
|
|
|
410 => 'Gone',
|
49
|
|
|
|
|
|
|
411 => 'Length Required',
|
50
|
|
|
|
|
|
|
412 => 'Precondition Failed',
|
51
|
|
|
|
|
|
|
413 => 'Request Entity Too Large',
|
52
|
|
|
|
|
|
|
414 => 'Request-URI Too Large',
|
53
|
|
|
|
|
|
|
415 => 'Unsupported Media Type',
|
54
|
|
|
|
|
|
|
416 => 'Request Range Not Satisfiable',
|
55
|
|
|
|
|
|
|
417 => 'Expectation Failed',
|
56
|
|
|
|
|
|
|
422 => 'Unprocessable Entity', # RFC 2518 (WebDAV)
|
57
|
|
|
|
|
|
|
423 => 'Locked', # RFC 2518 (WebDAV)
|
58
|
|
|
|
|
|
|
424 => 'Failed Dependency', # RFC 2518 (WebDAV)
|
59
|
|
|
|
|
|
|
425 => 'No code', # WebDAV Advanced Collections
|
60
|
|
|
|
|
|
|
426 => 'Upgrade Required', # RFC 2817
|
61
|
|
|
|
|
|
|
449 => 'Retry with', # unofficial Microsoft
|
62
|
|
|
|
|
|
|
500 => 'Internal Server Error',
|
63
|
|
|
|
|
|
|
501 => 'Not Implemented',
|
64
|
|
|
|
|
|
|
502 => 'Bad Gateway',
|
65
|
|
|
|
|
|
|
503 => 'Service Unavailable',
|
66
|
|
|
|
|
|
|
504 => 'Gateway Timeout',
|
67
|
|
|
|
|
|
|
505 => 'HTTP Version Not Supported',
|
68
|
|
|
|
|
|
|
506 => 'Variant Also Negotiates', # RFC 2295
|
69
|
|
|
|
|
|
|
507 => 'Insufficient Storage', # RFC 2518 (WebDAV)
|
70
|
|
|
|
|
|
|
509 => 'Bandwidth Limit Exceeded', # unofficial
|
71
|
|
|
|
|
|
|
510 => 'Not Extended', # RFC 2774
|
72
|
|
|
|
|
|
|
);
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 get_file
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Get content of a file
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Cake::Utils::get_file('/path/to/some/file');
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub get_file {
|
83
|
0
|
|
|
0
|
1
|
0
|
my $file = shift;
|
84
|
0
|
0
|
|
|
|
0
|
open my $cont, '<', $file or croak "Can't open $file for input:\n$!";
|
85
|
0
|
|
|
|
|
0
|
local $/;
|
86
|
0
|
|
|
|
|
0
|
my $content = <$cont>;
|
87
|
0
|
|
|
|
|
0
|
close $cont;
|
88
|
0
|
|
|
|
|
0
|
return $content;
|
89
|
|
|
|
|
|
|
}
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub create_file {
|
92
|
0
|
|
|
0
|
0
|
0
|
my ($options) = @_;
|
93
|
0
|
|
|
|
|
0
|
my $folder = $options->{folder};
|
94
|
0
|
|
0
|
|
|
0
|
my $file = $options->{file} || croak "You must provide a file name";
|
95
|
0
|
|
0
|
|
|
0
|
my $content = $options->{content} || "";
|
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
0
|
if ($folder){
|
98
|
|
|
|
|
|
|
#create_folder($folder);
|
99
|
0
|
|
|
|
|
0
|
$folder =~ s/\/$//;
|
100
|
0
|
|
|
|
|
0
|
$file =~ s/^\///;
|
101
|
0
|
|
|
|
|
0
|
$file = $folder.'/'.$file;
|
102
|
|
|
|
|
|
|
} else {
|
103
|
0
|
|
|
|
|
0
|
my @paths = split '/', $file;
|
104
|
0
|
|
|
|
|
0
|
my $file = pop @paths;
|
105
|
0
|
|
|
|
|
0
|
my $folder = join '/', @paths;
|
106
|
0
|
|
|
|
|
0
|
$file = $folder.'/'.$file;
|
107
|
|
|
|
|
|
|
}
|
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
0
|
return $file;
|
110
|
|
|
|
|
|
|
}
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub create_folder {
|
113
|
0
|
|
|
0
|
0
|
0
|
my $folder = shift;
|
114
|
0
|
0
|
|
|
|
0
|
if (! -e "$folder") {
|
115
|
0
|
0
|
|
|
|
0
|
mkdir($folder) or croak "Can't create $folder : $!\n";
|
116
|
|
|
|
|
|
|
}
|
117
|
|
|
|
|
|
|
}
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub combineHashes {
|
120
|
0
|
|
|
0
|
0
|
0
|
my ($hash1,$hash2) = @_;
|
121
|
0
|
|
|
|
|
0
|
while (my($key,$value) = each(%{$hash2})) {
|
|
0
|
|
|
|
|
0
|
|
122
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
0
|
if ($hash1->{$key}){
|
124
|
0
|
0
|
|
|
|
0
|
if (!ref $hash1->{$key}){
|
125
|
0
|
|
|
|
|
0
|
my $holds = [$hash1->{$key},$value];
|
126
|
0
|
|
|
|
|
0
|
$hash1->{$key} = $holds;
|
127
|
|
|
|
|
|
|
} else {
|
128
|
0
|
|
|
|
|
0
|
push (@{$hash1->{$key}},$value);
|
|
0
|
|
|
|
|
0
|
|
129
|
|
|
|
|
|
|
}
|
130
|
|
|
|
|
|
|
} else {
|
131
|
0
|
|
|
|
|
0
|
$hash1->{$key} = $value;
|
132
|
|
|
|
|
|
|
}
|
133
|
|
|
|
|
|
|
}
|
134
|
0
|
|
|
|
|
0
|
return $hash1;
|
135
|
|
|
|
|
|
|
}
|
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub content_length {
|
138
|
0
|
|
|
0
|
0
|
0
|
my $body = shift;
|
139
|
0
|
0
|
|
|
|
0
|
return unless defined $body;
|
140
|
0
|
0
|
|
|
|
0
|
if (!ref $body) {
|
|
|
0
|
|
|
|
|
|
141
|
0
|
|
|
|
|
0
|
return length($body);
|
142
|
|
|
|
|
|
|
} elsif ( ref $body eq 'GLOB' ) {
|
143
|
0
|
|
|
|
|
0
|
return tell($body);
|
144
|
|
|
|
|
|
|
}
|
145
|
0
|
|
|
|
|
0
|
return;
|
146
|
|
|
|
|
|
|
}
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
#============================================================================
|
149
|
|
|
|
|
|
|
# Require & noRequire
|
150
|
|
|
|
|
|
|
#============================================================================
|
151
|
|
|
|
|
|
|
sub Require {
|
152
|
4
|
|
|
4
|
0
|
15
|
return noRequire(@_,'Require');
|
153
|
|
|
|
|
|
|
}
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub noRequire {
|
156
|
8
|
|
|
8
|
0
|
16
|
my $class = shift;
|
157
|
8
|
|
|
|
|
11
|
my $namespace = shift;
|
158
|
8
|
|
|
|
|
10
|
my $require = shift;
|
159
|
8
|
50
|
|
|
|
20
|
if ($namespace){
|
160
|
8
|
100
|
|
|
|
30
|
unless ($class =~ s/^\+//){
|
161
|
6
|
|
|
|
|
16
|
$class = $namespace.'::'.$class;
|
162
|
|
|
|
|
|
|
}
|
163
|
|
|
|
|
|
|
}
|
164
|
|
|
|
|
|
|
|
165
|
8
|
|
|
|
|
11
|
my $package = $class;
|
166
|
8
|
|
|
|
|
36
|
$package =~ s/::/\//g;
|
167
|
8
|
|
|
|
|
11
|
$package .= '.pm';
|
168
|
|
|
|
|
|
|
|
169
|
8
|
100
|
|
|
|
21
|
if ($require){
|
170
|
4
|
50
|
|
|
|
14
|
if (!$INC->{$package}){
|
171
|
4
|
|
|
|
|
358
|
eval 'require "$package"';
|
172
|
4
|
50
|
|
|
|
37
|
} if ($@){
|
173
|
0
|
|
|
|
|
0
|
croak ($@);
|
174
|
|
|
|
|
|
|
}
|
175
|
|
|
|
|
|
|
}
|
176
|
8
|
|
|
|
|
37
|
return $class;
|
177
|
|
|
|
|
|
|
}
|
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub get_status_code {
|
180
|
0
|
|
|
0
|
0
|
0
|
my $env = shift;
|
181
|
0
|
|
|
|
|
0
|
my $message = $StatusCode{$_[0]};
|
182
|
0
|
0
|
0
|
|
|
0
|
if ($env->{'http.version'} && $env->{'http.version'} == 1.0){
|
183
|
0
|
|
|
|
|
0
|
return "HTTP/1.0 $_[0] $message\015\012";
|
184
|
|
|
|
|
|
|
}
|
185
|
0
|
|
|
|
|
0
|
return "HTTP/1.1 $_[0] $message\015\012";
|
186
|
|
|
|
|
|
|
}
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
#============================================================================
|
189
|
|
|
|
|
|
|
# convert givin date to epoch
|
190
|
|
|
|
|
|
|
#============================================================================
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 to_epoch('1m')
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
convert predefined values to Unix machine time
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Cake::Utils::to_epoch('1h');
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
#values
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
y = year
|
201
|
|
|
|
|
|
|
M = month
|
202
|
|
|
|
|
|
|
d = day
|
203
|
|
|
|
|
|
|
h = hour
|
204
|
|
|
|
|
|
|
m = minute
|
205
|
|
|
|
|
|
|
"othing" = seconds
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub to_epoch {
|
210
|
0
|
|
0
|
0
|
0
|
0
|
my $length = shift || '1h'; ##one hour is the default value
|
211
|
0
|
|
|
0
|
|
0
|
my $types = {
|
212
|
|
|
|
|
|
|
'm' => sub {return 60*$_[0]}, ##minute
|
213
|
0
|
|
|
0
|
|
0
|
'h' => sub {return 3600*$_[0]}, ##hour
|
214
|
0
|
|
|
0
|
|
0
|
'd' => sub {return (24*3600)*$_[0]}, ##day
|
215
|
0
|
|
|
0
|
|
0
|
'M' => sub {return (30*24*3600)*$_[0]}, ##month
|
216
|
0
|
|
|
0
|
|
0
|
'y' => sub {return (365*24*3600)*$_[0]}, ##year
|
217
|
0
|
|
|
|
|
0
|
};
|
218
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
0
|
my ($num,$type) = $length =~ /(\d*)(\w)/;
|
220
|
0
|
0
|
|
|
|
0
|
$num = 1 if !$num;
|
221
|
0
|
|
|
|
|
0
|
my $expire;
|
222
|
0
|
0
|
|
|
|
0
|
if ($types->{$type}){
|
223
|
0
|
|
|
|
|
0
|
$expire = $types->{$type}->($num);
|
224
|
|
|
|
|
|
|
} else {
|
225
|
0
|
|
0
|
|
|
0
|
$expire = $length || 3600;
|
226
|
|
|
|
|
|
|
}
|
227
|
0
|
|
|
|
|
0
|
return $expire+time();
|
228
|
|
|
|
|
|
|
}
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
#============================================================================
|
231
|
|
|
|
|
|
|
# OS crlf : copied from CGI::Simple
|
232
|
|
|
|
|
|
|
#============================================================================
|
233
|
|
|
|
|
|
|
sub crlf {
|
234
|
|
|
|
|
|
|
#return "\n";
|
235
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $CRLF ) = @_;
|
236
|
0
|
0
|
|
|
|
0
|
$self->{'app.crlf'} = $CRLF if $CRLF; # allow value to be set manually
|
237
|
0
|
0
|
|
|
|
0
|
unless ( $self->{'app.crlf'} ) {
|
238
|
0
|
|
|
|
|
0
|
my $OS = $^O;
|
239
|
0
|
0
|
|
|
|
0
|
$self->{'app.crlf'}
|
240
|
|
|
|
|
|
|
= ( $OS =~ m/VMS/i ) ? "\n"
|
241
|
|
|
|
|
|
|
: ( "\t" ne "\011" ) ? "\r\n"
|
242
|
|
|
|
|
|
|
: "\015\012";
|
243
|
|
|
|
|
|
|
}
|
244
|
|
|
|
|
|
|
|
245
|
0
|
|
|
|
|
0
|
return $self->{'app.crlf'};
|
246
|
|
|
|
|
|
|
}
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
#============================================================================
|
249
|
|
|
|
|
|
|
# Random String Generator
|
250
|
|
|
|
|
|
|
#============================================================================
|
251
|
|
|
|
|
|
|
sub random_string {
|
252
|
0
|
|
0
|
0
|
0
|
0
|
my $num = shift || 16;
|
253
|
0
|
|
|
|
|
0
|
return time().join '',map { sprintf q|%X|, rand($num) } 1 .. 36;
|
|
0
|
|
|
|
|
0
|
|
254
|
|
|
|
|
|
|
}
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
sub serialize {
|
257
|
0
|
|
|
0
|
0
|
0
|
shift; #shift cake class
|
258
|
0
|
|
|
|
|
0
|
return Cake::Utils::Serializer->new(shift);
|
259
|
|
|
|
|
|
|
}
|
260
|
|
|
|
|
|
|
#============================================================================
|
261
|
|
|
|
|
|
|
# Check for persistance
|
262
|
|
|
|
|
|
|
# FIXME: I'm not sure if this has no bugs at all
|
263
|
|
|
|
|
|
|
#
|
264
|
|
|
|
|
|
|
# FROM perldocs
|
265
|
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
266
|
|
|
|
|
|
|
# The CHECK and INIT blocks in code compiled by require, string do, or string
|
267
|
|
|
|
|
|
|
# eval will not be executed if they occur after the end of the main compilation
|
268
|
|
|
|
|
|
|
# phase; that can be a problem in mod_perl and other persistent environments
|
269
|
|
|
|
|
|
|
# which use those functions to load code at runtime.
|
270
|
|
|
|
|
|
|
#
|
271
|
|
|
|
|
|
|
# Which exactly what I'm using as an advantage :)
|
272
|
|
|
|
|
|
|
#============================================================================
|
273
|
|
|
|
|
|
|
my $run_once = 0;
|
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
{
|
276
|
8
|
|
|
8
|
|
72
|
no warnings;
|
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
1120
|
|
277
|
|
|
|
|
|
|
INIT {
|
278
|
5
|
|
|
5
|
|
23
|
$run_once = 1;
|
279
|
|
|
|
|
|
|
}
|
280
|
|
|
|
|
|
|
}
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
sub run_once {
|
283
|
0
|
|
|
0
|
0
|
0
|
my $self = shift;
|
284
|
0
|
0
|
|
|
|
0
|
if (exists $self->env->{run_once}){
|
285
|
0
|
|
|
|
|
0
|
return $self->env->{run_once};
|
286
|
|
|
|
|
|
|
}
|
287
|
0
|
|
|
|
|
0
|
return $run_once;
|
288
|
|
|
|
|
|
|
}
|
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
1;
|
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
__END__
|