Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How a web server sending data to a CGI perl script ?

by Anonymous Monk
on Jan 21, 2016 at 12:59 UTC ( [id://1153271]=note: print w/replies, xml ) Need Help??


in reply to How a web server sending data to a CGI perl script ?

So, I don't know much about CGI. Your questions look pretty general to me, though. It seems you're asking about what incantations to use to put stuff in environment and redirect standard streams. Here is an example. I assume it's close enough to how CGI works. Note that there is no error handling whatsoever (I leave it as an exercise for the reader). Also note that it assumes Unix-like operating system.
# server.pl use strict; use warnings; use POSIX (); my $cgis = 0; $SIG{CHLD} = sub { while ( waitpid( -1, POSIX::WNOHANG ) > 0 ) { $cgis -= 1; } }; while (1) { print '(Ctrl-D to quit)> '; my $request = <STDIN>; last if not defined $request; chomp $request; print "Server: about to fork a CGI client\n"; $ENV{foo} = 'bar'; # use some real $ENV{baz} = 'quux'; # CGI variables pipe my ( $child_stdin, $input_to_child, ); pipe my ( $output_from_child, $child_stdout, ); my $pid = fork; if ( $pid == 0 ) { close $input_to_child; close $output_from_child; open STDIN, '<&', $child_stdin; open STDOUT, '>&', $child_stdout; exec {'perl'} 'perl', 'client.pl'; } else { close $child_stdin; close $child_stdout; print "Server: forked process $pid\n"; $cgis += 1; print $input_to_child $request; close $input_to_child; print "Server: reading child response\n", <$output_from_child>; } } print "Server exiting, remaining children: $cgis\n";
and client
# client.pl use strict; use warnings; my $input = <STDIN>; print <<END This is a pseudo-CGI client reporting. Process number $$ Got input "$input" \$ENV{foo} is "$ENV{foo}" \$ENV{baz} is "$ENV{baz}" End of report END

Replies are listed 'Best First'.
Re^2: How a web server sending data to a CGI perl script ?
by Anonymous Monk on Jan 21, 2016 at 13:38 UTC
    come to think of it, Perl's open is not entirely appropriate for redirecting. It's better to use low-level dup2, otherwise there are some bad interactions when standard streams are already closed (which must be the case for a server).
    if ( $pid == 0 ) { close $input_to_child; close $output_from_child; if ( fileno $child_stdout != POSIX::STDOUT_FILENO ) { POSIX::dup2( fileno $child_stdout, POSIX::STDOUT_FILENO ); close $child_stdout; } if ( fileno $child_stdin != POSIX::STDIN_FILENO ) { POSIX::dup2( fileno $child_stdin, POSIX::STDIN_FILENO ); close $child_stdin; } exec {'perl'} 'perl', 'client.pl'; }
      which must be the case for a server
      come to think of it more, that shouldn't be the case... I must be tired today :) Anyway, I changed the code mainly because I always thought Perl's open used dup2, but then I checked the doc and it actually says: dup(2). So it's probably better to use POSIX::dup2 directly.
Re^2: How a web server sending data to a CGI perl script ?
by exilepanda (Friar) on Jan 22, 2016 at 02:28 UTC
    It seems you're asking about what incantations to use to put stuff in environment and redirect standard streams
    Yes exactly it is!

    Though your script won't work for me (my fault didn't mention I am on a Win7 box).  <$output_from_child>; will never return. But at least give me a hint to start from a pipe() fork() pairs... But that's okay to leave this homework for me. ( wondering if anything to do about the $SIG{CHLD}, anyway.. )

    Again, thank you very much!=)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found