Looking in the Bilge - MVC part 2

Let's start by looking under the deck and examining the basic web site request/response cycle and CGI environment that drives perl based dynamic websites. Requests generally comes in two flavours either requesting a document or sending information to the server to be documented. Needless to say this is very simplistic view of this process but will work for our purposes. The process of asking for a document is referred to as GET'ing a web page and the process of submitting new data to the as server is generally called POST'ing information.
Looking at the other side of this process, the response is generally to display a document requested or confirmation of the data that was posted. It also usually will give you additional navigational links to other documents and/or forms to post more data and continue the cycle.


Once the request is sent to the server it generally interacts with the Common Gateway Interface or CGI.  Good news is Perl has some very simple constructs for accessing the CGI environment. I will be using a basic script to look at these constructs and see how they react to my different requests.

#!/usr/local/bin/perl
use Data::Dumper;

print "Content-type: text/html\n\n";
print "<h2>Perl Web Environment...</h2>\n\n";

print "<h3>GET test </h3>\n";
print "<a href='http://localhost/cgi/info.pl?key=value&number=large value&label=name'>Get Data</a>";

print "<h3>POST test </h3>\n";
print <<EOF;
<form action="http://localhost/cgi/info.pl" method="post">
<input type="text" name="test1"><br>
<input type="text" name="test2"><br>
<input type="text" name="test3"><br>
<input type="submit" value="Post Data">
</form>
EOF

@std = <STDIN>;
print "<h3>Standard Input </h3>\n<pre>";
print Dumper(\@std);
print "</pre><br>\n";

print "<h3>Perl Lib paths (\@INC)</h3>\n<pre>";
print Dumper(\@INC);
print "</pre><br>\n";

print "<h3>Perl Environment Varaibles (\%ENV)</h3>\n<pre>";
print Dumper(\%ENV);
print "</pre><br>\n";

First lets look at Perl's %ENV hash which contains all the environment variables that have been set by the web server. From this information we can tell who and what is making the request and what is being requested or sent.

Perl Environment Varaibles (%ENV)

$VAR1 = {
'SCRIPT_NAME' => '/cgi/info.pl',
'SERVER_NAME' => 'localhost',
'SERVER_ADMIN' => 'root@localhost',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
'HTTP_CONNECTION' => 'keep-alive',
'REQUEST_METHOD' => 'GET',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'SCRIPT_FILENAME' => '/home/webroot/cgi/info.pl',
'SERVER_SOFTWARE' => 'Apache/2.2.22 (Fedora)',
'QUERY_STRING' => '',
'REMOTE_PORT' => '49131',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0',
'SERVER_PORT' => '80',
'SERVER_SIGNATURE' => 'Apache/2.x.xx (distro linux) Server at localhost Port 80',
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.5',
'REMOTE_ADDR' => '192.168.0.20',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'PATH' => '/sbin:/usr/sbin:/bin:/usr/bin',
'REQUEST_URI' => '/cgi/info.pl',
'GATEWAY_INTERFACE' => 'CGI/1.1',
'SERVER_ADDR' => '10.0.0.1',
'DOCUMENT_ROOT' => '/home/webroot/',
'HTTP_HOST' => 'localhost'
};

If I inspect the REQUEST_METHOD, we can tell that the request is GET'ing information from the server. Also since there is no QUERY_STRING you can tell  I'm requesting the data in a default context. Another thing that will be important is to know is where this script will be accessing external libraries and modules from. I can look the @INC predefined variable for this information.

Perl Lib paths (@INC)

$VAR1 = [
'/usr/local/lib/perl5',
'/usr/local/share/perl5',
'/usr/lib/perl5/vendor_perl',
'/usr/share/perl5/vendor_perl',
'/usr/lib/perl5',
'/usr/share/perl5',
'.'
];

Finally if I inspect the STDIN of our script, nothing is seen unless I POST or upload data to the script. In this case the data in then supplied to the perl script via the STDIN construct. With this information we can tell perl to use request information to drive a dynamic website using these various constructs. Up to this point, Im certain this has caused more questions than its answered, but I know now the underpinnings of a CGI request and how Perl sees the information being passed to it. But really don't want to mess with this low level stuff, after all I'm the captain of my own ship. So as one of my shipmates I have selected for this excursion, the Perl's CGI module, will keep me on deck and focused on where im going rather than what is floating around in the bilge.

For the next part of my excursion I would like to get my web server set up to support the routing scheme I have chosen. Secondly I'de like to get the basic entry script together to bootstrap my new MVC app, and then maybe mull over the primary MVC object. Till then happy fishing...   

No comments:

Post a Comment