source: trunk/essentials/dev-lang/perl/lib/CGI/Push.pm@ 3298

Last change on this file since 3298 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 10.7 KB
Line 
1package CGI::Push;
2
3# See the bottom of this file for the POD documentation. Search for the
4# string '=head'.
5
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).
9
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.
15
16# The most recent version and complete docs are available at:
17# http://stein.cshl.org/WWW/software/CGI/
18
19$CGI::Push::VERSION='1.04';
20use CGI;
21use CGI::Util 'rearrange';
22@ISA = ('CGI');
23
24$CGI::DefaultClass = 'CGI::Push';
25$CGI::Push::AutoloadClass = 'CGI';
26
27# add do_push() and push_delay() to exported tags
28push(@{$CGI::EXPORT_TAGS{':standard'}},'do_push','push_delay');
29
30sub do_push {
31 my ($self,@p) = CGI::self_or_default(@_);
32
33 # unbuffer output
34 $| = 1;
35 srand;
36 my ($random) = sprintf("%08.0f",rand()*1E8);
37 my ($boundary) = "----=_NeXtPaRt$random";
38
39 my (@header);
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);
46
47 my(@o);
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);
56
57 $boundary = "$CGI::CRLF--$boundary";
58
59 print "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY.${boundary}$CGI::CRLF";
60
61 my (@contents) = &$callback($self,++$COUNTER);
62
63 # now we enter a little loop
64 while (1) {
65 print "Content-type: ${type}$CGI::CRLF$CGI::CRLF" unless $type =~ /^dynamic|heterogeneous$/i;
66 print @contents;
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();
71 } else {
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);
77 }
78 print "${boundary}--$CGI::CRLF";
79 last;
80 }
81 }
82 print "WARNING: YOUR BROWSER DOESN'T SUPPORT THIS SERVER-PUSH TECHNOLOGY.$CGI::CRLF";
83}
84
85sub simple_counter {
86 my ($self,$count) = @_;
87 return $self->start_html("CGI::Push Default Counter"),
88 $self->h1("CGI::Push Default Counter"),