File Coverage

blib/lib/Apache2/ASP/Server.pm
Criterion Covered Total %
statement 24 66 36.3
branch 0 6 0.0
condition n/a
subroutine 8 20 40.0
pod 7 10 70.0
total 39 102 38.2


line stmt bran cond sub pod time code
1              
2             package Apache2::ASP::Server;
3              
4 23     23   80 use strict;
  23         29  
  23         590  
5 23     23   76 use warnings 'all';
  23         276  
  23         641  
6 23     23   12138 use Mail::Sendmail;
  23         203534  
  23         2422  
7 23     23   11041 use encoding 'utf8';
  23         231285  
  23         109  
8              
9              
10             #==============================================================================
11             sub new
12             {
13 0     0 0   my ($class, %args) = @_;
14              
15 0           my $s = bless {LastError => undef}, $class;
16            
17 0           return $s;
18             }# end new()
19              
20              
21             #==============================================================================
22             sub GetLastError
23             {
24 0     0 0   $_[0]->{LastError};
25             }# end GetLastError()
26              
27              
28             #==============================================================================
29             sub context
30             {
31 0     0 0   $Apache2::ASP::HTTPContext::ClassName->current;
32             }# end context()
33              
34              
35             #==============================================================================
36             # Shamelessly ripped off from Apache::ASP::Server, by Joshua Chamas,
37             # who shamelessly ripped it off from CGI.pm, by Lincoln D. Stein.
38             # :)
39             sub URLEncode
40             {
41 0     0 1   my $toencode = $_[1];
42 23     23   13840 no warnings 'uninitialized';
  23         43  
  23         618  
43 0           $toencode =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/esg;
  0            
44 0           $toencode;
45             }# end URLEncode()
46              
47              
48             #==============================================================================
49             sub URLDecode
50             {
51 0     0 1   my ($s, $todecode) = @_;
52 0 0         return unless defined($todecode);
53 23     23   17463 $todecode =~ tr/+/ /; # pluses become spaces
  23         194  
  23         305  
  0            
54 0           $todecode =~ s/%(?:([0-9a-fA-F]{2})|u([0-9a-fA-F]{4}))/
55 0 0         defined($1)? chr hex($1) : _utf8_chr(hex($2))/ge;
56 0           return $todecode;
57             }# end URLDecode()
58              
59              
60             #==============================================================================
61             sub HTMLEncode
62             {
63 0     0 1   my ($s, $str) = @_;
64 23     23   97914 no warnings 'uninitialized';
  23         39  
  23         622  
65 0           $str =~ s/&/&/g;
66 0           $str =~ s/
67 0           $str =~ s/>/>/g;
68 0           $str =~ s/"/"/g;
69 0           $str =~ s/'/'/g;
70 0           return $str;
71             }# end HTMLEncode()
72              
73              
74             #==============================================================================
75             sub HTMLDecode
76             {
77 0     0 1   my ($s, $str) = @_;
78 23     23   7305 no warnings 'uninitialized';
  23         42  
  23         612  
79 0           $str =~ s/</
80 0           $str =~ s/>/>/g;
81 0           $str =~ s/"/"/g;
82 0           $str =~ s/&/&/g;
83 0           return $str;
84             }# end HTMLEncode()
85              
86              
87             #==============================================================================
88             sub MapPath
89             {
90 0     0 1   my ($s, $path) = @_;
91            
92 0 0         return unless defined($path);
93            
94 0           $s->context->config->web->www_root . $path;
95             }# end MapPath()
96              
97              
98             #==============================================================================
99             sub Mail
100             {
101 0     0 1   my ($s, %args) = @_;
102            
103             # XXX: Base64-encode the content, and update the content-type to reflect that
104             # if content-type === 'text/html'.
105             # XXX: Consider updating this so that we can send attachments as well.
106 0           Mail::Sendmail::sendmail( %args );
107             }# end Mail()
108              
109              
110             #==============================================================================
111             sub RegisterCleanup
112             {
113 0     0 1   my ($s, $sub, @args) = @_;
114            
115             # This works both in "testing" mode and within a live mod_perl environment.
116 0           $s->context->get_prop('r')->pool->cleanup_register( $sub, \@args );
117             }# end RegisterCleanup()
118              
119              
120             #==============================================================================
121             sub _utf8_chr
122             {
123 0     0     my ($c) = @_;
124 0           require utf8;
125 0           my $u = chr($c);
126 0           utf8::encode($u); # drop utf8 flag
127 0           return $u;
128             }# end _utf8_chr()
129              
130              
131             #==============================================================================
132             sub DESTROY
133             {
134 0     0     my $s = shift;
135            
136 0           undef(%$s);
137             }# end DESTROY()
138              
139             1;# return true:
140              
141              
142             =pod
143              
144             =head1 NAME
145              
146             Apache2::ASP::Server - Utility methods for Apache2::ASP
147              
148             =head1 SYNOPSIS
149              
150             my $full_path = $Server->MapPath('/index.asp');
151            
152             $Server->URLEncode( 'user@email.com' );
153              
154             $Server->URLDecode( 'user%40email.com' );
155            
156             $Server->HTMLEncode( '
' );
157            
158             $Server->HTMLDecode( '<br />' );
159            
160             $Server->Mail(
161             To => 'user@email.com',
162             From => '"Friendly Name" ',
163             Subject => 'Hello World',
164             Message => "E Pluribus Unum.\n"x777
165             );
166            
167             $Server->RegisterCleanup( sub {
168             my @args = @_;
169             ...
170             }, @args
171             );
172              
173             =head1 DESCRIPTION
174              
175             The ASP Server object is historically a wrapper for a few utility functions that
176             don't belong anywhere else.
177              
178             Keeping with that tradition, the Apache2::ASP Server object is a collection of
179             functions that don't belong anywhere else.
180              
181             =head1 PUBLIC METHODS
182              
183             =head2 URLEncode( $str )
184              
185             Converts a string into its url-encoded equivalent. This approximates to
186             JavaScript's C function or L's C function.
187              
188             Example:
189              
190             <%= $Server->URLEncode( 'user@email.com' ) %>
191              
192             Returns
193              
194             user%40email.com
195              
196             =head2 URLDecode( $str )
197              
198             Converts a url-encoded string into its non-url-encoded equivalent. This works
199             the same way as JavaScript's and L's C function.
200              
201             Example:
202              
203             <%= $Server->URLDecode( 'user%40email.com' ) %>
204              
205             Returns
206              
207             user@email.com
208              
209             =head2 HTMLEncode( $str )
210              
211             Safely converts <, > and & into C<<>, C<>> and C<&>, respectively.
212              
213             =head2 HTMLDecode( $str )
214              
215             Converts C<<>, C<>> and C<&> into <, > and &, respectively.
216              
217             =head2 MapPath( $relative_path )
218              
219             Given a relative path, C will return the absolute path for it, under the
220             document root of the current website.
221              
222             For example, C might return C
223              
224             =head2 Mail( %args )
225              
226             Sends an email message. The following arguments are required:
227              
228             =over 4
229              
230             =item To
231              
232             The email address the message should be sent to.
233              
234             =item From
235              
236             The email address the message should be sent from.
237              
238             =item Subject
239              
240             The subject of the email.
241              
242             =item Message
243              
244             The content of the body.
245              
246             =back
247              
248             Other arguments are passed through to L.
249              
250             =head2 RegisterCleanup( \&code[, @args ] )
251              
252             A wrapper around L's C function. Pass in a coderef
253             and (optionally) arguments to be passed to that coderef, and it is executed during
254             the cleanup phase of the current request.
255              
256             If we were doing vanilla mod_perl, you could achieve the same effect with this:
257              
258             $r->pool->cleanup_register( sub { ... }, \@args );
259              
260             =head1 BUGS
261            
262             It's possible that some bugs have found their way into this release.
263            
264             Use RT L to submit bug reports.
265            
266             =head1 HOMEPAGE
267            
268             Please visit the Apache2::ASP homepage at L to see examples
269             of Apache2::ASP in action.
270              
271             =head1 AUTHOR
272              
273             John Drago
274              
275             =head1 COPYRIGHT
276              
277             Copyright 2008 John Drago. All rights reserved.
278              
279             =head1 LICENSE
280              
281             This software is Free software and is licensed under the same terms as perl itself.
282              
283             =cut
284