in reply to Re: display of output from cgi script
in thread display of output from cgi script

1) So STDOUT is just a file handle to store any output. How is the output transfered to browser from STDOUT is still not clear.
"CGI script's standard handles (out/err/in) are connected to pipes set up by the webserver" what does this mean ? Can you pls explain more on this ?

2) when data from form is transferrd by browser to webserver, webserver stores this data in environment variables. Are these environment variables specific to CGI interface or are they standard for all interfaces ? In other words, if I am using CGI protocol to generate dynamic pages then I am using these environment variables to get the data. But if I am using any other way than CGI, will I still be using same environment variables or will it be diferent ?
Are these environemnt variables set by the webserver or OS ?
  • Comment on Re^2: display of output from cgi script

Replies are listed 'Best First'.
Re^3: display of output from cgi script
by Khen1950fx (Canon) on Dec 21, 2009 at 17:31 UTC
Re^3: display of output from cgi script
by almut (Canon) on Dec 21, 2009 at 18:59 UTC
    "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>