CGI/Push.pm

filter: Statementstmt 96.8% 61 / 63 Branchbran 65.6% 21 / 32 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 4 totaltotal 76.8% 96 / 125 CC 22 SCAR 27.6
CC
22
Cov
82%
do_push
21.6
do_sleep
3.6
push_delay
2.0
CRAP 15.8
« CGI/Util.pm ↑ Summary CGI/Carp.pm »
1 package CGI::Push;
2  
3 1 my $appease_cpants_kwalitee = q/
4 use strict;
5 use warnings;
6 #/;
7  
8 1 $CGI::Push::VERSION='5.02';
9 1 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use CGI::Util 'rearrange';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 @ISA = ('CGI');
12  
13 1 $CGI::DefaultClass = 'CGI::Push';
14  
15 # add do_push() and push_delay() to exported tags
16 1 push(@{$CGI::EXPORT_TAGS{':standard'}},'do_push','push_delay');
17  
18 4 ▶sub do_push {
Subroutine do_push: called CC=19 SCAR=30.7
Pod undocumented
19 4 my ($self,@p) = CGI::self_or_default(@_);
20  
21 # unbuffer output
22 4 $| = 1;
23 4 srand;
24 4 my ($random) = sprintf("%08.0f",rand()*1E8);
25 4 my ($boundary) = "----=_NeXtPaRt$random";
26  
27 4 my (@header);
28 4 my ($type,$callback,$delay,$last_page,$cookie,$target,$expires,$nph,@other) = rearrange([TYPE,NEXT_PAGE,DELAY,LAST_PAGE,[COOKIE,COOKIES],TARGET,EXPIRES,NPH],@p);
29 4 ▶ $type = 'text/html' unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 1 3 4
30 # $type is printed directly in each part's header, so check it here
31 4 CGI::_check_header_values($type);
32 3 ▶ $callback = \&simple_counter unless $callback && ref($callback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless $callback and ref $callback eq "CODE" 0 3 3
Condition33%1/3
$callback and ref $callback eq "CODE"
!ll&&!rl&&r
003
MC/DC0%0/2
$callback and ref $callback eq "CODE"
$callback ref $callback eq "CODE"
Truth table33%1/3
A and B
A= $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
33 3 ▶ $delay = 1 unless defined($delay);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delay 0 3 3
34 3 $self->push_delay($delay);
35 3 ▶ $nph = 1 unless defined($nph);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nph 3 0 3
36  
37 3 my(@o);
38 0 foreach (@other) { push(@o,split("=")); }
39 3 ▶ push(@o,'-Target'=>$target) if defined($target);
Branch: 1 branch
BranchTrueFalseTotal
if defined $target 0 3 3
40 3 ▶ push(@o,'-Cookie'=>$cookie) if defined($cookie);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie 0 3 3
41 3 push(@o,'-Type'=>"multipart/x-mixed-replace;boundary=\"$boundary\"");
42 3 ▶ push(@o,'-Server'=>"CGI.pm Push Module") if $nph;
Branch: 1 branch
BranchTrueFalseTotal
if $nph 3 0 3
43 3 push(@o,'-Status'=>'200 OK');
44 3 ▶ push(@o,'-nph'=>1) if $nph;
Branch: 1 branch
BranchTrueFalseTotal
if $nph 3 0 3
45 3 print $self->header(@o);
46  
47 3 $boundary = "$CGI::CRLF--$boundary";
48  
49 3 print "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY.${boundary}$CGI::CRLF";
50  
51 3 my (@contents) = &$callback($self,++$COUNTER);
52  
53 # now we enter a little loop
54 3 while (1) {
55 4 ▶ print "Content-type: ${type}$CGI::CRLF$CGI::CRLF" unless $type =~ /^dynamic|heterogeneous$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^dynamic|heterogeneous$/i 3 1 4
56 4 print @contents;
57 4 @contents = &$callback($self,++$COUNTER);
58 4 ▶ if ((@contents) && defined($contents[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@contents and defined $contents[0]) { } 1 3 4
Condition66%2/3
@contents and defined $contents[0]
!ll&&!rl&&r
301
MC/DC50%1/2
@contents and defined $contents[0]
@contents defined $contents[0]
Truth table66%2/3
A and B
A= @contentsB= defined $contents[0]
ABresult
0X0
100
111
59 1 print "${boundary}$CGI::CRLF";
60 1 ▶ do_sleep($self->push_delay()) if $self->push_delay();
Branch: 1 branch
BranchTrueFalseTotal
if $self->push_delay 0 1 1
61 } else {
62 3 ▶ if ($last_page && ref($last_page) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_page and ref $last_page eq "CODE") 2 1 3
Condition66%2/3
$last_page and ref $last_page eq "CODE"
!ll&&!rl&&r
102
MC/DC50%1/2
$last_page and ref $last_page eq "CODE"
$last_page ref $last_page eq "CODE"
Truth table66%2/3
A and B
A= $last_pageB= ref $last_page eq "CODE"
ABresult
0X0
100
111
63 2 print "${boundary}$CGI::CRLF";
64 2 ▶ do_sleep($self->push_delay()) if $self->push_delay();
Branch: 1 branch
BranchTrueFalseTotal
if $self->push_delay 0 2 2
65 2 ▶ print "Content-type: ${type}$CGI::CRLF$CGI::CRLF" unless $type =~ /^dynamic|heterogeneous$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^dynamic|heterogeneous$/i 2 0 2
66 2 print &$last_page($self,$COUNTER);
67 }
68 3 print "${boundary}--$CGI::CRLF";
69 3 last;
70 }
71 }
72 3 print "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY.$CGI::CRLF";
73 }
74  
75 1 ▶sub simple_counter {
Subroutine simple_counter: called CC=1 SCAR=0.0
Pod undocumented
76 1 my ($self,$count) = @_;
77 1 return $self->start_html("CGI::Push Default Counter"),
78 $self->h1("CGI::Push Default Counter"),
79 "This page has been updated ",$self->strong($count)," times.",
80 $self->hr(),
81 $self->a({'-href'=>'http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html'},'CGI.pm home page'),
82 $self->end_html;
83 }
84  
85 1 ▶sub do_sleep {
Subroutine do_sleep: called CC=3 SCAR=12.7
Pod undocumented
86 1 my $delay = shift;
87 1 ▶ if ( ($delay >= 1) && ($delay!~/\./) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($delay >= 1 and not $delay =~ /\./) { } 0 1 1
Condition33%1/3
$delay >= 1 and not $delay =~ /\./
!ll&&!rl&&r
100
MC/DC0%0/2
$delay >= 1 and not $delay =~ /\./
$delay >= 1 not $delay =~ /\./
Truth table33%1/3
A and not(B)
A= $delay >= 1B= not $delay =~ /\./
ABresult
0X0
100
111
88 0 sleep($delay);
89 } else {
90 1 select(undef,undef,undef,$delay);
91 1 return $delay;
92 }
93 }
94  
95 8 ▶sub push_delay {
Subroutine push_delay: called CC=2 SCAR=6.9
Pod undocumented
96 8 my ($self,$delay) = CGI::self_or_default(@_);
97 return defined($delay) ? $self->{'.delay'} =
98 8 ▶ $delay : $self->{'.delay'};
Branch: 1 branch
BranchTrueFalseTotal
defined $delay ? : 4 4 8
99 }
100  
101 1 1;
102  
103 =head1 NAME
104  
105 CGI::Push - Simple Interface to Server Push
106  
107 =head1 SYNOPSIS
108  
109 use strict;
110 use warnings;
111  
112 use CGI::Push qw(:standard);
113  
114 do_push(
115 -next_page => \&next_page,
116 -last_page => \&last_page,
117 -delay => 0.5
118 );
119  
120 sub next_page {
121 my($q,$counter) = @_;
122 return undef if $counter >= 10;
123 ....
124 }
125  
126 sub last_page {
127 my($q,$counter) = @_;
128 return ...
129 }
130  
131 =head1 DESCRIPTION
132  
133 CGI::Push is a subclass of the CGI object created by CGI.pm. It is
134 specialized for server push operations, which allow you to create
135 animated pages whose content changes at regular intervals.
136  
137 You provide CGI::Push with a pointer to a subroutine that will draw
138 one page. Every time your subroutine is called, it generates a new
139 page. The contents of the page will be transmitted to the browser
140 in such a way that it will replace what was there beforehand. The
141 technique will work with HTML pages as well as with graphics files,
142 allowing you to create animated GIFs.
143  
144 Only Netscape Navigator supports server push. Internet Explorer
145 browsers do not.
146  
147 =head1 USING CGI::Push
148  
149 CGI::Push adds one new method to the standard CGI suite, do_push().
150 When you call this method, you pass it a reference to a subroutine
151 that is responsible for drawing each new page, an interval delay, and
152 an optional subroutine for drawing the last page. Other optional
153 parameters include most of those recognized by the CGI header()
154 method.
155  
156 You may call do_push() in the object oriented manner or not, as you
157 prefer:
158  
159 use CGI::Push;
160 $q = CGI::Push->new;
161 $q->do_push(-next_page=>\&draw_a_page);
162  
163 -or-
164  
165 use CGI::Push qw(:standard);
166 do_push(-next_page=>\&draw_a_page);
167  
168 Parameters are as follows:
169  
170 =over 4
171  
172 =item -next_page
173  
174 do_push(-next_page=>\&my_draw_routine);
175  
176 This required parameter points to a reference to a subroutine responsible for
177 drawing each new page. The subroutine should expect two parameters
178 consisting of the CGI object and a counter indicating the number
179 of times the subroutine has been called. It should return the
180 contents of the page as an B<array> of one or more items to print.
181 It can return a false value (or an empty array) in order to abort the
182 redrawing loop and print out the final page (if any)
183  
184 sub my_draw_routine {
185 my($q,$counter) = @_;
186 return undef if $counter > 100;
187 ...
188 }
189  
190 You are of course free to refer to create and use global variables
191 within your draw routine in order to achieve special effects.
192  
193 =item -last_page
194  
195 This optional parameter points to a reference to the subroutine
196 responsible for drawing the last page of the series. It is called
197 after the -next_page routine returns a false value. The subroutine
198 itself should have exactly the same calling conventions as the
199 -next_page routine.
200  
201 =item -type
202  
203 This optional parameter indicates the content type of each page. It
204 defaults to "text/html". Normally the module assumes that each page
205 is of a homogeneous MIME type. However if you provide either of the
206 magic values "heterogeneous" or "dynamic" (the latter provided for the
207 convenience of those who hate long parameter names), you can specify
208 the MIME type -- and other header fields -- on a per-page basis. See
209 "heterogeneous pages" for more details.
210  
211 =item -delay
212  
213 This indicates the delay, in seconds, between frames. Smaller delays
214 refresh the page faster. Fractional values are allowed.
215  
216 B<If not specified, -delay will default to 1 second>
217  
218 =item -cookie, -target, -expires, -nph
219  
220 These have the same meaning as the like-named parameters in
221 CGI::header().
222  
223 If not specified, -nph will default to 1 (as needed for many servers, see below).
224  
225 =back
226  
227 =head2 Heterogeneous Pages
228  
229 Ordinarily all pages displayed by CGI::Push share a common MIME type.
230 However by providing a value of "heterogeneous" or "dynamic" in the
231 do_push() -type parameter, you can specify the MIME type of each page
232 on a case-by-case basis.
233  
234 If you use this option, you will be responsible for producing the
235 HTTP header for each page. Simply modify your draw routine to
236 look like this:
237  
238 sub my_draw_routine {
239 my($q,$counter) = @_;
240 return header('text/html'), # note we're producing the header here
241 ....
242 }
243  
244 You can add any header fields that you like, but some (cookies and
245 status fields included) may not be interpreted by the browser. One
246 interesting effect is to display a series of pages, then, after the
247 last page, to redirect the browser to a new URL. Because redirect()
248 does b<not> work, the easiest way is with a -refresh header field,
249 as shown below:
250  
251 sub my_draw_routine {
252 my($q,$counter) = @_;
253 return undef if $counter > 10;
254 return header('text/html'), # note we're producing the header here
255 ...
256 }
257  
258 sub my_last_page {
259 return header(-refresh=>'5; URL=http://somewhere.else/finished.html',
260 -type=>'text/html'),
261 ...
262 }
263  
264 =head2 Changing the Page Delay on the Fly
265  
266 If you would like to control the delay between pages on a page-by-page
267 basis, call push_delay() from within your draw routine. push_delay()
268 takes a single numeric argument representing the number of seconds you
269 wish to delay after the current page is displayed and before
270 displaying the next one. The delay may be fractional. Without
271 parameters, push_delay() just returns the current delay.
272  
273 =head1 INSTALLING CGI::Push SCRIPTS
274  
275 Server push scripts must be installed as no-parsed-header (NPH)
276 scripts in order to work correctly on many servers. On Unix systems,
277 this is most often accomplished by prefixing the script's name with "nph-".
278 Recognition of NPH scripts happens automatically with WebSTAR and
279 Microsoft IIS. Users of other servers should see their documentation
280 for help.
281  
282 Apache web server from version 1.3b2 on does not need server
283 push scripts installed as NPH scripts: the -nph parameter to do_push()
284 may be set to a false value to disable the extra headers needed by an
285 NPH script.
286  
287 =head1 AUTHOR INFORMATION
288  
289 The CGI.pm distribution is copyright 1995-2007, Lincoln D. Stein. It is
290 distributed under the Artistic License 2.0. It is currently
291 maintained by Lee Johnson with help from many contributors.
292  
293 Address bug reports and comments to: https://github.com/leejo/CGI.pm/issues
294  
295 The original bug tracker can be found at: https://rt.cpan.org/Public/Dist/Display.html?Queue=CGI.pm
296  
297 When sending bug reports, please provide the version of CGI.pm, the version of
298 Perl, the name and version of your Web server, and the name and version of the
299 operating system you are using. If the problem is even remotely browser
300 dependent, please provide information about the affected browsers as well.
301 Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
302  
303 =head1 BUGS
304  
305 This section intentionally left blank.
306  
307 =head1 SEE ALSO
308  
309 L<CGI::Carp>, L<CGI>
310  
311 =cut
312  
« CGI/Util.pm ↑ Summary CGI/Carp.pm »