"CGI script's standard handles (out/err/in) are connected to pipes set up by the webserver" what does this mean ?
A pipe is an OS-supplied means used for one-way interprocess communication. That is, a pipe has two ends, one for reading and one
for writing. Both ends are like normal file handles (except that they
aren't seek-able).
Here's a quick sample in Perl to illustrate what a webserver
typically does to run a CGI script (this is heavily simplified, and
webservers like Apache of course do implement this in C, but you
get the idea...):
#!/usr/bin/perl
pipe RH, WH; # Perl's interface to the system call of the same name
my $pid = fork(); die "fork failed" unless defined $pid;
if ($pid) { # parent - the webserver process that handles the request
close WH;
# read the CGI's output from the pipe
while (my $line = <RH>) {
print STDERR "CGI said: $line";
# (this would be passed on to the browser...)
}
} else { # child - a new process which runs the CGI script
close RH;
close STDOUT;
# connect stdout (more precisely: file descriptor 1) to
# the writing end of the pipe
open STDOUT, ">&WH" or die "open to pipe failed: $!";
exec "perl mycgi.pl" or die "exec of CGI failed: $!";
}
With the following sample mycgi.pl
#!/usr/bin/perl
print <<'EOCGI';
Content-Type: text/html
<html>
<header><title>Pipe Demo</title></header>
<body>
<pre>
foo
bar
baz
</pre>
</body>
</html>
EOCGI
the output produced by th above snippet would be
$ ./813729.pl
CGI said: Content-Type: text/html
CGI said:
CGI said: <html>
CGI said: <header><title>Pipe Demo</title></header>
CGI said: <body>
CGI said: <pre>
CGI said: foo
CGI said: bar
CGI said: baz
CGI said: </pre>
CGI said: </body>
CGI said: </html>
|