ASP4/Response.pm

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