CGI Presentation

74,479 views

Published on

CGI - General Presentation

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
74,479
On SlideShare
0
From Embeds
0
Number of Embeds
49
Actions
Shares
0
Downloads
281
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

CGI Presentation

  1. 1. CGI Sopan Shewale , sopan.shewale@gmail.com
  2. 2. A few Assumptions <ul><li>Web Application </li></ul><ul><li>Web Server </li></ul><ul><li>Web Browser </li></ul><ul><li>HTML </li></ul><ul><li>Static Pages </li></ul><ul><li>Dynamic Pages </li></ul>
  3. 3. HTTP short discussion <ul><li>URL </li></ul>http:// www.persistentsys.com :80 /cgi/calender.cgi ? month=jan # week2 Scheme Host port Path Query Fragment <ul><li>Request/Response Cycle </li></ul>
  4. 4. HTTP short discussion (Cont…) <ul><li>First Line of http request contains “method”, “resource”, and version string of protocol. </li></ul><ul><ul><li>Request Methods can be “GET”, “HEADER”, “POST”, “PUT”, “DELETE”, “CONNECT”, “OPTIONS”, “TRACE” </li></ul></ul><ul><ul><li>PUT and DELETE are not used in CGI </li></ul></ul><ul><ul><li>GET AND POST are best friends of CGI </li></ul></ul>
  5. 5. HTTP short discussion (Cont…) <ul><li>A few words about GET and POST </li></ul><ul><ul><li>GET: </li></ul></ul><ul><ul><ul><li>is the standard (intended) method to retrieve a document via HTTP on the WEB, should not have any side-effects. </li></ul></ul></ul><ul><ul><ul><li>Back-button issues because of GET request </li></ul></ul></ul><ul><ul><ul><li>You can notice query parameters in URL </li></ul></ul></ul><ul><ul><li>POST: </li></ul></ul><ul><ul><ul><li>Used with HTML forms to submit information </li></ul></ul></ul><ul><ul><ul><li>Always included a body containing the submitted information </li></ul></ul></ul><ul><ul><ul><li>You can not notice query parameters in URL </li></ul></ul></ul>
  6. 6. CGI: What is that? <ul><li>HTTP is the common language that web browsers and web servers use to communicate with each other on the internet </li></ul><ul><li>CGI is a specification for transferring information between a Web Server and a CGI program </li></ul><ul><li>CGI is most common way for web servers to interact dynamically with Users </li></ul><ul><li>CGI scripts help Web Applications to create the Dynamic Pages </li></ul>
  7. 7. How a CGI Applications Work? CGI Application User Web Browser 1 2 3 4 HTTP Request HTTP Response CGI Program’s Response Call CGI Server Application (on Server)
  8. 8. How a CGI Applications Work? <ul><li>When Web Server gets a request for a CGI script, the web server executes the CGI Script as an another process. </li></ul><ul><li>The Web Server Passes some parameters and collects the output. </li></ul><ul><li>The Output is sent back to the browsers just as it had been fetched from a static file. </li></ul>
  9. 9. Alternatives to CGI <ul><li>Many Alternatives appeared-Some of them </li></ul><ul><ul><li>Attempt to avoid the drawback of: Creating a separate process to execute the script every time it is requested. </li></ul></ul><ul><ul><li>Try to make a less of a distinction between HTML pages and code by moving code into HTML pages itself. </li></ul></ul><ul><li>List goes as follow: </li></ul><ul><ul><li>ASP: Created by Microsoft, ASP engine is integrated into the web server so it does not require to an additional process. </li></ul></ul><ul><ul><li>PHP: Programming Langauge, similar to Perl, supports embedded code within HTML pages . </li></ul></ul>
  10. 10. Alternatives to CGI (cont…) <ul><li>List goes as follow: (cont…) </li></ul><ul><ul><li>Java servlets: Use Java Technology, so must be compiled as Classes before they are run, interface is quite different that CGI. </li></ul></ul><ul><ul><li>Mod_perl: Is a module for Apache web Server, avoids creating new instance of perl process for each CGI request. Mod_perl embeds the perl interpreter into web server-gives performance advantage. </li></ul></ul>
  11. 11. Configuring Apache to Permit CGI <ul><li>ScriptAlias </li></ul><ul><ul><li>“ ScriptAlias” directive tells apache that a particular directory is set aside for CGI Programs. Apache assumes that every file in that directory is a CGI program </li></ul></ul><ul><ul><li>The ScriptAlias directive looks as follow: </li></ul></ul><ul><li>Explicitly using Options to permit CGI Execution </li></ul><ul><ul><li>One can explicitly use the Options directive, inside main configuration of Apache to specify the CGI execution is permitted in a particular directory: </li></ul></ul>ScriptAlias /cgi-bin/ /usr/local/apache/cgi-bin / <Directory /usr/local/apache/htdocs/somedir> Options +ExecCGI </Directory >
  12. 12. CGI Environment <ul><li>CGI scripts generally executed with limited permission </li></ul><ul><li>CGI Scripts are given predefined environment variables that provide information about web server and client </li></ul><ul><ul><li>For Perl, it’s available through %ENV hash </li></ul></ul><ul><li>Example of Environment variables </li></ul><ul><ul><li>SERVER_NAME: The servers hostname/ip </li></ul></ul><ul><ul><li>SERVER_PROTOCOL: The name and version of the protocol. </li></ul></ul><ul><ul><li>SERVER_PORT: The port number to which request was sent </li></ul></ul><ul><ul><li>REQUEST_METHOD: The method with witch the request was made </li></ul></ul><ul><ul><li>PATH_INFO: </li></ul></ul><ul><ul><li>SCRIPT_NAME </li></ul></ul><ul><ul><li>QUERY_STRING </li></ul></ul>
  13. 13. CGI Environment (Cont…) <ul><li>Example of Environment variables (cont…) </li></ul><ul><ul><li>REMOTE_HOST </li></ul></ul><ul><ul><li>REMOTE_ADDR </li></ul></ul><ul><ul><li>AUTH_TYPE: if the server supports user authentication </li></ul></ul><ul><ul><li>REMOTE_USER: If the server supports users authentication, and the script is protected, this is the username they have authenticated as. </li></ul></ul><ul><ul><li>CONTENT_TYPE </li></ul></ul><ul><ul><li>CONTENT_LENGTH </li></ul></ul>
  14. 14. Perl <ul><li>CGI applications can be developed almost in any programming language, but Perl is popularly used </li></ul><ul><ul><li>Perl is easy to Learn (at lease people say) </li></ul></ul><ul><ul><li>Easily portable and available on many platforms </li></ul></ul><ul><ul><li>Extremely powerful string manipulation (RE is powerful) </li></ul></ul><ul><ul><li>Countless Open Source Modules available on CPAN ( http://www.cpan.org ) </li></ul></ul>
  15. 15. CGI.pm module <ul><li>Provides interface for most of the common CGI tasks. </li></ul><ul><li>Helps for </li></ul><ul><ul><li>Parsing input parameters </li></ul></ul><ul><ul><li>outputting headers and a powerful/elegant way to output HTML code from scripts. </li></ul></ul><ul><ul><li>Handling Errors </li></ul></ul><ul><li>Also supports Object Oriented Syntax </li></ul><ul><li>Simple Example: </li></ul>#!/usr/bin/perl use strict; use warnings; use CGI; my $q = new CGI(); print $q->header(&quot;text/html&quot;), $q->start_html(&quot;Welcome&quot;), $q->p(&quot;Welcome to the world of CGI&quot;), $q->end_html();
  16. 16. Handling Input with CGI.pm <ul><li>$cgi->auth_type, $cgi->content_type, etc are methods to get information about environment </li></ul><ul><li>$cgi->param is the method to access parameters submitted to script-applies for both POST and GET method. </li></ul><ul><li>Modifying parameters is also possible (sometimes extremely useful) </li></ul><ul><ul><li>$cgi->param( title=>”Sr. Member of Tech Staff”); </li></ul></ul><ul><ul><li>$cgi->delete(“title”); </li></ul></ul><ul><ul><li>$cgi->delete_all; </li></ul></ul>
  17. 17. Handling Output with CGI.pm <ul><li>Extremely useful for outputting headers and HTML with Perl </li></ul><ul><li>Headers </li></ul><ul><ul><li>$cgi->header(-type=>”text/html”, -status=>”404 Not found”); </li></ul></ul><ul><ul><li>$cgi->header(-type=>”text/html”, -expires=>”+30m”); </li></ul></ul><ul><li>HTML </li></ul><ul><ul><li>Forms </li></ul></ul><ul><ul><li>Tables </li></ul></ul><ul><ul><li>start_html/end_html etc </li></ul></ul>
  18. 18. Handling Errors with CGI.pm <ul><li>Many times things does not work as planned </li></ul><ul><li>One can use CGI::Carp (included with CGI.pm) </li></ul><ul><ul><li>For trapping the abnormal exists/crashes of the script </li></ul></ul><ul><ul><li>Displaying informative message on browser </li></ul></ul>
  19. 19. Maintaining State <ul><li>HTTP is a stateless protocol </li></ul><ul><li>The series of interactions that a particular user has with our site is a session. </li></ul><ul><li>The Client must pass unique identifier with each request </li></ul><ul><ul><li>Using request line </li></ul></ul><ul><ul><li>Using header line </li></ul></ul><ul><ul><li>Using content(in case of post method) </li></ul></ul><ul><li>Possible methods </li></ul><ul><ul><li>Query strings and extra path information </li></ul></ul><ul><ul><li>Hidden Fields </li></ul></ul><ul><ul><li>Client Side Cookies (CGI::Session, CGI::Cookie modules are extremely useful to do this) </li></ul></ul>
  20. 20. Authentication and Identification <ul><li>Authentication </li></ul><ul><ul><li>Can be supported by Web Servers </li></ul></ul><ul><ul><li>When used with Cookies/Sessions – Application can help with Authentication </li></ul></ul><ul><li>Identification </li></ul><ul><ul><li>If Web Servers handle Authentication </li></ul></ul><ul><ul><ul><li>REMOTE_USER variable can be used to identify the user </li></ul></ul></ul><ul><ul><li>If Applications handle creation of Sessions/Cookie </li></ul></ul><ul><ul><ul><li>While authentication is done, User Name can be identified and set into the session at Server Side. </li></ul></ul></ul>
  21. 21. Example <ul><li>Application which demonstrates CGI </li></ul><ul><ul><li>Click Increase or Decrease button and track the count </li></ul></ul><ul><ul><li>Explain Back Button Issues </li></ul></ul><ul><ul><li>Reload Issues </li></ul></ul><ul><ul><li>Explain Cookie/Session-by walkthrough the code. </li></ul></ul>
  22. 22. Example: Count Click Script #!/usr/bin/perl # ######################### ## Author: Sopan Shewale ### This script is created for giving demo on click count. ## The Script is support to display increse/decrease click's, handles back button of browser, does not handle reload stuff. ## also it's based on sessions. ######################## use strict; use warnings; use CGI; use CGI::Session; use CGI::Cookie; my $q = new CGI(); my $sessionid = $q->cookie(&quot;CGISESSID&quot;) || undef; my $session = new CGI::Session(undef, $sessionid, {Directory=>'/tmp'}); $sessionid = $session->id(); my $cookie = new CGI::Cookie(-name=>'CGISESSID', -value=>$sessionid, -path=>&quot;/&quot;); print $q->header('text/html', -cookie=>$cookie); print $q->start_html(&quot;Welcome to Click Count Demo&quot;); print &quot;<h1>Welcome to Click Count Demo</h1>&quot;; my $count = $session->param('count'); ## count-is click count variable if(!defined($count)) { $session->param('count', 0); $count=0;} ### if session is first time created, set count=0 $session->param('count', $count); $count = $session->param('count'); #print &quot;<h1>The Click Count is: $count &quot;; ## Form stuff print $q->startform(-method=>'POST'); print $q->submit( -name=>&quot;Increase&quot;, -value=>'Increase1'); print $q->submit( -name=>&quot;Decrease&quot;, -value=>'Decrease1'); print $q->endform();
  23. 23. Example: Count Click Script (Cont…) ## Which button is being pressed my $which_button = $q->param('Increase'); if(defined ($which_button)) { print &quot;Increase pressed&quot;; $count = increase_count($count); ## Increase the count since increase button is clicked $session->param('count', $count); }else { $which_button=$q->param('Decrease'); if(defined($which_button)){ print &quot;Decrease pressed&quot;; $count = decrease_count($count); ## Decrease the count since decrease button is clicked $session->param('count', $count); } else {print &quot;You have not pressed any button, seems you are typing/re-typing the same URL&quot;; } } $count = $session->param('count'); print &quot;<h1>The Click Count is: $count &quot;; print $q->end_html(); ## increases the count by 1 sub increase_count { my $number = shift; $number = $number +1; return $number; } ## decreases the count by 1 sub decrease_count { my $number = shift; $number = $number -1; return $number; }
  24. 24. Example of Back- button Issue
  25. 25. What Next? <ul><li>If you want to stick to Perl </li></ul><ul><ul><li>CGI::Application – Web Development Framework which Use Perl, CGI </li></ul></ul><ul><ul><li>Catalyst- Web Framework-can also work with FastCGI or mod_perl </li></ul></ul><ul><li>If you are free to move into Other Technologies </li></ul><ul><ul><li>Many choices are there. </li></ul></ul><ul><ul><li>Have a look at http://www.theserverside.com/tt/articles/article.tss?l=ArtOfWebDevBookReport </li></ul></ul><ul><ul><li>Have a look at http://www-28.ibm.com/developerworks/web </li></ul></ul><ul><ul><li>Search Google  </li></ul></ul>
  26. 26. References <ul><li>Presentation by Lincol Stein - http://stein.cshl.org/~lstein/talks/cgipm/Sld001.htm </li></ul><ul><li>Book : CGI Programming with Perl by Scott Guelich, Shishir Gundavaram etc </li></ul><ul><li>Nice Tutorial : http://inconnu.islug.org/~ink/perl_cgi/index.html </li></ul><ul><li>CGI Specifications: http://hoohoo.ncsa.uiuc.edu/cgi/interface.html </li></ul><ul><li>How the Web Works: HTTP and CGI Explained - http://www.garshol.priv.no/download/text/http-tut.html </li></ul><ul><li>Where the Web Leads us, article by Tim O’Reilly http://www.xml.com/pub/a/1999/10/tokyo.html </li></ul><ul><li>Track Tim O’Reilly - http://www.oreilly.com/ </li></ul>
  27. 27. Thank you For any queries write to Email: sopan.shewale@gmail.com

×