blib/lib/ASP4/Response.pm

filter: Statementstmt 94.6% 107 / 113 Branchbran 56.6% 17 / 30 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 26 / 26 Podpod 73.6% 14 / 19 totaltotal 83.5% 167 / 200 CC 19 SCAR 17.7
CC
19
Cov
84%
SetCookie
18.3
Expires
6.5
Redirect
2.0
CRAP 5.9
« blib/lib/ASP4/HTTPContext.pm ↑ Summary blib/lib/ASP4/HandlerResolver.pm »
1  
2 package ASP4::Response;
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 HTTP::Date qw( time2str );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use ASP4::HTTPContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use ASP4::Mock::RequestRec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 sub new
12 5145 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 5145 my $s = bless {
14 _status => 200,
15 _expires => 0,
16 _content_type => 'text/html',
17 _expires_absolute => time2str( time() ),
18 }, shift;
19 5145 $s->Status( $s->Status );
20 5145 $s->Expires( $s->Expires );
21 5145 $s->ContentType( $s->ContentType );
22
23 5145 return $s;
24 }# end new()
25  
26 78828 ▶sub context { ASP4::HTTPContext->current }
Subroutine context: called CC=1 SCAR=0.0
Pod undocumented
27  
28  
29 sub ContentType
30 10297 ▶{
Subroutine ContentType: called CC=2 SCAR=6.9
Pod documented
31 10297 my $s = shift;
32
33 10297 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 5152 5145 10297
34 {
35 5152 my $type = shift;
36 5152 $s->{_content_type} = $type;
37 5152 $s->context->r->content_type( $type );
38 5152 $s->SetHeader( 'content-type' => $type );
39 }
40 else
41 {
42 5145 return $s->{_content_type};
43 }# end if()
44 }# end ContentType()
45  
46  
47 sub Expires
48 11294 ▶{
Subroutine Expires: called CC=6 SCAR=18.7
Pod undocumented
49 11294 my $s = shift;
50 11294 ▶ if( my $value = shift )
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = shift()) 1003 10291 11294
51 {
52 1003 my $time;
53 1003 ▶ if( my ($num,$type) = $value =~ m/^(\-?\d+)([MHD])$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $value =~ /^(\-?\d+)([MHD])$/) { } 1 1002 1003
54 {
55 1 my $expires;
56 1 ▶ if( $type eq 'M' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'M') { } 0 1 1
elsif ($type eq 'H') { } 1 0 1
elsif ($type eq 'D') { } 0 0 0
57 # Minutes:
58 0 $expires = time() + ( $num * 60 );
59 }
60 elsif( $type eq 'H' ) {
61 # Hours:
62 1 $expires = time() + ( $num * 60 * 60 );
63 }
64 elsif( $type eq 'D' ) {
65 # Days:
66 0 $expires = time() + ( $num * 60 * 60 * 24 );
67 }# end if()
68 1 $time = $expires;
69 }
70 else
71 {
72 1002 $time = $value;
73 }# end if()
74
75 1003 $s->{_expires} = $time;
76 1003 $s->{_expires_absolute} = time2str( $time );
77 1003 $s->SetHeader( expires => $s->ExpiresAbsolute );
78 }# end if()
79
80 11294 return $s->{_expires};
81 }# end Expires()
82  
83  
84 1003 ▶sub ExpiresAbsolute { shift->{_expires_absolute} }
Subroutine ExpiresAbsolute: called CC=1 SCAR=0.0
Pod undocumented
85  
86  
87 sub Status
88 20590 ▶{
Subroutine Status: called CC=2 SCAR=6.9
Pod documented
89 20590 my $s = shift;
90
91 20590 ▶ @_ ? $s->context->r->status( $s->{_status} = +shift ) : $s->{_status};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5148 15442 20590
92 }# end Status()
93  
94  
95 sub End
96 5148 ▶{
Subroutine End: called CC=2 SCAR=6.9
Pod documented
97 5148 my $s = shift;
98
99 5148 ▶ if( $s->Status =~ m{^2} )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->Status =~ /^2/) { } 5142 6 5148
100 {
101 5142 $s->Flush;
102 }
103 else
104 {
105 6 delete $s->context->headers_out->{'content-type'};
106 }# end if()
107
108 # Would be nice to somehow stop all execution:
109 5148 $s->context->did_end( 1 );
110 }# end End()
111  
112  
113 sub Flush
114 10288 ▶{
Subroutine Flush: called CC=1 SCAR=0.0
Pod documented
115 10288 my $s = shift;
116 10288 $s->context->rflush;
117 }# end Flush()
118  
119  
120 sub Clear
121 1 ▶{
Subroutine Clear: called CC=1 SCAR=0.0
Pod undocumented
122 shift->context->rclear
123 1 }# end Clear()
124  
125  
126 sub IsClientConnected
127 1103 ▶{
Subroutine IsClientConnected: called CC=1 SCAR=0.0
Pod documented
128 1103 ! shift->context->r->connection->aborted();
129 }# end IsClientConnected()
130  
131  
132 sub Write
133 40654 ▶{
Subroutine Write: called CC=1 SCAR=0.0
Pod documented
134 40654 my $s = shift;
135 40654 $s->context->rprint( shift(@_) )
136 }# end Write()
137  
138  
139 sub SetCookie
140 2 ▶{
Subroutine SetCookie: called CC=10 SCAR=29.1
Pod documented
141 2 my ($s, %args) = @_;
142
143 2 ▶ $args{domain} ||= eval { $s->context->config->data_connections->session->cookie_domain } || $ENV{HTTP_HOST};
Condition33%2/6
$args{"domain"} ||= eval { do { $s->context->config->data_connections->session->cookie_domain } } || $ENV{"HTTP_HOST"}
l!l&&r!l&&!r
020
eval { do { $s->context->config->data_connections->session->cookie_domain } } || $ENV{'HTTP_HOST'}
l!l&&r!l&&!r
200
MC/DC0%0/3
$args{"domain"} ||= eval { do { $s->context->config->data_connections->session->cookie_domain } } || $ENV{"HTTP_HOST"}
$args{"domain"} eval { do { $s->context->config->data_connections->session->cookie_domain } } $ENV{'HTTP_HOST'}
Truth table0%0/4
A ||= B || C
A= $args{"domain"}B= eval { do { $s->context->config->data_connections->session->cookie_domain } }C= $ENV{'HTTP_HOST'}
ABCresult
1XX1
01X1
0011
0000
144 2 ▶ $args{path} ||= '/';
Condition50%1/2
$args{"path"} ||= "/"
l!l
02
MC/DC0%0/1
$args{"path"} ||= "/"
$args{"path"}
Truth table50%1/2
A
A= $args{"path"}
Aresult
11
00
145 2 my @parts = ( );
146 2 push @parts, $s->context->server->URLEncode($args{name}) . '=' . $s->context->server->URLEncode($args{value});
147 2 ▶ unless( $args{domain} eq '*' )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"domain"} eq "*") 0 2 2
148 {
149 0 push @parts, 'domain=' . $s->context->server->URLEncode($args{domain});
150 }# end unless()
151 2 push @parts, 'path=' . $args{path};
152 2 ▶ if( $args{expires} )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"expires"}) 2 0 2
153 {
154 2 ▶ if( my ($num,$type) = $args{expires} =~ m/^(\-?\d+)([MHD])$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $args{'expires'} =~ /^(\-?\d+)([MHD])$/) { } 2 0 2
155 {
156 2 my $expires;
157 2 ▶ if( $type eq 'M' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'M') { } 2 0 2
elsif ($type eq 'H') { } 0 0 0
elsif ($type eq 'D') { } 0 0 0
158 # Minutes:
159 2 $expires = time() + ( $num * 60 );
160 }
161 elsif( $type eq 'H' ) {
162 # Hours:
163 0 $expires = time() + ( $num * 60 * 60 );
164 }
165 elsif( $type eq 'D' ) {
166 # Days:
167 0 $expires = time() + ( $num * 60 * 60 * 24 );
168 }# end if()
169 2 push @parts, 'expires=' . time2str( $expires );
170 }
171 else
172 {
173 0 push @parts, 'expires=' . time2str( $args{expires} );
174 }# end if()
175 }# end if()
176 2 $s->AddHeader( 'Set-Cookie' => join('; ', @parts) . ';' );
177 }# end SetCookie()
178  
179  
180 sub AddHeader
181 3 ▶{
Subroutine AddHeader: called CC=1 SCAR=0.0
Pod documented
182 3 my ($s, $name, $value) = @_;
183
184 3 $s->context->headers_out->push_header( $name => $value );
185 }# end AddHeader()
186  
187  
188 sub SetHeader
189 6160 ▶{
Subroutine SetHeader: called CC=1 SCAR=0.0
Pod documented
190 6160 my ($s, $name, $value) = @_;
191
192 6160 $s->context->headers_out->header( $name => $value );
193 }# end AddHeader()
194  
195  
196 sub Headers
197 5129 ▶{
Subroutine Headers: called CC=1 SCAR=0.0
Pod documented
198 5129 my $s = shift;
199
200 5129 my $out = $s->context->headers_out;
201 5129 map {{
202 6162 $_ => $out->{$_}
203 }} keys %$out;
204 }# end Headers()
205  
206  
207 sub Redirect
208 1 ▶{
Subroutine Redirect: called CC=2 SCAR=7.0
Pod documented
209 1 my ($s, $url) = @_;
210
211 1 $s->Clear;
212 1 $s->Status( 301 );
213 1 ▶ $s->Expires( "-24H" )
Branch: 1 branch
BranchTrueFalseTotal
unless $s->Expires 1 0 1
214 unless $s->Expires;
215 1 $s->SetHeader( Location => $url );
216 1 $s->End;
217 1 return $s->Status;
218 }# end Redirect()
219  
220  
221 2 ▶sub Declined { -1 }
Subroutine Declined: called CC=1 SCAR=0.0
Pod documented
222  
223  
224 sub Include
225 8 ▶{
Subroutine Include: called CC=1 SCAR=0.0
Pod documented
226 8 my ($s, $file, $args) = @_;
227
228 8 $s->Write( $s->_subrequest( $file, $args ) );
229 }# end Include()
230  
231  
232 sub TrapInclude
233 2 ▶{
Subroutine TrapInclude: called CC=1 SCAR=0.0
Pod documented
234 2 my ($s, $file, $args) = @_;
235
236 2 return $s->_subrequest( $file, $args );
237 }# end TrapInclude()
238  
239  
240 sub _subrequest
241 10 ▶{
Subroutine _subrequest: called CC=1 SCAR=0.0
242 10 my ($s, $file, $args) = @_;
243
244 10 my $context = ASP4::HTTPContext->new( is_subrequest => 1 );
245 10 my $original_r = $s->context->r;
246 10 my $root = $s->context->config->web->www_root;
247 10 my $cgi = $s->context->cgi;
248 10 (my $uri = $file) =~ s/^\Q$root\E//;
249 10 my $r = ASP4::Mock::RequestRec->new(
250 uri => $uri,
251 args => $original_r->args,
252 );
253 SCOPE: {
254 10 local $ASP4::HTTPContext::_instance = $context;
255 10 local $ENV{SCRIPT_NAME} = $uri;
256 10 local $ENV{SCRIPT_FILENAME} = $file;
257 10 $context->setup_request( $r, $cgi );
258 10 $context->execute( $args, 1 );
259 };
260
261 10 return $context->r->buffer;
262 }# end _subrequest()
263  
264  
265 #sub _subrequest
266 #{
267 # my ($s, $file, $args) = @_;
268 #
269 # $s->context->add_buffer();
270 # my $original_r = $s->context->r;
271 # my $root = $s->context->config->web->www_root;
272 # (my $uri = $file) =~ s/^\Q$root\E//;
273 # my $r = ASP4::Mock::RequestRec->new(
274 # uri => $uri,
275 # args => $original_r->args,
276 # );
277 # local $ENV{SCRIPT_NAME} = $uri;
278 # local $ENV{SCRIPT_FILENAME} = $file;
279 # my $original_status = $s->Status();
280 # $s->context->setup_request( $r, $s->context->cgi );
281 # $s->context->execute( $args, 1 );
282 # $s->Flush;
283 # $s->Status( $original_status );
284 # my $buffer = $s->context->purge_buffer();
285 # $s->context->{r} = $original_r;
286 # $s->context->did_end( 0 );
287 # return $r->buffer;
288 #}# end _subrequest()
289  
290  
291 sub DESTROY
292 5112 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
293 5112 my $s = shift;
294 5112 undef(%$s);
295 }# end DESTROY()
296  
297 15 1;# return true:
298  
299 =pod
300  
301 =head1 NAME
302  
303 ASP4::Response - Interface to the outgoing HTTP response
304  
305 =head1 SYNOPSIS
306  
307 $Response->ContentType("text/html");
308
309 $Response->Status( 200 );
310
311 $Response->Clear();
312
313 $Response->Flush();
314  
315 $Response->Write("Hello, World!");
316
317 $Response->AddHeader( 'x-awesomeness' => '100%' );
318
319 $Response->SetHeader( 'x-velocity' => '100MPH' );
320
321 # Expires in the future:
322 $Response->Expires( '30M' ); # 30 minutes from now
323 $Response->Expires( '30H' ); # 30 hours from now
324 $Response->Expires( '30D' ); # 30 days from now
325
326 # Expires in the past:
327 $Response->Expires( '-30M' ); # 30 minutes ago
328 $Response->Expires( '-30H' ); # 30 hours ago
329 $Response->Expires( '-30D' ); # 30 days ago
330
331 $Response->SetCookie(
332 # Required parameters:
333 name => "customer-email",
334 value => $Form->{email},
335
336 # The rest are optional:
337 expires => '30D', # 30 days
338 path => '/',
339 domain => '.mysite.com',
340 );
341
342 $Response->Redirect( "/path/to/page.asp" );
343
344 $Response->Include( $Server->MapPath("/my/include.asp") );
345 $Response->Include( $Server->MapPath("/my/include.asp"), \%args );
346
347 my $string = $Response->TrapInclude( $Server->MapPath("/my/widget.asp") );
348 my $string = $Response->TrapInclude( $Server->MapPath("/my/widget.asp"), \%args );
349
350 return $Response->Declined;
351
352 $Response->End;
353
354 while( 1 ) {
355 last unless $Response->IsClientConnected();
356 $Response->Write("Still Here!<br/>");
357 sleep(1);
358 }
359
360 my HTTP::Headers $headers = $Response->Headers;
361
362 # Read-only:
363 my $expires_on = $Response->ExpiresAbsolute;
364  
365 =head1 DESCRIPTION
366  
367 The C<$Response> object offers a unified interface to send content back to the client.
368  
369 =head1 PROPERTIES
370  
371 =head2 ContentType( [$type] )
372  
373 Sets or gets the C<content-type> header for the response. Examples are C<text/html>, C<image/gif>, C<text/csv>, etc.
374  
375 =head2 Status( [$status] )
376  
377 Sets or gets the C<Status> header for the response. See L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html> for details.
378  
379 B<NOTE:> Only the numeric part is necessary - eg: 200, 301, 404, etc.
380  
381 =head2 Headers()
382  
383 Returns the L<HTTP::Headers> object that will be used for the outgoing response.
384  
385 If necessary, you can manipulate this object in any way you see fit.
386  
387 =head2 Declined
388  
389 For use within a L<ASP4::RequestFilter> subclass, like this:
390  
391 sub run {
392 # Permit requests only every other second:
393 if( time() % 2 ) {
394 return $Response->Declined;
395 }
396 else {
397 $Response->Write("Try again");
398 return $Response->End;
399 }
400 }
401  
402 =head2 IsClientConnected
403  
404 In a ModPerl environment, this can be used to determine whether the client has
405 closed the connection (hit the "Stop" button or closed their browser). Useful within
406 a long-running loop.
407  
408 =head1 METHODS
409  
410 =head2 Write( $str )
411  
412 Adds C<$str> to the output buffer.
413  
414 =head2 Flush( )
415  
416 Causes the output buffer to be flushed to the client.
417  
418 =head2 End( )
419  
420 Aborts the current request.
421  
422 Example:
423  
424 # Good:
425 return $Response->End;
426  
427 Simply calling...
428  
429 # Bad!
430 $Response->End;
431  
432 ...will not work as intended.
433  
434 =head2 AddHeader( $name => $value )
435  
436 Appends C<$value> to the header C<$name>.
437  
438 =head2 SetHeader( $name => $value )
439  
440 Sets (and replaces) the header C<$name> to the value of C<$value>.
441  
442 =head2 SetCookie( %args )
443  
444 Adds a new cookie to the response.
445  
446 C<%args> B<must> contain the following:
447  
448 =over 4
449  
450 =item * name
451  
452 A string - the name of the cookie.
453  
454 =item * value
455  
456 The value of the cookie.
457  
458 =back
459  
460 Other parameters are:
461  
462 =over 4
463  
464 =item * expires
465  
466 Can be in one of the following formats:
467  
468 =over 8
469  
470 =item * 30B<M>
471  
472 Minutes - how many minutes from "now" calculated as C<time() + (30 * 60)>
473  
474 Example:
475  
476 expires => '30M'
477 expires => '-5M' # 5 minutes ago
478  
479 =item * 2B<H>
480  
481 Hours - how many hours from "now" calculated as C<time() + (2 * 60 * 60)>
482  
483 Example:
484  
485 expires => '2H' # 2 hours
486 expires => '12H' # 12 Hours
487  
488 =item * 7B<D>
489  
490 Days - how many days from "now" calculated as C<time() + (7 * 60 * 60 * 24)>
491  
492 Example:
493  
494 expires => '7D' # A week
495 expires => '30D' # A month
496  
497 =back
498  
499 =item * path
500  
501 Defaults to "C</>" - you can restrict the "path" that the cookie will apply to.
502  
503 =item * domain
504  
505 Defaults to whatever you set your config->data_connections->session->cookie_domain to
506 in your asp4-config.json. Otherwise defaults to C<$ENV{HTTP_HOST}>.
507  
508 You can override the defaults by passing in a domain, but the browser may not accept
509 other domains. See L<http://www.ietf.org/rfc/rfc2109.txt> for details.
510  
511 =back
512  
513 =head2 Redirect( $url )
514  
515 Causes the following HTTP header to be sent:
516  
517 Status: 301 Moved
518 Location: $url
519  
520 =head2 Include( $path [, \%args ] )
521  
522 Executes the ASP script at C<$path> and includes its output. Additional C<\%args>
523 may be passed along to the include.
524  
525 The passed-in args are accessible to the include like this:
526  
527 <%
528 my ($self, $context, $args) = @_;
529
530 # Args is a hashref:
531 %>
532  
533 =head2 TrapInclude( $path [, \%args ] )
534  
535 Executes the ASP script at C<$path> and returns its output. Additional C<\%args>
536 may be passed along to the include.
537  
538 The passed-in args are accessible to the include like this:
539  
540 <%
541 my ($self, $context, $args) = @_;
542
543 # Args is a hashref:
544 %>
545  
546 =head1 BUGS
547  
548 It's possible that some bugs have found their way into this release.
549  
550 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
551  
552 =head1 HOMEPAGE
553  
554 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
555 of ASP4 in action.
556  
557 =cut
558  
« blib/lib/ASP4/HTTPContext.pm ↑ Summary blib/lib/ASP4/HandlerResolver.pm »