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-2000, 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.04';
21 use CGI::Util 'rearrange';
24 $CGI::DefaultClass = 'CGI::Push';
25 $CGI::Push::AutoloadClass = 'CGI';
27 # add do_push() and push_delay() to exported tags
28 push(@{$CGI::EXPORT_TAGS{':standard'}},'do_push','push_delay');
31 my ($self,@p) = CGI::self_or_default(@_);
36 my ($random) = sprintf("%08.0f",rand()*1E8);
37 my ($boundary) = "----=_NeXtPaRt$random";
40 my ($type,$callback,$delay,$last_page,$cookie,$target,$expires,$nph,@other) = rearrange([TYPE,NEXT_PAGE,DELAY,LAST_PAGE,[COOKIE,COOKIES],TARGET,EXPIRES,NPH],@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);
45 $nph = 1 unless defined($nph);
48 foreach (@other) { push(@o,split("=")); }
49 push(@o,'-Target'=>$target) if defined($target);
50 push(@o,'-Cookie'=>$cookie) if defined($cookie);
51 push(@o,'-Type'=>"multipart/x-mixed-replace;boundary=\"$boundary\"");
52 push(@o,'-Server'=>"CGI.pm Push Module") if $nph;
53 push(@o,'-Status'=>'200 OK');
54 push(@o,'-nph'=>1) if $nph;
55 print $self->header(@o);
57 $boundary = "$CGI::CRLF--$boundary";
59 print "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY.${boundary}$CGI::CRLF";
61 my (@contents) = &$callback($self,++$COUNTER);
63 # now we enter a little loop
65 print "Content-type: ${type}$CGI::CRLF$CGI::CRLF" unless $type =~ /^dynamic|heterogeneous$/i;
67 @contents = &$callback($self,++$COUNTER);
68 if ((@contents) && defined($contents[0])) {
69 print "${boundary}$CGI::CRLF";
70 do_sleep($self->push_delay()) if $self->push_delay();
72 if ($last_page && ref($last_page) eq 'CODE') {
73 print "${boundary}$CGI::CRLF";
74 do_sleep($self->push_delay()) if $self->push_delay();
75 print "Content-type: ${type}$CGI::CRLF$CGI::CRLF" unless $type =~ /^dynamic|heterogeneous$/i;
76 print &$last_page($self,$COUNTER);
78 print "${boundary}--$CGI::CRLF";
82 print "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY.$CGI::CRLF";
86 my ($self,$count) = @_;
87 return $self->start_html("CGI::Push Default Counter"),
88 $self->h1("CGI::Push Default Counter"),
89 "This page has been updated ",$self->strong($count)," times.",
91 $self->a({'-href'=>'http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html'},'CGI.pm home page'),
97 if ( ($delay >= 1) && ($delay!~/\./) ){
100 select(undef,undef,undef,$delay);
105 my ($self,$delay) = CGI::self_or_default(@_);
106 return defined($delay) ? $self->{'.delay'} =
107 $delay : $self->{'.delay'};
114 CGI::Push - Simple Interface to Server Push
118 use CGI::Push qw(:standard);
120 do_push(-next_page=>\&next_page,
121 -last_page=>\&last_page,
125 my($q,$counter) = @_;
126 return undef if $counter >= 10;
127 return start_html('Test'),
129 "This page has been called ", strong($counter)," times",
134 my($q,$counter) = @_;
135 return start_html('Done'),
137 strong($counter - 1),' iterations.',
143 CGI::Push is a subclass of the CGI object created by CGI.pm. It is
144 specialized for server push operations, which allow you to create
145 animated pages whose content changes at regular intervals.
147 You provide CGI::Push with a pointer to a subroutine that will draw
148 one page. Every time your subroutine is called, it generates a new
149 page. The contents of the page will be transmitted to the browser
150 in such a way that it will replace what was there beforehand. The
151 technique will work with HTML pages as well as with graphics files,
152 allowing you to create animated GIFs.
154 Only Netscape Navigator supports server push. Internet Explorer
157 =head1 USING CGI::Push
159 CGI::Push adds one new method to the standard CGI suite, do_push().
160 When you call this method, you pass it a reference to a subroutine
161 that is responsible for drawing each new page, an interval delay, and
162 an optional subroutine for drawing the last page. Other optional
163 parameters include most of those recognized by the CGI header()
166 You may call do_push() in the object oriented manner or not, as you
171 $q->do_push(-next_page=>\&draw_a_page);
175 use CGI::Push qw(:standard);
176 do_push(-next_page=>\&draw_a_page);
178 Parameters are as follows:
184 do_push(-next_page=>\&my_draw_routine);
186 This required parameter points to a reference to a subroutine responsible for
187 drawing each new page. The subroutine should expect two parameters
188 consisting of the CGI object and a counter indicating the number
189 of times the subroutine has been called. It should return the
190 contents of the page as an B<array> of one or more items to print.
191 It can return a false value (or an empty array) in order to abort the
192 redrawing loop and print out the final page (if any)
194 sub my_draw_routine {
195 my($q,$counter) = @_;
196 return undef if $counter > 100;
197 return start_html('testing'),
199 "This page called $counter times";
202 You are of course free to refer to create and use global variables
203 within your draw routine in order to achieve special effects.
207 This optional parameter points to a reference to the subroutine
208 responsible for drawing the last page of the series. It is called
209 after the -next_page routine returns a false value. The subroutine
210 itself should have exactly the same calling conventions as the
215 This optional parameter indicates the content type of each page. It
216 defaults to "text/html". Normally the module assumes that each page
217 is of a homogenous MIME type. However if you provide either of the
218 magic values "heterogeneous" or "dynamic" (the latter provided for the
219 convenience of those who hate long parameter names), you can specify
220 the MIME type -- and other header fields -- on a per-page basis. See
221 "heterogeneous pages" for more details.
225 This indicates the delay, in seconds, between frames. Smaller delays
226 refresh the page faster. Fractional values are allowed.
228 B<If not specified, -delay will default to 1 second>
230 =item -cookie, -target, -expires, -nph
232 These have the same meaning as the like-named parameters in
235 If not specified, -nph will default to 1 (as needed for many servers, see below).
239 =head2 Heterogeneous Pages
241 Ordinarily all pages displayed by CGI::Push share a common MIME type.
242 However by providing a value of "heterogeneous" or "dynamic" in the
243 do_push() -type parameter, you can specify the MIME type of each page
244 on a case-by-case basis.
246 If you use this option, you will be responsible for producing the
247 HTTP header for each page. Simply modify your draw routine to
250 sub my_draw_routine {
251 my($q,$counter) = @_;
252 return header('text/html'), # note we're producing the header here
253 start_html('testing'),
255 "This page called $counter times";
258 You can add any header fields that you like, but some (cookies and
259 status fields included) may not be interpreted by the browser. One
260 interesting effect is to display a series of pages, then, after the
261 last page, to redirect the browser to a new URL. Because redirect()
262 does b<not> work, the easiest way is with a -refresh header field,
265 sub my_draw_routine {
266 my($q,$counter) = @_;
267 return undef if $counter > 10;
268 return header('text/html'), # note we're producing the header here
269 start_html('testing'),
271 "This page called $counter times";
275 return header(-refresh=>'5; URL=http://somewhere.else/finished.html',
278 h1('This is the last page'),
284 =head2 Changing the Page Delay on the Fly
286 If you would like to control the delay between pages on a page-by-page
287 basis, call push_delay() from within your draw routine. push_delay()
288 takes a single numeric argument representing the number of seconds you
289 wish to delay after the current page is displayed and before
290 displaying the next one. The delay may be fractional. Without
291 parameters, push_delay() just returns the current delay.
293 =head1 INSTALLING CGI::Push SCRIPTS
295 Server push scripts must be installed as no-parsed-header (NPH)
296 scripts in order to work correctly on many servers. On Unix systems,
297 this is most often accomplished by prefixing the script's name with "nph-".
298 Recognition of NPH scripts happens automatically with WebSTAR and
299 Microsoft IIS. Users of other servers should see their documentation
302 Apache web server from version 1.3b2 on does not need server
303 push scripts installed as NPH scripts: the -nph parameter to do_push()
304 may be set to a false value to disable the extra headers needed by an
307 =head1 AUTHOR INFORMATION
309 Copyright 1995-1998, Lincoln D. Stein. All rights reserved.
311 This library is free software; you can redistribute it and/or modify
312 it under the same terms as Perl itself.
314 Address bug reports and comments to: lstein@cshl.org
318 This section intentionally left blank.