blib/lib/ASP.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 240 CC 19 SCAR 28.9
CC
19
Cov
0%
param
72.0
BEGIN
12.0
escapeHTML
12.0
CRAP 18.0
1 #####################################################################
2 #
3 # ASP - Facilitate integration of PerlScript with ASP
4 #
5 # Author: Tim Hammerquist
6 # Revision: 1.07
7 # NOTES: based on Matt Sergeant's Win32-ASP module.
8 #
9 #####################################################################
10 #
11 # Copyright 2000 Tim Hammerquist. All rights reserved.
12 #
13 # This file is distributed under the Artistic License.
14 # See http://www.perl.com/language/misc/Artistic.html or
15 # the license that comes with your perl distribution.
16 #
17 # Contact me at cafall@voffice.net with any comments,
18 # flames, queries, suggestions, or general curiosity.
19 #
20 #####################################################################
21  
22 require 5.005;
23 use strict;
24  
25 my ($APACHE, $WIN32);
26 $APACHE = $Apache::ASP::VERSION;
27 $WIN32 = $^O =~ /win/i;
28  
29 package ASP::IO;
30 sub TIEHANDLE { shift->new(@_) }
31 sub PRINT { shift->print(@_) }
32 sub PRINTF { shift->print(sprintf(@_)) }
33 sub new { bless {}, shift; }
34 sub print {
35 my $self = shift;
36 ASP::Print(@_);
37 1;
38 }
39  
40 1;
41  
42 package ASP;
43 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $ASPOUT);
44  
45 require CGI;
46  
47 BEGIN {
48 require Exporter;
49  
50 use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS
51 $Application $ObjectContext $Request $Response
52 $Server $Session $ScriptingNamespace @DeathHooks
53 );
54  
55 @ISA = qw( Exporter );
56 %EXPORT_TAGS = (
57 basic => [qw(
58 Print Warn die exit param param_count
59 )],
60 strict => [qw(
61 Print Warn die exit param param_count
62 $Application $ObjectContext $Request
63 $Response $Server $Session
64 $ScriptingNamespace
65 )],
66 all => [qw(
67 Print Warn die exit param param_count
68 $Application $ObjectContext $Request
69 $Response $Server $Session
70 $ScriptingNamespace
71 DebugPrint HTMLPrint
72 escape unescape escapeHTML unescapeHTML
73 )],
74 );
75 Exporter::export_tags('basic');
76 Exporter::export_ok_tags('all');
77  
78 $Application = $main::Application;
79 $ObjectContext = $main::ObjectContext;
80 $Request = $main::Request;
81 $Response = $main::Response;
82 $Server = $main::Server;
83 $Session = $main::Session;
84 $ScriptingNamespace = $main::ScriptingNamespace unless $APACHE;
85  
86 if ($WIN32) {
87 %ENV = ();
88 for (Win32::OLE::in $Request->ServerVariables) {
89 $ENV{$_} = $Request->ServerVariables($_)->Item;
90 }
91 }
92 }
93  
94 $VERSION='1.07';
95  
96 $ASPOUT = tie *RESPONSE_FH, 'ASP::IO';
97 select RESPONSE_FH unless $APACHE;
98 $SIG{__WARN__} = sub { ASP::Print(@_) };
99  
100 sub _END { &$_() for @DeathHooks; @DeathHooks = (); 1; }
101  
102 =head1 NAME
103  
104 ASP - a Module for ASP (PerlScript) Programming
105  
106 =head1 SYNOPSIS
107  
108 use strict;
109 use ASP qw(:strict);
110  
111 print "Testing, testing.<BR><BR>";
112 my $item = param('item');
113  
114 if($item eq 'Select one...') {
115 die "Please select a value from the list.";
116 }
117  
118 print "You selected $item.";
119 exit;
120  
121 =head1 DESCRIPTION
122  
123 This module is based on Matt Sergeant's excellent
124 Win32::ASP module, which can be found at
125 E<lt>F<http://www.fastnetltd.ndirect.co.uk/Perl>E<gt>.
126 After using Mr. Sergeant's module, I took on the task of
127 customizing and optimizing it for my own purposes. Feel
128 free to use it if you find it useful.
129  
130 =head1 NOTES
131  
132 This module is designed to work with both ASP PerlScript on IIS4,
133 as well as mod_perl/Apache::ASP on *nix platforms. Apache::ASP
134 already provides some of the functionality provided by this module;
135 because of this (and to avoid redundancy), ASP.pm attempts to detect
136 its environment. Differences between Apache and MS ASP are noted.
137  
138 Both of the print() and warn() standard perl funcs are overloaded
139 to output to the browser. print() is also available via the
140 $ASP::ASPOUT->print() method call.
141  
142 $Request->ServerVariables are only stuffed into %ENV on Win32
143 platforms, as Apache::ASP already provides this.
144  
145 ASP.pm also exports the $ScriptingNamespace symbol (Win32 only).
146 This symbol allows PerlScript to call subs/functions written in
147 another script language. For example:
148  
149 <%@ language=PerlScript %>
150 <%
151 use ASP qw(:strict);
152 print $ScriptingNamespace->SomeSub("arg1");
153 %>
154 <SCRIPT language=VBScript runat=server>
155 Function SomeSub (str)
156 SomeSub = SomethingThatReturnsSomething()
157 End Function
158 </SCRIPT>
159  
160 =head1 USE
161  
162 =head2 use ASP qw(:basic);
163  
164 Exports basic subs: Print, Warn, die, exit, param, param_count. Same
165 as C<use ASP;>
166  
167 =head2 use ASP qw(:strict);
168  
169 Allows the use of the ASP objects under C<use strict;>.
170  
171 NOTE: This is not the only way to accomplish this, but I think it's
172 the cleanest, most convenient way.
173  
174 =head2 use ASP qw(:all);
175  
176 Exports all subs except those marked 'not exported'.
177  
178 =head2 use ASP ();
179  
180 Overloads print() and warn() and provides the $ASP::ASPOUT object.
181  
182 =head1 FUNCTION REFERENCE
183  
184 =head2 warn LIST
185  
186 C<warn> (or more specifically, the __WARN__ signal) has been re-routed to
187 output to the browser.
188  
189 FYI: When implemented, this tweak led to the removal of the prototypes
190 Matt placed on his subs.
191  
192 =head2 Warn LIST
193  
194 C<Warn> is an alias for the ASP::Print method described below. The
195 overloading of C<warn> as described above does not currently work
196 in Apache::ASP, so this is provided.
197  
198 =cut
199 sub Warn { ASP::Print(@_); }
200  
201 =head2 print LIST
202  
203 C<print> is overloaded to write to the browser by default. The inherent
204 behavior of print has not been altered and you can still use an alternate
205 filehandle as you normally would. This allows you to use print just
206 as you would in CGI scripts. The following statement would need no
207 modification between CGI and ASP PerlScript:
208  
209 print param('URL'), " was requested by ", $ENV{REMOTE_HOST}, "\n";
210  
211 =head2 Print LIST
212  
213 Prints a string or comma separated list of strings to the browser. Use
214 as if you were using C<print> in a CGI application. Print gets around ASP's
215 limitations of 128k in a single $Response->Write() call.
216  
217 NB: C<print> calls Print, so you could use either, but
218 print more closely resembles perl.
219  
220 =cut
221 sub Print {
222 for (@_) {
223 if ( length($_) > 128000 ) {
224 ASP::Print( unpack('a128000a*', $_) );
225 } else {
226 $main::Response->Write($_);
227 }
228 }
229 }
230  
231 =head2 DebugPrint LIST
232  
233 Output is displayed between HTML comments so the output doesn't
234 interfere with page aesthetics.
235  
236 =cut
237 sub DebugPrint { ASP::Print("<!--\n", @_, "\n-->"); }
238  
239 =head2 HTMLPrint LIST
240  
241 The same as C<Print> except the output is HTML-encoded so that
242 any HTML tags appear as sent, i.e. E<lt> becomes &lt;, E<gt> becomes &gt; etc.
243  
244 =cut
245 sub HTMLPrint { map { ASP::Print($main::Server->HTMLEncode($_)) } @_ ; }
246  
247 =head2 die LIST
248  
249 Prints the contents of LIST to the browser and then exits. die
250 automatically calls $Response->End for you, it also executes any
251 cleanup code you have added with C<AddDeathHook>.
252  
253 =cut
254 sub die {
255 ASP::Print(@_, "</BODY></HTML>");
256 _END;
257 $main::Response->End();
258 CORE::die();
259 }
260  
261 =head2 exit
262  
263 Exits the current script. $Response->End is called automatically for you.
264 Any cleanup code added with C<AddDeathHook> is also called.
265  
266 =cut
267 sub exit {
268 _END;
269 $main::Response->End();
270 CORE::exit();
271 }
272  
273 =head2 escape LIST
274  
275 Escapes (URL-encodes) a list. Uses ASP object method
276 $Server->URLEncode().
277  
278 =cut
279 sub escape { map { $main::Server->URLEncode($_) } @_; }
280  
281 =head2 unescape LIST
282  
283 Unescapes a URL-encoded list. Algorithms ripped from CGI.pm
284 method of the same name.
285  
286 =cut
287 sub unescape {
288 map {
289 tr/+/ /;
290 s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
291 } @_;
292 }
293  
294 =head2 escapeHTML LIST
295  
296 Escapes a list of HTML. Uses ASP object method $Server->HTMLEncode().
297  
298 If passed an array reference, escapeHTML will return a reference
299 to the escaped array.
300  
301 =cut
302 sub escapeHTML {
303 my ($flag, @args) = (0, @_);
304 @args = @{$args[0]} and $flag++ if ref $args[0] eq "ARRAY";
305 $_ = $main::Server->HTMLEncode($_) for @args;
306 $flag ? \@args : @args;
307 }
308  
309 =head2 unescapeHTML LIST
310  
311 Unescapes an HTML-encoded list.
312  
313 If passed an array reference, unescapeHTML will return a reference
314 to the un-escaped array.
315  
316 =cut
317 sub unescapeHTML {
318 my ($flag, @args) = (0, @_);
319 @args = @{$args[0]} and $flag++ if ref $args[0] eq "ARRAY";
320 map {
321 s/&amp;/&/gi;
322 s/&quot;/"/gi;
323 s/&nbsp;/ /gi;
324 s/&gt;/>/gi;
325 s/&lt;/</gi;
326 s/&#(\d+);/chr($1)/ge;
327 s/&#x([0-9a-f]+);/chr(hex($1))/gi;
328 } @args;
329 $flag ? \@args : @args;
330 }
331  
332 =head2 param EXPR [, EXPR]
333  
334 Simplifies parameter access and makes switch from GET to POST transparent.
335  
336 Given the following querystring:
337  
338 myscript.asp?x=a&x=b&y=c
339  
340 param() returns ('x', 'y')
341 param('y') returns 'c'
342 param('x') returns ('a', 'b')
343 param('x',1) returns 'a'
344 param('x',2) returns 'b'
345  
346 NOTE: Under Apache::ASP, param() simply passes the arguments
347 to CGI::param() because Apache::ASP doesn't support the $obj->{Count}
348 property used in this function.
349  
350 =cut
351 sub param {
352 if ($APACHE) {
353 return (wantarray) ? (CGI::param(@_)) : scalar(CGI::param(@_));
354 }
355 unless (@_) {
356 my @keys;
357 push( @keys, $_ ) for ( Win32::OLE::in $main::Request->QueryString );
358 push( @keys, $_ ) for ( Win32::OLE::in $main::Request->Form );
359 return @keys;
360 }
361 $_[1] = 1 unless defined $_[1];
362 unless (wantarray) {
363 if ($main::Request->ServerVariables('REQUEST_METHOD')->Item eq 'GET') {
364 return $main::Request->QueryString($_[0])->Item($_[1]);
365 } else {
366 return $main::Request->Form($_[0])->Item($_[1]);
367 }
368 } else {
369 my ($i, @ret);
370 if ($main::Request->ServerVariables('REQUEST_METHOD')->Item eq 'GET') {
371 my $count = $main::Request->QueryString($_[0])->{Count};
372 for ($i = 1; $i <= $count; $i++ ) {
373 push @ret, $main::Request->QueryString($_[0])->Item($i);
374 }
375 } else {
376 my $count = $main::Request->Form($_[0])->{Count};
377 for ($i = 1; $i <= $count; $i++) {
378 push @ret, $main::Request->Form($_[0])->Item($i);
379 }
380 }
381 return @ret;
382 }
383 }
384  
385 =head2 param_count EXPR
386  
387 Returns the number of times EXPR appears in the request (Form or
388 QueryString).
389  
390 For example, if URL is
391  
392 myscript.asp?x=a&x=b&y=c
393  
394 then
395  
396 param_count('x');
397  
398 returns 2.
399  
400 NOTE: Under Apache::ASP, param_count() performs some manipulation
401 using CGI::param() because Apache::ASP doesn't support the
402 $obj->{Count} property used in this function.
403  
404
405  
406 =cut
407 sub param_count {
408 if ($APACHE) {
409 return scalar( @{[ CGI::param($_[0]) ]} );
410 }
411 if ($main::Request->ServerVariables('REQUEST_METHOD')->Item eq 'GET') {
412 return $main::Request->QueryString($_[0])->{Count};
413 } else {
414 return $main::Request->Form($_[0])->{Count};
415 }
416 }
417  
418 =head2 AddDeathHook LIST
419  
420 Allows cleanup code to be executed when you C<die> or C<exit>.
421 Useful for closing database connections in the event of a
422 fatal error.
423  
424 <%
425 my $conn = Win32::OLE-new('ADODB.Connection');
426 $conn->Open("MyDSN");
427 $conn->BeginTrans();
428 ASP::AddDeathHook( sub { $Conn->Close if $Conn; } );
429 %>
430  
431 Death hooks are not executed except by explicitly calling the die() or exit()
432 methods provided by ASP.pm.
433  
434 AddDeathHook is not exported.
435  
436 =cut
437 sub AddDeathHook { push @DeathHooks, @_; }
438  
439 # These two functions are ripped from CGI.pm
440 sub expire_calc {
441 my($time) = @_;
442 my(%mult) = ('s'=>1,
443 'm'=>60,
444 'h'=>60*60,
445 'd'=>60*60*24,
446 'M'=>60*60*24*30,
447 'y'=>60*60*24*365);
448 # format for time can be in any of the forms...
449 # "now" -- expire immediately
450 # "+180s" -- in 180 seconds
451 # "+2m" -- in 2 minutes
452 # "+12h" -- in 12 hours
453 # "+1d" -- in 1 day
454 # "+3M" -- in 3 months
455 # "+2y" -- in 2 years
456 # "-3m" -- 3 minutes ago(!)
457 # If you don't supply one of these forms, we assume you are
458 # specifying the date yourself
459 my $offset;
460 if ( !$time || $time eq 'now' ) {
461 $offset = 0;
462 } elsif ( $time =~ /^([+-]?\d+)([mhdMy]?)/ ) {
463 $offset = ($mult{$2} || 1)*$1;
464 } else {
465 return $time;
466 }
467 return ($time + $offset);
468 }
469  
470 =head1 AUTHOR
471  
472 Tim Hammerquist E<lt>F<tim@dichosoft.com>E<gt>
473  
474 =head1 HISTORY
475  
476 =over 4
477  
478 =item Version 1.07
479  
480 Added Warn() because warn() overloading doesn't appear to work
481 under Apache::ASP.
482  
483 Was forced to clear @DeathHooks array after calling _END() because
484 of the persistent state of Apache::ASP holding over contents across
485 executions.
486  
487 Removed BinaryWrite(), SetCookie(), and Autoload functionality.
488  
489 =item Version 1.00
490  
491 The escapeHTML() and unescapeHTML() functions now accept array refs as well
492 as lists, as Win32::ASP::HTMLEncode() was supposed to.
493 Thanks to Matt Sergeant for the fix.
494  
495 =item Version 0.97
496  
497 Optimized and debugged.
498  
499 =item Version 0.77
500  
501 Overloaded warn() and subsequently removed prototypes.
502  
503 Exported $ScriptingNamespace object.
504  
505 Added methods escape(), unescape(), escapeHTML(), unescapeHTML().
506 Thanks to Bill Odom for pointing these out!
507  
508 Re-implemented SetCookie and BinaryWrite functions.
509  
510 =item Version 0.11
511  
512 Optimized and debugged.
513  
514 =back
515  
516 =head1 SEE ALSO
517  
518 ASP::NextLink(3)
519  
520 =cut
521 1;
522 __END__