File Coverage

blib/lib/CGI/Push.pm
Criterion Covered Total %
statement 56 58 96.5
branch 21 32 65.6
condition 6 12 50.0
subroutine 7 7 100.0
pod 0 4 0.0
total 90 113 79.6


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