blib/lib/ASP4/Server.pm

filter: Statementstmt 64.9% 37 / 57 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.4% 13 / 17 Podpod 80.0% 8 / 10 totaltotal 62.1% 59 / 95 CC 5 SCAR 11.7
CC
5
Cov
58%
Error
12.0
Mail
6.0
MapPath
2.0
CRAP 3.2
« blib/lib/ASP4/SessionStateManager.pm ↑ Summary blib/lib/ASP4/PageLoader.pm »
1  
2 package ASP4::Server;
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use ASP4::HTTPContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use ASP4::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Mail::Sendmail;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new
11 5145 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 5145 return bless { }, shift;
13 }# end new()
14  
15  
16 1 ▶sub context { ASP4::HTTPContext->current }
Subroutine context: called CC=1 SCAR=0.0
Pod undocumented
17  
18  
19 sub URLEncode
20 4 ▶{
Subroutine URLEncode: called CC=1 SCAR=0.0
Pod documented
21 4 ASP4::HTTPContext->current->cgi->escape( $_[1] );
22 }# end URLEncode()
23  
24  
25 sub URLDecode
26 0 ▶{
Subroutine URLDecode: not called CC=1 SCAR=6.9
Pod documented
27 0 ASP4::HTTPContext->current->cgi->unescape( $_[1] );
28 }# end URLDecode()
29  
30  
31 sub HTMLEncode
32 4 ▶{
Subroutine HTMLEncode: called CC=1 SCAR=0.0
Pod documented
33 4 my ($s, $str) = @_;
34 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 $str =~ s/&/&/g;
36 4 $str =~ s/</&lt;/g;
37 4 $str =~ s/>/&gt;/g;
38 4 $str =~ s/"/&quot;/g;
39 4 $str =~ s/'/&#39;/g;
40 4 return $str;
41 }# end HTMLEncode()
42  
43  
44 sub HTMLDecode
45 0 ▶{
Subroutine HTMLDecode: not called CC=1 SCAR=2.9
Pod documented
46 0 my ($s, $str) = @_;
47 15 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 0 $str =~ s/&lt;/</g;
49 0 $str =~ s/&gt;/>/g;
50 0 $str =~ s/&quot;/"/g;
51 0 $str =~ s/&amp;/&/g;
52 0 $str =~ s/&#39;/'/g;
53 0 return $str;
54 }# end HTMLDecode()
55  
56  
57 sub MapPath
58 12 ▶{
Subroutine MapPath: called CC=2 SCAR=7.1
Pod documented
59 12 my ($s, $path) = @_;
60
61 12 ▶ return unless defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 12 12
62
63 12 ASP4::HTTPContext->current->config->web->www_root . $path;
64 }# end MapPath()
65  
66  
67 sub Mail
68 0 ▶{
Subroutine Mail: not called CC=2 SCAR=17.9
Pod documented
69 0 my $s = shift;
70
71 0 Mail::Sendmail::sendmail( @_ );
72 0 ▶ die $Mail::Sendmail::error if $Mail::Sendmail::error;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Sendmail::error 0 0 0
73 0 return $Mail::Sendmail::log;
74 }# end Mail()
75  
76  
77 sub RegisterCleanup
78 1 ▶{
Subroutine RegisterCleanup: called CC=1 SCAR=0.0
Pod documented
79 1 my ($s, $sub, @args) = @_;
80
81 1 $s->context->r->pool->cleanup_register( $sub, \@args );
82 }# end RegisterCleanup()
83  
84  
85 sub Error
86 0 ▶{
Subroutine Error: not called CC=3 SCAR=24.8
Pod documented
87 0 my $s = shift;
88
89 0 ▶ my $error = ref($_[0]) && $_[0]->isa('ASP4::Error') ? $_[0] : ASP4::Error->new( @_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0]->isa('ASP4::Error') ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0]->isa("ASP4::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]->isa("ASP4::Error")
ref $_[0] $_[0]->isa("ASP4::Error")
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]->isa("ASP4::Error")
ABresult
0X0
100
111
90  
91 0 $s->context->stash->{error} = $error;
92 0 $s->context->config->load_class( $s->context->config->errors->error_handler );
93 0 my $error_handler = $s->context->config->errors->error_handler->new();
94 0 $error_handler->init_asp_objects( $s->context );
95 0 $error_handler->run( $s->context );
96 0 return $error;
97 }# end Error()
98  
99  
100 15 1;# return true:
101  
102 =pod
103  
104 =head1 NAME
105  
106 ASP4::Server - Utility Methods
107  
108 =head1 SYNOPSIS
109  
110 # Get the full disk path to /contact/form.asp:
111 $Server->MapPath("/contact/form.asp");
112
113 # Email someone:
114 $Server->Mail(
115 To => 'jim@bob.com',
116 From => 'Joe Jangles <joe@jangles.net>',
117 Subject => 'Test Email',
118 Message => "Hello There!",
119 );
120
121 # Avoid XSS:
122 <input type="text" name="foo" value="<%= $Server->HTMLEncode( $Form->{foo} ) %>" />
123
124 # Proper URLs:
125 <a href="foo.asp?bar=<%= $Server->URLEncode($Form->{bar}) %>">Click</a>
126  
127 =head1 DESCRIPTION
128  
129 The C<$Server> object provides some utility methods that don't really fit anywhere
130 else, but are still important.
131  
132 =head1 PUBLIC METHODS
133  
134 =head2 HTMLEncode( $str )
135  
136 Performs a simple string substitution to sanitize C<$str> for inclusion on HTML pages.
137  
138 Removes the threat of cross-site-scripting (XSS).
139  
140 Eg:
141  
142 <tag/>
143  
144 Becomes:
145  
146 &lt;tag/&gt;
147  
148 =head2 HTMLDecode( $str )
149  
150 Does exactly the reverse of HTMLEncode.
151  
152 Eg:
153  
154 &lt;tag/&gt;
155  
156 Becomes:
157  
158 <tag/>
159  
160 =head2 URLEncode( $str )
161  
162 Converts a string for use within a URL.
163  
164 eg:
165  
166 test@test.com
167  
168 becomes:
169  
170 test%40test.com
171  
172 =head2 URLDecode( $str )
173  
174 Converts a url-encoded string to a normal string.
175  
176 eg:
177  
178 test%40test.com
179  
180 becomes:
181  
182 test@test.com
183  
184 =head2 MapPath( $file )
185  
186 Converts a relative path to a full disk path.
187  
188 eg:
189  
190 /contact/form.asp
191  
192 becomes:
193  
194 /var/www/mysite.com/htdocs/contact/form.asp
195  
196 =head2 Mail( %args )
197  
198 Sends email - uses L<Mail::Sendmail>'s C<sendmail(...)> function.
199  
200 =head2 RegisterCleanup( \&code, @args )
201  
202 The supplied coderef will be executed with its arguments as the request enters
203 its Cleanup phase.
204  
205 See L<http://perl.apache.org/docs/2.0/user/handlers/http.html#PerlCleanupHandler> for details.
206  
207 =head2 Error( [%args] )
208  
209 Calling C<<$Server->Error()>> without arguments will use the value of C<$@> and
210 generate a L<ASP4::Error> object from it, then pass it to the C<run(...)> method
211 of your C<<$Config->errors->error_handler>> for processing.
212  
213 Please take a look at the documentation for L<ASP4::Error>, L<ASP4::ErrorHandler>
214 and L<ASP4::ErrorHandler::Remote> for details on how errors are handled.
215  
216 =head1 BUGS
217  
218 It's possible that some bugs have found their way into this release.
219  
220 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
221  
222 =head1 HOMEPAGE
223  
224 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
225 of ASP4 in action.
226  
227 =cut
228  
« blib/lib/ASP4/SessionStateManager.pm ↑ Summary blib/lib/ASP4/PageLoader.pm »