1#===============================================================================
2#
3#  DESCRIPTION:  Set of  service subs
4#
5#       AUTHOR:  Aliaksandr P. Zahatski, <zahatski@gmail.com>
6#===============================================================================
7package WebDAO::Util;
8use strict;
9use warnings;
10use Carp;
11use WebDAO::Engine;
12use WebDAO::Session;
13our $VERSION = '0.01';
14
15=head2  load_module <package>
16
17Check if already loaded package and preload else
18
19return :  0  - fail load class
20          1  - suss loaded
21          -1 - already loaded
22
23=cut
24
25sub load_module {
26    my $class = shift || return;
27
28    #check non loaded mods
29    my ( $main, $module ) = $class =~ m/(.*\:\:)?(\S+)$/;
30    $main ||= 'main::';
31    $module .= '::';
32    no strict 'refs';
33    unless ( exists $$main{$module} ) {
34        eval "use $class";
35        if ($@) {
36            croak "Error register class :$class with $@ ";
37            return 0;
38        }
39        return 1;
40    }
41    use strict 'refs';
42    -1;
43}
44
45=head2 _parse_str_to_hash <str>
46
47convert string like:
48
49    config=/tmp/tests.ini;host=test.local
50
51to hash:
52
53    {
54      config=>'/tmp/tests.ini',
55      host=>'test.local'
56    }
57
58=cut 
59
60sub _parse_str_to_hash {
61    my $str = shift;
62    return unless $str;
63    my %hash = map { split( /=/, $_ ) } split( /;/, $str );
64    foreach ( values %hash ) {
65        s/^\s+//;
66        s/\s+^//;
67    }
68    \%hash;
69}
70
71=head2 get_classes <hash with defaults>
72
73Get classes by check ENV variables
74
75    get_classes( wdEngine=> $def_eng_class)
76
77return ref to hash
78
79=cut
80
81sub get_classes {
82
83    my %defaults = (
84        wdEngine     => 'WebDAO::Engine',
85        wdSession    => 'WebDAO::Session',
86        wdSessionPar => undef,
87        wdEnginePar  => undef,
88        @_
89    );
90    my $env          = delete $defaults{__env}     || \%ENV;
91    my $need_preload = delete $defaults{__preload} || 0;
92
93    $defaults{wdSession} =
94         $env->{WD_SESSION}
95      || $env->{wdSession}
96      || $defaults{wdSession};
97    $defaults{wdEngine} =
98         $env->{WD_ENGINE}
99      || $env->{wdEngine}
100      || $defaults{wdEngine};
101
102    #init params
103    $defaults{wdEnginePar} =
104      WebDAO::Util::_parse_str_to_hash( $env->{WD_ENGINE_PAR}
105          || $env->{wdEnginePar} )
106      || {};
107    $defaults{wdSessionPar} =
108      WebDAO::Util::_parse_str_to_hash( $env->{WD_SESSION_PAR}
109          || $env->{wdSessionPar} )
110      || {};
111
112    if ($need_preload) {
113        for (qw/wdSession  wdEngine /) {
114            WebDAO::Util::load_module( $defaults{$_} );
115        }
116    }
117
118    \%defaults;
119
120}
121
122
123our %HTTPStatusCode = (
124    100 => 'Continue',
125    101 => 'Switching Protocols',
126    102 => 'Processing',                      # RFC 2518 (WebDAV)
127    200 => 'OK',
128    201 => 'Created',
129    202 => 'Accepted',
130    203 => 'Non-Authoritative Information',
131    204 => 'No Content',
132    205 => 'Reset Content',
133    206 => 'Partial Content',
134    207 => 'Multi-Status',                    # RFC 2518 (WebDAV)
135    300 => 'Multiple Choices',
136    301 => 'Moved Permanently',
137    302 => 'Found',
138    303 => 'See Other',
139    304 => 'Not Modified',
140    305 => 'Use Proxy',
141    307 => 'Temporary Redirect',
142    400 => 'Bad Request',
143    401 => 'Unauthorized',
144    402 => 'Payment Required',
145    403 => 'Forbidden',
146    404 => 'Not Found',
147    405 => 'Method Not Allowed',
148    406 => 'Not Acceptable',
149    407 => 'Proxy Authentication Required',
150    408 => 'Request Timeout',
151    409 => 'Conflict',
152    410 => 'Gone',
153    411 => 'Length Required',
154    412 => 'Precondition Failed',
155    413 => 'Request Entity Too Large',
156    414 => 'Request-URI Too Large',
157    415 => 'Unsupported Media Type',
158    416 => 'Request Range Not Satisfiable',
159    417 => 'Expectation Failed',
160    422 => 'Unprocessable Entity',            # RFC 2518 (WebDAV)
161    423 => 'Locked',                          # RFC 2518 (WebDAV)
162    424 => 'Failed Dependency',               # RFC 2518 (WebDAV)
163    425 => 'No code',                         # WebDAV Advanced Collections
164    426 => 'Upgrade Required',                # RFC 2817
165    449 => 'Retry with',                      # unofficial Microsoft
166    500 => 'Internal Server Error',
167    501 => 'Not Implemented',
168    502 => 'Bad Gateway',
169    503 => 'Service Unavailable',
170    504 => 'Gateway Timeout',
171    505 => 'HTTP Version Not Supported',
172    506 => 'Variant Also Negotiates',         # RFC 2295
173    507 => 'Insufficient Storage',            # RFC 2518 (WebDAV)
174    509 => 'Bandwidth Limit Exceeded',        # unofficial
175    510 => 'Not Extended',                    # RFC 2774
176);
177
1781;
179
180