Re: How a web server sending data to a CGI perl script ?
by dsheroh (Monsignor) on Jan 21, 2016 at 08:44 UTC
|
a CGI script don't need to concern what the server is, so I believe there must be a standard way to do that.
Indeed there is a standard way to do that: The Common Gateway Interface, usually referred to by its initials, "C.G.I.".
If I understand the question correctly, you're writing a web server and want to be able to call scripts via CGI, but you don't know what the calling side of the interface is supposed to do. If my understanding is correct, then you should probably take a look at the CGI spec, aka RFC 3875 - The Common Gateway Interface (CGI) Version 1.1.
| [reply] |
|
|
#!perl
print "Content-Type: text/html\n\n";
print $ENV{REMOTE_ADDR};
The script don't need to care about the server is IIS or Apache...
| [reply] [d/l] |
|
|
It is exactly what you're asking. A server implementing CGI uses %ENV to communicate HTTP headers and other basic values (document_root)
STDIN is used for the BODY of the HTTP request
If this doesn't answer your question ... ask a different question please
| [reply] |
|
|
|
|
|
|
|
|
Re: How a web server sending data to a CGI perl script ?
by Anonymous Monk on Jan 21, 2016 at 08:16 UTC
|
| [reply] |
Re: How a web server sending data to a CGI perl script ?
by Anonymous Monk on Jan 21, 2016 at 12:59 UTC
|
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
| [reply] [d/l] [select] |
|
|
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';
}
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] [select] |
|
|
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!=)
| [reply] [d/l] [select] |
Re: How a web server sending data to a CGI perl script ?
by flexvault (Monsignor) on Jan 22, 2016 at 17:52 UTC
|
Hello exilepanda,
I re-read your posts several times, and I think this is what you're trying to do:
WebServer --> Client --> 2nd Client --> %ENV input
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . V
WebServer <-- Client <-- 2nd Client <-- HTML output
You want the WebServer to call a Client (CGI-script) to send the 'Todo' work to a 2nd Client ( Perl script ) that actually does the processing and then send the result (HTML output) back to the WebServer.
If this isn't what you want to do, then don't bother reading the rest of my post :-)
I have done this where we used Apache2 web-servers to call a small cgi-script which sent/received the data over sockets to a remote server(s) to process the 'Todo' work. If you do a super search on "Persistent Perl", you may get some ideas on "how" to accomplish this.
If this is what you're looking for, then you can ask some better questions and get some much better answers.
Hope this helps...Ed
Regards...Ed
"Well done is better than well said." - Benjamin Franklin
| [reply] |
|
|
Thanks Ed, I might want to do the things as your described ( 2nd client ), but the fact is that I already stuck at the first client.
From a CGI script perspective, when it's triggered by the server, the script will naturally have all relevant %ENV variables like REMOTE_ADDR, COOKIE, REQUEST_METHOD etc.
So that my very root question is how these $ENV variables are sent to the CGI script before the first line of code in the CGI script is run ?
| [reply] |
|
|
Hello exilepanda,
If you do it the way I described, the %ENV variables are already set-up for you. Do not touch the %ENV, but copy the %ENV to a temp hash. And remember you still need the GET / POST data ( the actual $Todo work ) from the WebServer. ( Example ).
my $Todo = ...; # Data sent via GET or POST from browser via We
+bServer
my $environment = '';
foreach my $env ( keys %ENV )
{ $environment .= "$env\t$ENV{$env}\n"; }
my $send = pack("N",length($environment) ) . $environment . pack(
+"N",length($Todo) ) . $Todo;
my $ret = send_data( $svr, \$send ); # Send to processing Perl s
+cript
Now the second client has the environment of the WebSever, and it's own %ENV from Perl. The processing client uses %TempENV for WebServer info and it's own %ENV if it needs it.
It's clean and you don't have to get into the error handling of the WebServer. Good Luck!
Regards...Ed
"Well done is better than well said." - Benjamin Franklin
| [reply] [d/l] |
|
|
$ cat shabba
for my $key ( grep /perl/i , keys %ENV ){ print qq{ $key $ENV{$key}\n}
+; }
$ perl shabba
PERL_MB_OPT --config installhtmldir= --binhtml=
$ perl -e " $ENV{PERL_SHABBA}=666; system $^X, q{shabba}; "
PERL_SHABBA 666
PERL_MB_OPT --config installhtmldir= --binhtml= --config installht
+mldir= --binhtml=
$ perl shabba
PERL_MB_OPT --config installhtmldir= --binhtml=
| [reply] [d/l] |
|
|
|
|