the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

Sponsored by El Scripto

Visit the Mortgage Loan Place for Home Loans and also click here to find VA Loans on our site.

Get more website traffic

home / articles / cgi

Server Push in Perl

With the recent emphasis on so-called "server push" technologies, like Netscape's Netcaster, I thought it would be nice to take a look at another type of push that's actually been around for a few years. This one involves a server connection that doesn't close. Normally, when a client requests a document or image from a web server, a connection is opened, the data transmitted, then the connection is closed. This works well for nearly all types of transmissions. But, what if you want to keep sending multiple documents, like a stream of images a series of .html or .au files?

Welcome to the wonderful world of persistent connections. First featured in Netscape's Amazing, Continuously Refreshing Fish Cam, this special type of connection will remain open to send a stream of data. Basically, it takes advantage of a little-used content type. It's been working just fine in Netscape since version 1.1, so don't be afraid to implement it; even those people who've never upgraded their browsers can use it. Unfortunately, however, Microsoft's Internet Explorer 3.0 does not support it.

We'll be using the following content-type header:

Content-type: multipart/x-mixed-replace; boundary=somemagicalboundarystring
Each part will be delimited with the string "somemagicalboundarystring". For obvious reasons, this string should not appear in any of the output documents. It should be noted that each part of the multipart document will replace the previous one in the web browser. So, the effect will be the following, including all the HTTP headers:
HTTP/1.0 200
Content-type: multipart/x-mixed-replace; boundary=magicalboundarystring

--magicalboundarystring
Content-type: image/jpeg

JPEG IMAGE STREAM GOES HERE
--magicalboundarystring
Content-type: image/gif

GIF IMAGE STREAM GOES HERE
--magicalboundarystring
Content-type: text/plain

Hi. This is a sample plain text file
--magicalboundarystring

The x-mixed-replace content type means that each part will replace the previous one. You'll see a series of images, html documents or whatever the content happens to be.

Web servers can be notoriously difficult to set up and maintain. So, you'll have to figure out how to implement a subtle feature called "non-parsed" CGIs on your site. Normally, the web server will buffer all output from your CGI program until it finishes. We don't want that to happen here. With Apache, it's quite easy. If the name of your CGI program starts with "nph" it won't be parsed. So, save the following Perl script with a name like "nph-serverpush.pl" and try it out. Just be sure to change the path in "/some/path/to/images/and/stuff/" to a directory on your server that's world readable. (It should be the absolute path, not a virtual one.) This script will read each file in that directory. If the file's extension is .html, .txt, .jpeg, .jpg, or .gif, it will be sent and wait 5 seconds before sending the next one.

If the user hits the STOP button, the HTTP connection will be broken and the Perl script will terminate. You should note that while this script is running your server has a connection open. This shouldn't be a problem on most web servers which can handle many simultaneous connections.

Source Code

Author: Doug Steinwand
Date: 08/12/1997

More articles about CGI
More articles by Doug Steinwand
Author Biography
Get thousands of people selling your product online for FREE
write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.

DY>