3 # See the bottom of this file for the POD documentation. Search for the
6 # You can run this file through either pod2man or pod2html to produce pretty
7 # documentation in manual or html file format (these utilities are part of the
8 # Perl 5 distribution).
10 # Copyright 1995,1996, Lincoln D. Stein. All rights reserved.
11 # It may be used and modified freely, but I do request that this copyright
12 # notice remain attached to the file. You may modify this module as you
13 # wish, but if you redistribute a modified version, please attach a note
14 # listing the modifications you have made.
16 # The most recent version and complete docs are available at:
17 # http://stein.cshl.org/WWW/software/CGI/
19 $CGI::Push::VERSION='1.01';
23 $CGI::DefaultClass = 'CGI::Push';
24 $CGI::Push::AutoloadClass = 'CGI';
26 # add do_push() and push_delay() to exported tags
27 push(@{$CGI::EXPORT_TAGS{':standard'}},'do_push','push_delay');
30 my ($self,@p) = CGI::self_or_default(@_);
35 my ($random) = sprintf("%16.0f",rand()*1E16);
36 my ($boundary) = "----------------------------------$random";
39 my ($type,$callback,$delay,$last_page,$cookie,$target,$expires,@other) =
40 $self->rearrange([TYPE,NEXT_PAGE,DELAY,LAST_PAGE,[COOKIE,COOKIES],TARGET,EXPIRES],@p);
41 $type = 'text/html' unless $type;
42 $callback = \&simple_counter unless $callback && ref($callback) eq 'CODE';
43 $delay = 1 unless defined($delay);
44 $self->push_delay($delay);
47 foreach (@other) { push(@o,split("=")); }
48 push(@o,'-Target'=>$target) if defined($target);
49 push(@o,'-Cookie'=>$cookie) if defined($cookie);
50 push(@o,'-Type'=>"multipart/x-mixed-replace; boundary=$boundary");
51 push(@o,'-Server'=>"CGI.pm Push Module");
52 push(@o,'-Status'=>'200 OK');
54 print $self->header(@o);
55 print "${boundary}$CGI::CRLF";
57 # now we enter a little loop
60 last unless (@contents = &$callback($self,++$COUNTER)) && defined($contents[0]);
61 print "Content-type: ${type}$CGI::CRLF$CGI::CRLF"
62 unless $type eq 'dynamic';
63 print @contents,"$CGI::CRLF";
64 print "${boundary}$CGI::CRLF";
65 do_sleep($self->push_delay()) if $self->push_delay();
69 if ($last_page && ref($last_page) eq 'CODE') {
70 print "Content-type: ${type}$CGI::CRLF$CGI::CRLF" unless $type =~ /^dynamic|heterogeneous$/i;
71 print &$last_page($self,$COUNTER),"$CGI::CRLF${boundary}$CGI::CRLF";
76 my ($self,$count) = @_;
78 CGI->start_html("CGI::Push Default Counter"),
79 CGI->h1("CGI::Push Default Counter"),
80 "This page has been updated ",CGI->strong($count)," times.",
82 CGI->a({'-href'=>'http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html'},'CGI.pm home page'),
89 if ( ($delay >= 1) && ($delay!~/\./) ){
92 select(undef,undef,undef,$delay);
97 my ($self,$delay) = CGI::self_or_default(@_);
98 return defined($delay) ? $self->{'.delay'} =
99 $delay : $self->{'.delay'};
106 CGI::Push - Simple Interface to Server Push
110 use CGI::Push qw(:standard);
112 do_push(-next_page=>\&next_page,
113 -last_page=>\&last_page,
117 my($q,$counter) = @_;
118 return undef if $counter >= 10;
119 return start_html('Test'),
121 "This page has been called ", strong($counter)," times",
126 my($q,$counter) = @_;
127 return start_html('Done'),
129 strong($counter),' iterations.',
135 CGI::Push is a subclass of the CGI object created by CGI.pm. It is
136 specialized for server push operations, which allow you to create
137 animated pages whose content changes at regular intervals.
139 You provide CGI::Push with a pointer to a subroutine that will draw
140 one page. Every time your subroutine is called, it generates a new
141 page. The contents of the page will be transmitted to the browser
142 in such a way that it will replace what was there beforehand. The
143 technique will work with HTML pages as well as with graphics files,
144 allowing you to create animated GIFs.
146 =head1 USING CGI::Push
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()
155 You may call do_push() in the object oriented manner or not, as you
160 $q->do_push(-next_page=>\&draw_a_page);
164 use CGI::Push qw(:standard);
165 do_push(-next_page=>\&draw_a_page);
167 Parameters are as follows:
173 do_push(-next_page=>\&my_draw_routine);
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<array> 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)
183 sub my_draw_routine {
184 my($q,$counter) = @_;
185 return undef if $counter > 100;
186 return start_html('testing'),
188 "This page called $counter times";
191 You are of course free to refer to create and use global variables
192 within your draw routine in order to achieve special effects.
196 This optional parameter points to a reference to the subroutine
197 responsible for drawing the last page of the series. It is called
198 after the -next_page routine returns a false value. The subroutine
199 itself should have exactly the same calling conventions as the
204 This optional parameter indicates the content type of each page. It
205 defaults to "text/html". Normally the module assumes that each page
206 is of a homogenous MIME type. However if you provide either of the
207 magic values "heterogeneous" or "dynamic" (the latter provided for the
208 convenience of those who hate long parameter names), you can specify
209 the MIME type -- and other header fields -- on a per-page basis. See
210 "heterogeneous pages" for more details.
214 This indicates the delay, in seconds, between frames. Smaller delays
215 refresh the page faster. Fractional values are allowed.
217 B<If not specified, -delay will default to 1 second>
219 =item -cookie, -target, -expires
221 These have the same meaning as the like-named parameters in
226 =head2 Heterogeneous Pages
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.
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
237 sub my_draw_routine {
238 my($q,$counter) = @_;
239 return header('text/html'), # note we're producing the header here
240 start_html('testing'),
242 "This page called $counter times";
245 You can add any header fields that you like, but some (cookies and
246 status fields included) may not be interpreted by the browser. One
247 interesting effect is to display a series of pages, then, after the
248 last page, to redirect the browser to a new URL. Because redirect()
249 does b<not> work, the easiest way is with a -refresh header field,
252 sub my_draw_routine {
253 my($q,$counter) = @_;
254 return undef if $counter > 10;
255 return header('text/html'), # note we're producing the header here
256 start_html('testing'),
258 "This page called $counter times";
262 header(-refresh=>'5; URL=http://somewhere.else/finished.html',
265 h1('This is the last page'),
271 =head2 Changing the Page Delay on the Fly
273 If you would like to control the delay between pages on a page-by-page
274 basis, call push_delay() from within your draw routine. push_delay()
275 takes a single numeric argument representing the number of seconds you
276 wish to delay after the current page is displayed and before
277 displaying the next one. The delay may be fractional. Without
278 parameters, push_delay() just returns the current delay.
280 =head1 INSTALLING CGI::Push SCRIPTS
282 Server push scripts B<must> be installed as no-parsed-header (NPH)
283 scripts in order to work correctly. On Unix systems, this is most
284 often accomplished by prefixing the script's name with "nph-".
285 Recognition of NPH scripts happens automatically with WebSTAR and
286 Microsoft IIS. Users of other servers should see their documentation
289 =head1 AUTHOR INFORMATION
291 Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
293 This library is free software; you can redistribute it and/or modify
294 it under the same terms as Perl itself.
296 Address bug reports and comments to: lstein@cshl.org
300 This section intentionally left blank.