Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by exilepanda (Pilgrim)
on Jan 21, 2016 at 08:11 UTC ( #1153243=perlquestion: print w/replies, xml ) Need Help??

exilepanda has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am trying to write a pure perl web server and already done writing the server which listening on a port and handling the respond code, setup the %ENV, content type etc. everything correctly ( Socket based ). But the limitation is I have to done everything inside the server for the rest of the query.

Then I found my missing puzzle is how a web server sends *data* to another perl script, because a CGI script don't need to concern what the server is, so I believe there must be a standard way to do that. I did a Google search, but all the results are pointing me to how to interact between HTML form and use CGI, or what kind of standard %ENV variables required, or respond code, sort of.

So my questions are:

  • how do I pass queries to external script in such scenario( with %ENV )?
  • how to make the STDIN ready if that's POST request?

Any clue or reference readings are much appreciated. If that isn't too complex, some example code is most welcome!! =)Thanks in advance!

Replies are listed 'Best First'.
Re: How a web server sending data to a CGI perl script ?
by dsheroh (Prior) 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.

      Um.. Not exactly what I am asking. Say, I fetched a URI from the client wanted "index.pl", then I want to *delegate* the query string , %ENV I've constructed in my server to index.pl for implementation, and I capture the result and return it to the client.

      The meaning I said a CGI script don't need to concern... is that a CGI script can always code like this :

      #!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...

        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

Re: How a web server sending data to a CGI perl script ?
by Anonymous Monk on Jan 21, 2016 at 08:16 UTC
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
      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.
      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!=)

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

      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 ?

        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

        $ 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=

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1153243]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2020-02-26 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (117 votes). Check out past polls.

    Notices?