120 likes | 217 Views
CS 330 Class 8. Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm. Add another format to phone. Programming plan for today: Continuation with CGI Scripts and Perl (not CGI) Environment variables.
E N D
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm. Add another format to phone. Programming plan for today: Continuation with CGI Scripts and Perl (not CGI) Environment variables
CGI Communication Server Machine (1) Client Server SW (5) (4) (2) Other servers CGI Script (3) Other programs (1) Server SW decodes client HTTP request (2) Server SW sets variables and executes CGI script (3) CGI script runs (4) Script returns output with CGI headers to server SW (5) Server translates output and headers into HTTP response to client
Scripts and Perl • First look at scripts not intended to run in CGI • Shell scripts • Perl programs • Shell scripts • In the Unix shell, in our case BASH. • A list of shell (UNIX) commands in a file. • E.g. list on scripts/ #!bin/bash grep perl * | more • Run by typing the bash and the filename: bash list • Perl programs • Put #!/usr/bin/perl on first line, run by: perl name.pl
Variables in Perl Scalars - a string or a number $Name = “fred” or ‘fred’ $Number = 7 name.pl: #!/usr/bin/perl print "What is your name? "; # output to STDOUT, the screen $Name = <STDIN>; # STDIN is the keyboard here chop($name); # removes the carriage return print "Hello $Name\n"; # inserts the value for $Name print 'Goodby $Name\n'; # does not insert the value name1.pl
Where do the CGI Scripts Reside? • /cgi-bin/ if allowed by the system administrator /cgi-bin/ is set via ScriptAlias command: ScriptAlias /cgi-bin/ /home/httpd/cgi-bin/ hello2.htm: <a href /cgi-bin/hello2.pl” > (aliased directory) • Any directory in a file with a .cgi extension: hello.htm: <form action= "../scripts/hello.cgi"> (no alias) • Our convention: scripts is a subdirectory of public_html • The scripts directory must have execute permission (chmod a+x *.cgi)
Logistics Summary • Edit on local computer and telnet or copy to aurora via cat > progname.cgi (paste) CTRL/C • Your programs should • have a .cgi extension • have execute permission • reside in a scripts directory • return an HTML document • Check syntax in telnet: • perl hello.cgi (try this) • note: this displays what is normally sent back to the server
How does data get into a script? • Client passes data to the server as environment variables • Client passes data as part of the URL Similar to data format in results from sendmail scripts/guest.cgi?Name=Fred&Age=25 • Server passes data to the script. Script accesses it via an environment variables.
Environment Variables • Data on the server that is available to the CGI program • e.g. the server name or client address. • These travel with a CGI request in a variable called %ENV • %ENV is an associative array with subscripts such as ‘SERVER_NAME’ • Variable name XXX is accessed through $ENV(‘XXX’) • e.g. $ENV(‘SERVER_NAME’) • Examples: • http://aurora.wells.edu/~cs330/scripts/env.cgi? • http://aurora.wells.edu/~cs330/scripts/env2.cgi?
env.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html>\n"; print "<head><title>About this Server</title></head>\n"; print "<h2>About this Server</h2>\n"; print "<hr><pre>\n"; print "Server name: ",$ENV{'SERVER_NAME'},"<br>\n"; print "Running on port: ",$ENV{'SERVER_PORT'},"<br>\n"; print "Server software: ",$ENV{'SERVER_SOFTWARE'},"<br>\n"; print "Server protocol: ",$ENV{'SERVER_PROTOCOL'},"<br>\n"; print "<hr></pre></html>\n";
env2.cgi #!/usr/local/bin/perl print "Content-type: text/html\n\n"; print "<html>\n"; print "<head><title>About this server</title></head>\n"; print "<body>\n"; print "<h2>Environment Variables</h2>\n"; print "<hr><pre>\n"; foreach $envvar (keys(%ENV)){ print "$envvar: $ENV{$envvar}<br>\n" } print "</pre></body></html>\n"
Data Passed From the Client form1.htm: <form action = "../scripts/form1.cgi" method="GET"> Your name: <input type = "text" name="Your Name"> <input type ="submit" value="Send"> </form> Response to selecting the submit button: http://aurora.wells.edu/~cs330/scripts/form1.cgi?Your+Name=Carol What is sent to the server? (next page) What is sent to the client?
form1.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print "<html>\n"; print "<head><title>Simple Form Processing</title></head>\n"; print "<h2>Simple Form Processing</h2>\n"; print "<hr><pre>\n"; print "The query string: ",$ENV{'QUERY_STRING'},"<br>\n\n"; ($field_name, $data) = split (/=/,$ENV{'QUERY_STRING'}); print "The field: ",$field_name, "\n"; print "The data: ",$data, "<br>\n"; print "<hr></pre></html>\n"; note: split (/=/, aString) separates aString at the first ‘=‘ and assigns the pieces to $field_name and $data