File Coverage

blib/lib/WebDAO/Util.pm
Criterion Covered Total %
statement 46 61 75.4
branch 7 14 50.0
condition 8 23 34.7
subroutine 10 11 90.9
pod 3 3 100.0
total 74 112 66.0


line stmt bran cond sub pod time code
1             #===============================================================================
2             #
3             # DESCRIPTION: Set of service subs
4             #
5             # AUTHOR: Aliaksandr P. Zahatski, <zag@cpan.org>
6             #===============================================================================
7             package WebDAO::Util;
8 5     5   529 use strict;
  5         14  
  5         137  
9 5     5   28 use warnings;
  5         12  
  5         168  
10 5     5   27 use Carp;
  5         12  
  5         251  
11 5     5   751 use WebDAO::Engine;
  5         10  
  5         201  
12 5     5   745 use WebDAO::Session;
  5         11  
  5         464  
13             our $VERSION = '0.03';
14              
15             =head2 load_module <package>
16              
17             Check if already loaded package and preload else
18              
19             return : 0 - fail load class
20             1 - suss loaded
21             -1 - already loaded
22              
23             =cut
24              
25             sub load_module {
26 0   0 0 1 0 my $class = shift || return;
27              
28             #check non loaded mods
29 0         0 my ( $main, $module ) = $class =~ m/(.*\:\:)?(\S+)$/;
30 0   0     0 $main ||= 'main::';
31 0         0 $module .= '::';
32 5     5   30 no strict 'refs';
  5         10  
  5         340  
33 0 0       0 unless ( exists $$main{$module} ) {
34 0         0 eval "use $class";
35 0 0       0 if ($@) {
36 0         0 croak "Error register class :$class with $@ ";
37 0         0 return 0;
38             }
39 0         0 return 1;
40             }
41 5     5   26 use strict 'refs';
  5         11  
  5         2680  
42 0         0 -1;
43             }
44              
45             =head2 _parse_str_to_hash <str>
46              
47             convert string like:
48              
49             config=/tmp/tests.ini;host=test.local
50              
51             to hash:
52              
53             {
54             config=>'/tmp/tests.ini',
55             host=>'test.local'
56             }
57              
58             =cut
59              
60             sub _parse_str_to_hash {
61 2     2   4 my $str = shift;
62 2 100       8 return unless $str;
63 1         6 my %hash = map { split( /=/, $_ ) } split( /\s*;\s*/, $str );
  2         7  
64 1         4 foreach ( values %hash ) {
65 2         4 s/^\s+//;
66 2         4 s/\s+^//;
67             }
68 1         4 \%hash;
69             }
70              
71             =head2 get_classes <hash with defaults>
72              
73             Get classes by check ENV variables
74              
75             get_classes( wdEngine=> $def_eng_class)
76              
77             return ref to hash
78              
79             =cut
80              
81             sub get_classes {
82              
83 1     1 1 688 my %defaults = (
84             wdEngine => 'WebDAO::Engine',
85             wdSession => 'WebDAO::Session',
86             wdSessionPar => undef,
87             wdEnginePar => undef,
88             @_
89             );
90 1   50     6 my $env = delete $defaults{__env} || \%ENV;
91 1   50     5 my $need_preload = delete $defaults{__preload} || 0;
92              
93             $defaults{wdSession} =
94             $env->{WD_SESSION}
95             || $env->{wdSession}
96 1   33     8 || $defaults{wdSession};
97             $defaults{wdEngine} =
98             $env->{WD_ENGINE}
99             || $env->{wdEngine}
100 1   33     8 || $defaults{wdEngine};
101              
102             #init params
103             $defaults{wdEnginePar} =
104             WebDAO::Util::_parse_str_to_hash( $env->{WD_ENGINE_PAR}
105             || $env->{wdEnginePar} )
106 1   50     6 || {};
107             $defaults{wdSessionPar} =
108             WebDAO::Util::_parse_str_to_hash( $env->{WD_SESSION_PAR}
109             || $env->{wdSessionPar} )
110 1   50     5 || {};
111              
112 1 50       3 if ($need_preload) {
113 0         0 for (qw/wdSession wdEngine /) {
114 0         0 WebDAO::Util::load_module( $defaults{$_} );
115             }
116             }
117              
118 1         3 \%defaults;
119            
120             }
121              
122             =head2 expire_calc <time shift str>
123              
124             Calculate time from str
125              
126             expire_calc('+1d') # current time() + 1 day
127             expire_calc('+1y') # current time() + 1 year
128             expire_calc('+1M') # current time() + 1 Month
129             expire_calc('+1m') # current time() + 1 minute
130              
131             return : <unix_timestamp>
132              
133             =cut
134              
135             # This internal routine creates an expires time exactly some number of
136             # hours from the current time. It incorporates modifications from
137             # Mark Fisher
138              
139             sub expire_calc {
140 2     2 1 4 my($time) = @_;
141 2         8 my(%mult) = ('s'=>1,
142             'm'=>60,
143             'h'=>60*60,
144             'd'=>60*60*24,
145             'M'=>60*60*24*30,
146             'y'=>60*60*24*365);
147             # format for time can be in any of the forms...
148             # "now" -- expire immediately
149             # "+180s" -- in 180 seconds
150             # "+2m" -- in 2 minutes
151             # "+12h" -- in 12 hours
152             # "+1d" -- in 1 day
153             # "+3M" -- in 3 months
154             # "+2y" -- in 2 years
155             # "-3m" -- 3 minutes ago(!)
156             # If you don't supply one of these forms, we assume you are
157             # specifying the date yourself
158 2         3 my($offset);
159 2 50 33     26 if (!$time || (lc($time) eq 'now')) {
    100          
    50          
160 0         0 $offset = 0;
161             } elsif ($time=~/^\d+/) {
162 1         5 return $time;
163             } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([smhdMy])/) {
164 1   50     6 $offset = ($mult{$2} || 1)*$1;
165             } else {
166 0         0 return $time;
167             }
168 1         6 my $cur_time = time;
169 1         8 return ($cur_time+$offset);
170             }
171              
172              
173             our %HTTPStatusCode = (
174             100 => 'Continue',
175             101 => 'Switching Protocols',
176             102 => 'Processing', # RFC 2518 (WebDAV)
177             200 => 'OK',
178             201 => 'Created',
179             202 => 'Accepted',
180             203 => 'Non-Authoritative Information',
181             204 => 'No Content',
182             205 => 'Reset Content',
183             206 => 'Partial Content',
184             207 => 'Multi-Status', # RFC 2518 (WebDAV)
185             300 => 'Multiple Choices',
186             301 => 'Moved Permanently',
187             302 => 'Found',
188             303 => 'See Other',
189             304 => 'Not Modified',
190             305 => 'Use Proxy',
191             307 => 'Temporary Redirect',
192             400 => 'Bad Request',
193             401 => 'Unauthorized',
194             402 => 'Payment Required',
195             403 => 'Forbidden',
196             404 => 'Not Found',
197             405 => 'Method Not Allowed',
198             406 => 'Not Acceptable',
199             407 => 'Proxy Authentication Required',
200             408 => 'Request Timeout',
201             409 => 'Conflict',
202             410 => 'Gone',
203             411 => 'Length Required',
204             412 => 'Precondition Failed',
205             413 => 'Request Entity Too Large',
206             414 => 'Request-URI Too Large',
207             415 => 'Unsupported Media Type',
208             416 => 'Request Range Not Satisfiable',
209             417 => 'Expectation Failed',
210             422 => 'Unprocessable Entity', # RFC 2518 (WebDAV)
211             423 => 'Locked', # RFC 2518 (WebDAV)
212             424 => 'Failed Dependency', # RFC 2518 (WebDAV)
213             425 => 'No code', # WebDAV Advanced Collections
214             426 => 'Upgrade Required', # RFC 2817
215             449 => 'Retry with', # unofficial Microsoft
216             500 => 'Internal Server Error',
217             501 => 'Not Implemented',
218             502 => 'Bad Gateway',
219             503 => 'Service Unavailable',
220             504 => 'Gateway Timeout',
221             505 => 'HTTP Version Not Supported',
222             506 => 'Variant Also Negotiates', # RFC 2295
223             507 => 'Insufficient Storage', # RFC 2518 (WebDAV)
224             509 => 'Bandwidth Limit Exceeded', # unofficial
225             510 => 'Not Extended', # RFC 2774
226             );
227              
228             1;
229