260 likes | 594 Views
CGI Programming Languages. Web Based Software Development July 21, 2005 Song, JaeHa. Agenda. Introduction CGI Mechanism CGI Languages Alternatives Conclusion. Introduction: Definition. CGI: Common Gateway Interface A simple interface for running gateways In an information server
E N D
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa
Agenda • Introduction • CGI Mechanism • CGI Languages • Alternatives • Conclusion
Introduction: Definition • CGI: Common Gateway Interface • A simple interface for running gateways • In an information server • In Platform-independent manner • Gateway: a program that provides path into internal applications or resources, such as database or file system
Introduction: Conceptual diagram • Script: The software that is invoked by the server according to this interface. • Server: The application program that invokes the script in order to service requests from the client. • Meta-variable: A named parameter which carries information from the server to the script. Client CGI Request Meta-variable Server Scripts Internal services HTTP Request Internal Request HTTP Response CGI Response Internal Response
CGI Mechanisms: Server • As an Application gateway • Receive request from client • Select CGI script to handle request • Convert client request to CGI request • Execute script • Convert CGI response into response for client • Authentication and security handling • Manages special directory such as /cgi-bin • CGI time-out handling • Error page handling
CGI Mechanisms: CGI request • Request meta-variables • sources • Environment variables • STDIN • Command line arguments • CONTENT_TYPE • REQUEST_METHOD • QUERY_STRING • CONTENT_LENGTH • … • Request message body • For POST method
CGI Mechanisms: CGI response • Response header fields • CGI-field • Content-Type • Location • Status • Other-field • Protocol-specific header fields • Extension header fields • Response message body • STDOUT
CGI Mechanisms: Interface ports • Input • STDIN • POST method with 0 < CONTENT_LENGTH • Environment variables • Request meta-variables • Command line argument • ISINDEX query • Output • STDOUT
CGI Languages: Only Perl ? • Almost written in Perl • But, CGI needs only • environment variables • STDIN/STDOUT • command line arguments
CGI Languages: Others • C/C++ • Fortran • TCL • Unix shells • Visual Basic • Apple scripts • Ruby • Etc.
CGI Languages: Choosing Others • Perl is always best ? • Concerns • Productivity • Learning curve • Execution overhead • Etc.
CGI Languages: CGI in C • C for CGI Programming • Well known: Short Learning Curve • Low execution overhead • inconvenient in String handling
CGI Languages: CGI Output in C #include <stdio.h> void main() { /** Print the CGI response header, required for all HTML output.**/ /** Note the extra \n, to send the blank line. **/ printf("Content-type: text/html\n\n") ; /** Print the HTML response page to STDOUT. **/ printf("<html>\n") ; printf("<head><title>CGI Output</title></head>\n") ; printf("<body>\n") ; printf("<h1>Hello, world.</h1>\n") ; printf("</body>\n") ; printf("</html>\n") ; exit(0) ; }
CGI Languages: CGI I/O in C #include <stdio.h> … main(int argc, char *argv[]) { entry entries[MAX_ENTRIES]; register int x,m=0;int cl;printf("Content-type: text/html%c%c",10,10);if(strcmp(getenv("REQUEST_METHOD"),"POST")) { …}if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) { printf("This script can only be used to decode form results. \n"); exit(1);}cl = atoi(getenv("CONTENT_LENGTH"));for(x=0;cl && (!feof(stdin));x++) { … } … }
CGI Languages: CGI in Ruby • Ruby for CGI Programming • Packages for Productivity improvement • CGICGI::CookieCGI::HtmlExtensionCGI::QueryExtensionCGI::SessionCGI::Session::FileStoreCGI::Session::MemoryStoreCGI::Session::PStore • Stylish • Under development • Small user group
CGI Languages: CGI in Ruby #!/usr/bin/env ruby print "HTTP/1.0 200 OK\r\n“ print "Content-type: text/html\r\n\r\n“ print "<html><body>Hello World!</body></html>\r\n" … require "cgi“ cgi = CGI.new value = cgi['field_name'] fields = cgi.keys cgi.has_key?('field_name') cgi.include?('field_name') …
Alternatives: SSI/Servlets/Server API • SSI • Server Side Includes • Simple interface for basic dynamic content • Non-standard • Servlets • an alternative API for JAVA • overcomes the limitation of JAVA not supporting environment variables • Server API • Generally the most powerful and most complex option • Non portable • Varies from server to server
Alternatives: Servlets/JSP • Kinds of CGI scripts • Scripts: … It need not be a standalone program, but could be a dynamically loaded or shared library or even a subroutine in the server. • Servlets: Thread instead of process • Lightweight : less overhead in execution • Sharing states: i.e. session information • Crash propagation • Cold deploy • JSP: reversion between hosts and guests • Contents oriented instead of control oriented • Less ‘”’ • Easy to read • Hot deploy
Alternatives: CGI Pros and Cons • Advantages • Separation of concerns • Server for client request handling • Scripts for application issues • Simple • Drawbacks • Performance • Scalability • Maintainability • Security
Conclusion • Common Gateway Interface • A simple interface for information server extension • For simple dynamic web programming • Any languages are possible when they supports minimum set of mechanisms • STDIN/STDOUT • Environment variables • Able to invoke in Information server • Mainly in Perl
References • CGI Programming RFC Page: • http://www.faqs.org/rfcs/rfc3875.html • W3C’s CGI Pages: • http://www.w3.org/CGI • CGI FAQ • http://www.htmlhelp.com/faq/cgifaq.1.html • Another CGI FAQ: • http://www.webthing.com/tutorials/cgifaq.html • Dynamic Programming Language Choice Criteria: • http://www.apacheweek.com/features/dynamicpages • Ruby CGI: • http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/ • Web Programming Languages Introduction: • http://www.freelance-help.com/web-programming.php