ASP/Response.pm

filter: Statementstmt 23.2% 30 / 129 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 30.0% 9 / 30 Podpod 90.0% 18 / 20 totaltotal 25.0% 57 / 228 CC 19 SCAR 22.9
CC
19
Cov
18%
AddCookie
42.0
ContentType
12.0
Status
12.0
CRAP 9.9
« ASP/HTTPContext/HandlerResolver.pm ↑ Summary ASP/ConfigNode.pm »
1  
2 package Apache2::ASP::Response;
3  
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use HTTP::Date qw( time2iso str2time time2str );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Carp qw( croak confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Apache2::ASP::Mock::RequestRec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Apache2::ASP::HTTPContext::SubContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 our $MAX_BUFFER_LENGTH = 1024 ** 2;
12  
13 #$SIG{__DIE__} = \&confess;
14 23 our $IS_TRAPINCLUDE = 0;
15  
16 #==============================================================================
17 sub new
18 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my ($class, %args) = @_;
20
21 0 delete($args{context});
22 0 my $s = bless {
23 _status => 200,
24 _output_buffer => [ ],
25 _do_buffer => 1,
26 _buffer_length => 0,
27 }, $class;
28 0 $s->ContentType('text/html');
29 0 $s->Expires( 0 );
30 0 return $s;
31 }# end new()
32  
33  
34 #==============================================================================
35 sub context
36 0 ▶{
Subroutine context: not called CC=1 SCAR=6.9
Pod undocumented
37 0 Apache2::ASP::HTTPContext->current;
38 }# end context()
39  
40  
41 #==============================================================================
42 sub ContentType
43 0 ▶{
Subroutine ContentType: not called CC=3 SCAR=24.8
Pod documented
44 0 my $s = shift;
45
46 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
47 {
48 # confess "Response.ContentType cannot be changed after headers have been sent"
49 return
50 0 ▶ if $s->context->{_did_send_headers};
Branch: 1 branch
BranchTrueFalseTotal
if $s->context->{"_did_send_headers"} 0 0 0
51 0 $s->context->content_type( shift );
52 }
53 else
54 {
55 0 return $s->context->content_type;
56 }# end if()
57 }# end ContentType()
58  
59  
60 #==============================================================================
61 sub Status
62 0 ▶{
Subroutine Status: not called CC=3 SCAR=24.8
Pod documented
63 0 my $s = shift;
64
65 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
66 {
67 # confess "Response.Status cannot be changed after headers have been sent"
68 return
69 0 ▶ if $s->context->{_did_send_headers};
Branch: 1 branch
BranchTrueFalseTotal
if $s->context->{"_did_send_headers"} 0 0 0
70
71 0 $s->{_status} = shift;
72 0 $s->context->r->status( $s->{_status} );
73 }
74 else
75 {
76 0 return $s->{_status};
77 }# end if()
78 }# end Status()
79  
80  
81 #==============================================================================
82 sub Expires
83 0 ▶{
Subroutine Expires: not called CC=2 SCAR=17.9
Pod documented
84 0 my $s = shift;
85
86 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
87 {
88 # Setter:
89 0 $s->{_expires} = shift;
90 0 $s->ExpiresAbsolute( time2str(time + $s->{_expires} * 60 ) );
91 }
92 else
93 {
94 # Getter:
95 0 return $s->{_expires};
96 }# end if()
97 }# end Expires()
98  
99  
100 #==============================================================================
101 sub ExpiresAbsolute
102 0 ▶{
Subroutine ExpiresAbsolute: not called CC=2 SCAR=17.9
Pod documented
103 0 my $s = shift;
104 0 ▶ if( my $when = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $when = shift()) { } 0 0 0
105 {
106 0 $s->DeleteHeader('expires');
107 0 $s->{_expires_absolute} = $when;
108 }
109 else
110 {
111 0 return $s->{_expires_absolute};
112 }# end if()
113 }# end ExpiresAbsolute()
114  
115  
116 #==============================================================================
117 sub Declined
118 0 ▶{
Subroutine Declined: not called CC=1 SCAR=6.9
Pod documented
119 0 return -1;
120 }# end Declined()
121  
122  
123 #==============================================================================
124 sub Redirect
125 0 ▶{
Subroutine Redirect: not called CC=2 SCAR=17.9
Pod documented
126 0 my ($s, $url) = @_;
127
128 confess "Response.Redirect cannot be called after headers have been sent"
129 0 ▶ if $s->context->{_did_send_headers};
Branch: 1 branch
BranchTrueFalseTotal
if $s->context->{"_did_send_headers"} 0 0 0
130
131 0 $s->Clear;
132 0 $s->AddHeader( location => $url );
133 0 $s->Status( 302 );
134 0 $s->End;
135 0 return 302; # New behavior - used to return '1':
136 }# end Redirect()
137  
138  
139 #==============================================================================
140 sub End
141 0 ▶{
Subroutine End: not called CC=1 SCAR=6.9
Pod documented
142 0 my $s = shift;
143
144 0 $s->Flush;
145 0 $s->context->set_prop( did_end => 1 );
146 }# end End()
147  
148  
149 #==============================================================================
150 sub Flush
151 0 ▶{
Subroutine Flush: not called CC=1 SCAR=6.9
Pod documented
152 0 my $s = shift;
153
154 0 $s->context->rflush;
155 }# end Flush()
156  
157  
158 #==============================================================================
159 sub Write
160 0 ▶{
Subroutine Write: not called CC=2 SCAR=17.9
Pod documented
161 0 my $s = shift;
162 0 ▶ return unless defined($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
163  
164 0 $s->context->print( shift );
165 }# end Write()
166  
167  
168 #==============================================================================
169 sub Include
170 0 ▶{
Subroutine Include: not called CC=3 SCAR=22.1
Pod documented
171 0 my ($s, $path, $args) = @_;
172 0 ▶ return if $s->context->{did_end};
Branch: 1 branch
BranchTrueFalseTotal
if $s->context->{"did_end"} 0 0 0
173
174 0 my $ctx = $s->context;
175 0 my $subcontext = Apache2::ASP::HTTPContext::SubContext->new( parent => $ctx );
176
177 0 my $root = $s->context->config->web->www_root;
178 0 $path =~ s@^\Q$root\E@@;
179 0 local $ENV{REQUEST_URI} = $path;
180 0 local $ENV{SCRIPT_FILENAME} = $ctx->server->MapPath( $path );
181 0 local $ENV{SCRIPT_NAME} = $path;
182
183 23 ▶ use Apache2::ASP::Mock::RequestRec;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 0 my $clone_r = Apache2::ASP::Mock::RequestRec->new( );
185 0 $clone_r->uri( $path );
186 0 $subcontext->setup_request( $clone_r, $ctx->cgi );
187 0 my $res = $subcontext->execute( $args );
188 0 $ctx->print( $subcontext->{r}->{buffer} );
189 0 $subcontext->DESTROY;
190  
191 0 ▶ if( $res > 200 )
Branch: 1 branch
BranchTrueFalseTotal
if ($res > 200) 0 0 0
192 {
193 0 $s->Status( $res );
194 }# end if()
195  
196 0 undef( $subcontext );
197 }# end Include()
198  
199  
200 #==============================================================================
201 sub TrapInclude
202 0 ▶{
Subroutine TrapInclude: not called CC=2 SCAR=13.2
Pod documented
203 0 my ($s, $path, $args) = @_;
204 0 ▶ return if $s->context->{did_end};
Branch: 1 branch
BranchTrueFalseTotal
if $s->context->{"did_end"} 0 0 0
205
206 23 ▶ use Apache2::ASP::HTTPContext::SubContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
207
208 0 my $ctx = $s->context;
209 0 my $subcontext = Apache2::ASP::HTTPContext::SubContext->new( parent => $ctx );
210
211 0 my $root = $s->context->config->web->www_root;
212 0 $path =~ s@^\Q$root\E@@;
213 0 local $ENV{REQUEST_URI} = $path;
214 0 local $ENV{SCRIPT_FILENAME} = $ctx->server->MapPath( $path );
215 0 local $ENV{SCRIPT_NAME} = $path;
216
217 23 ▶ use Apache2::ASP::Mock::RequestRec;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 0 my $clone_r = Apache2::ASP::Mock::RequestRec->new( );
219 0 $clone_r->uri( $path );
220 0 $subcontext->setup_request( $clone_r, $ctx->cgi );
221 0 my $res = $subcontext->execute( $args );
222 0 my $result = $subcontext->{r}->{buffer};
223 0 $subcontext->DESTROY;
224  
225 0 undef( $subcontext );
226 0 return $result;
227 }# end TrapInclude()
228  
229  
230 #==============================================================================
231 sub Cookies
232 0 ▶{
Subroutine Cookies: not called CC=1 SCAR=6.9
Pod documented
233 0 $_[0]->context->headers_out->{'set-cookie'};
234 }# end Cookies()
235  
236  
237 #==============================================================================
238 sub AddCookie
239 0 ▶{
Subroutine AddCookie: not called CC=6 SCAR=37.4
Pod documented
240 0 my $s = shift;
241
242 0 my ($name, $val, $path, $expires) = @_;
243 0 ▶ die "Usage: Response.AddCookie(name, value [, path [, expires ]])"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $val 0 0 0
Condition0%0/3
defined $name and defined $val
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and defined $val
defined $name defined $val
Truth table0%0/3
A and B
A= defined $nameB= defined $val
ABresult
0X0
100
111
244 unless defined($name) && defined($val);
245 0 ▶ $path ||= '/';
Condition0%0/2
$path ||= "/"
l!l
00
MC/DC0%0/1
$path ||= "/"
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
246 0 ▶ $expires ||= time() + ( 60 * 30 );
Condition0%0/3
$expires ||= time + 1800
l!l&&r!l&&!r
000
MC/DC0%0/2
$expires ||= time + 1800
$expires time + 1800
Truth table0%0/3
A ||= B
A= $expiresB= time + 1800
ABresult
1X1
011
000
247 0 ▶ my $expire_date ||= time2str( $expires );
Condition0%0/3
my $expire_date ||= &time2str($expires)
l!l&&r!l&&!r
000
MC/DC0%0/2
my $expire_date ||= &time2str($expires)
my $expire_date &time2str($expires)
Truth table0%0/3
A ||= B
A= my $expire_dateB= &time2str($expires)
ABresult
1X1
011
000
248
249 0 my $cookie = join '=', map { $s->context->cgi->escape( $_ ) } ( $name => $val );
250 0 $s->context->headers_out->push_header( 'set-cookie' => "$cookie; path=$path; expires=$expire_date" );
251 }# end AddCookie()
252  
253  
254 #==============================================================================
255 sub AddHeader
256 0 ▶{
Subroutine AddHeader: not called CC=3 SCAR=24.8
Pod documented
257 0 my ($s, $name, $val) = @_;
258
259 0 ▶ return unless defined($name) && defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $val 0 0 0
Condition0%0/3
defined $name and defined $val
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and defined $val
defined $name defined $val
Truth table0%0/3
A and B
A= defined $nameB= defined $val
ABresult
0X0
100
111
260
261 0 return $s->context->headers_out->{ $name } = $val;
262 }# end AddHeader()
263  
264  
265 #==============================================================================
266 sub DeleteHeader
267 0 ▶{
Subroutine DeleteHeader: not called CC=1 SCAR=6.9
Pod documented
268 0 my ($s, $name) = @_;
269
270 0 $s->context->headers_out->remove_header( $name );
271 }# end DeleteHeader()
272  
273  
274 #==============================================================================
275 sub Headers
276 0 ▶{
Subroutine Headers: not called CC=1 SCAR=6.9
Pod documented
277 0 $_[0]->context->headers_out;
278 }# end Headers()
279  
280  
281 #==============================================================================
282 sub Clear
283 0 ▶{
Subroutine Clear: not called CC=1 SCAR=6.9
Pod documented
284 0 $_[0]->{_output_buffer} = [ ];
285 }# end Clear()
286  
287  
288 #==============================================================================
289 sub IsClientConnected
290 0 ▶{
Subroutine IsClientConnected: not called CC=1 SCAR=6.9
Pod documented
291 0 return ! shift->context->get_prop('did_end');
292 }# end IsClientConnected()
293  
294  
295 #==============================================================================
296 sub DESTROY
297 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
298 0 my $s = shift;
299
300 0 undef(%$s);
301 }# end DESTROY()
302  
303 23 1;# return true:
304  
305 =head1 NAME
306  
307 Apache2::ASP::Response - Outgoing response object.
308  
309 =head1 SYNOPSIS
310  
311 return $Response->Redirect("/another.asp");
312
313 return $Response->Declined;
314
315 $Response->End;
316
317 $Response->ContentType("text/xml");
318
319 $Response->Status( 404 );
320
321 # Make this response expire 30 minutes ago:
322 $Response->Expires( -30 );
323
324 $Response->Include( $Server->MapPath("/inc/top.asp"), { foo => 'bar' } );
325
326 my $html = $Response->TrapInclude( $Server->MapPath("/inc/top.asp"), { foo => 'bar' } );
327
328 $Response->AddHeader("content-disposition: attachment;filename=report.csv");
329
330 $Response->Write( "hello, world" );
331
332 $Response->Clear;
333
334 $Response->Flush;
335  
336 =head1 DESCRIPTION
337  
338 Apache2::ASP::Response offers a wrapper around the outgoing response to the client.
339  
340 =head1 PUBLIC PROPERTIES
341  
342 =head2 ContentType( [$type] )
343  
344 Sets/gets the content-type response header (i.e. text/html, image/gif, etc).
345  
346 Default: text/html
347  
348 =head2 Status( [$status] )
349  
350 Sets/gets the status response header (i.e. 200, 404, etc).
351  
352 Default: 200
353  
354 =head2 Expires( [$minutes] )
355  
356 Default 0
357  
358 =head2 ExpiresAbsolute( [$http_date] )
359  
360 =head2 Declined( )
361  
362 Returns C<-1>.
363  
364 =head2 Cookies( )
365  
366 Returns all outgoing cookies for this response.
367  
368 =head2 Headers( )
369  
370 Returns all outgoing headers for this response.
371  
372 =head2 IsClientConnected( )
373  
374 Returns true if the client is still connected, false otherwise.
375  
376 =head1 PUBLIC METHODS
377  
378 =head2 Write( $str )
379  
380 Adds C<$str> to the response buffer.
381  
382 =head2 Redirect( $path )
383  
384 Clears the response buffer and sends a 301 redirect to the client.
385  
386 Throws an exception if headers have already been sent.
387  
388 =head2 Include( $path, \%args )
389  
390 Executes the script located at C<$path>, passing along C<\%args>. Output is
391 included as part of the current script's output.
392  
393 =head2 TrapInclude( $path, \%args )
394  
395 Executes the script located at C<$path>, passing along C<\%args>, and returns
396 the response as a string.
397  
398 =head2 AddCookie( $name => $value )
399  
400 Adds a cookie to the header.
401  
402 =head2 AddHeader( $name => $value )
403  
404 Adds a header to the response.
405  
406 =head2 DeleteHeader( $name )
407  
408 Removes an outgoing header.
409  
410 Throws an exception if headers have already been sent.
411  
412 =head2 Flush( )
413  
414 Sends any buffered output to the client.
415  
416 =head2 Clear( )
417  
418 Clears the outgoing buffer.
419  
420 =head2 End( )
421  
422 Closes the connection to the client and terminates the current request.
423  
424 Throws an exception if headers have already been sent.
425  
426 =head1 BUGS
427
428 It's possible that some bugs have found their way into this release.
429
430 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports.
431
432 =head1 HOMEPAGE
433
434 Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples
435 of Apache2::ASP in action.
436  
437 =head1 AUTHOR
438  
439 John Drago <jdrago_999@yahoo.com>
440  
441 =head1 COPYRIGHT
442  
443 Copyright 2008 John Drago. All rights reserved.
444  
445 =head1 LICENSE
446  
447 This software is Free software and is licensed under the same terms as perl itself.
448  
449 =cut
450  
« ASP/HTTPContext/HandlerResolver.pm ↑ Summary ASP/ConfigNode.pm »