Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
11 KiB

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