CGI Presentation

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    CGI Presentation - Presentation Transcript

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

    + Sopan ShewaleSopan Shewale, 3 years ago

    custom

    1541 views, 0 favs, 0 embeds more stats

    CGI - General Presentation

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1541
      • 1541 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 37
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags

    Groups / Events