1 CGICGI Common Gateway Interface Server-side Programming Lecture.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Advertisements

The Web: an architectural view. Browser Render HTML Get URL Send HTML Get HTML file HTTPD File System The primitive Web model.
J0 1 Marco Ronchetti - The Web: an architectural view.
Lecture plan Information retrieval (from week 11)
Adding Dynamic Content to your Web Site
DT228/3 Web Development WWW and Client server model.
Introduction to Model-View-Controller (MVC) Web Programming with TurboGears Leif Oppermann,
DT211/3 Internet Development Application Internet Development Application.
PZ15A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ15A - The Internet Programming Language Design and.
Active Server Pages Chapter 1. Introduction Understand how browsers and servers interacted when the Web was young Understand what early Internet and intranet.
And so on CGI programming Web Services Java Programs for the Web.
CGI and Perl MSc Publishing on the WWW. What is CGI ? (1) User Buying and selling Playing games Customised web pages Developer Means to run external programs.
WWW and Internet The Internet Creation of the Web Languages for document description Active web pages.
E-Commerce The technical side. LAMP Linux Linux Apache Apache MySQL MySQL PHP PHP All Open Source and free packages. Can be installed and run on most.
Quick Tour of the Web Technologies: The BIG picture LECTURE A bird’s eye view of the different web technologies that we shall explore and study.
WEB DESIGN SOME FOUNDATIONS. SO WHAT IS THIS INTERNET.
Server Side Scripting Norman White. Where do we do processing? Client side – Javascript (embed code in html) – Java applets (send java program to run.
RIA Introduce Comparison among several technology.
ITM352 PHP and Dynamic Web Pages: Server Side Processing.
INTRODUCTION TO WEB DATABASE PROGRAMMING
RIA & Adobe Flex Yunhui Fu 11/05/2008. What’s RIA RIA (Rich Internet Applications) –web applications which look and perform like desktop applications.
Chapter 1: Introduction to Web
Chapter 16 The World Wide Web Chapter Goals Compare and contrast the Internet and the World Wide Web Describe general Web processing Describe several.
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Introduction to Internet Programming (Web Based Application)
Chapter 17 - Deploying Java Applications on the Web1 Chapter 17 Deploying Java Applications on the Web.
Lecture 19 Web Application Frameworks Boriana Koleva Room: C54
Instructor, Dr. Khalili Bahram Jeevan Kumar Gogineni.
20-753: Fundamentals of Web Programming 1 Lecture 1: Introduction Fundamentals of Web Programming Lecture 1: Introduction.
Working with scripts. HTTP Was too limited Not dynamic.
HTML. Principle of Programming  Interface with PC 2 English Japanese Chinese Machine Code Compiler / Interpreter C++ Perl Assembler Machine Code.
Java CGI Lecture notes by Theodoros Anagnostopoulos.
Putting it all together Dynamic Data Base Access Norman White Stern School of Business.
Web Pages with Features. Features on Web Pages Interactive Pages –Shows current date, get server’s IP, interactive quizzes Processing Forms –Serach a.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Chapter.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Introduction to ASP.NET1. 2 Web applications in general Web applications are divided into two parts –The server part –The client part The server part.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
Overview Web Session 3 Matakuliah: Web Database Tahun: 2008.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Web Pages with Features. Features on Web Pages Interactive Pages –Shows current date, get server’s IP, interactive quizzes Processing Forms –Serach a.
Rich Internet Application
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
Session 1 Chapter 1 - Introduction to Web Development ITI 133: HTML5 Desktop and Mobile Level I
Ajax for Dynamic Web Development Gregory McChesney.
Web Technologies Lecture 8 Server side web. Client Side vs. Server Side Web Client-side code executes on the end-user's computer, usually within a web.
Scripting Languages Client Side and Server Side. Examples of client side/server side Examples of client-side side include: JavaScript Jquery (uses a JavaScript.
 Web pages originally static  Page is delivered exactly as stored on server  Same information displayed for all users, from all contexts  Dynamic.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
Albert F. Cervantes, M.S. California State University, Los Angeles
The Internet Salihu Ibrahim Dasuki (PhD) CSC102 INTRODUCTION TO COMPUTER SCIENCE.
1 The Internet Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CS320 Web and Internet Programming Introduction to Web Application Development Chengyu Sun California State University, Los Angeles.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
CX Introduction to Web Programming
Tonga Institute of Higher Education IT 141: Information Systems
Chengyu Sun California State University, Los Angeles
Section 6.3 Server-side Scripting
ITM352 PHP and Dynamic Web Pages: Server Side Processing 1.
Web Concepts Lesson 2 ITBS2203 E-Commerce for IT.
Development of Web Applications – Introduction revisited
Dongwon Lee, Ph.D. IST 516 Fall 2011
Chengyu Sun California State University, Los Angeles
Tonga Institute of Higher Education IT 141: Information Systems
WPS - your story so far Seems incredible complicated, already
Chengyu Sun California State University, Los Angeles
Architecture of the web
Chengyu Sun California State University, Los Angeles
Presentation transcript:

1 CGICGI Common Gateway Interface Server-side Programming Lecture

Rich Internet Applications An RIA is a web application that provides the client with the features and functionality of desktop applications Requires transferring the processing from the server to the client Javascript is one enabling technology for a RIA

RIAs in the Internet client-server model Client (browser) Web server Client does all the processing (eg play videos as they come in) Data (eg multimedia) stay on the server HTTP request for resource Server sends code but keeps data

Some technologies that support RIA development Javascript (and associated ECMA dialects) Adobe Flash  Flash player and IDE Java Applets and Java Webstart (see later) AJAX  Asynchronous JavaScript and XML

Server-Side Programming Lots of programs/applications designed to run on the machines on which they are installed How can a remote client request access to these?

CGI programming CGI => Common Gateway Interface  A protocol for interfacing local applications with a web server Sequence of steps  Client sends URL request  Program runs at the server side  Output is collected and sent back to the client  Often the output is an HTML “built” by the server

CGI using HTML and C language Why do we need CGI?  To read the information on the forms (HTML)  To build a customised HTML response to users To understand the concept lets use C at first... CGI is completely independent of the language and OS CGI is implemented in (almost) all webservers

CGI programs can be written in any language supported by the server. This includes compiled programming languages, such as C and C++; interpreted languages, such as Perl, Python, Ruby, and languages, such as Java, that lie somewhere in between.

Hello World! #include using namespace std; int main(void) { cout << "Content-Type: text/html;charset=us-ascii\n\n"; /** Print the HTML response page to STDOUT. **/ cout \n"; cout CGI Output \n"; cout \n" ; cout Hello, world. \n"; cout << "this is my first CGI" << "\n"; cout \n"; return 0; } Compile, then place the executable inside cgi-bin directory of xitami Test using a browser, URL:

How to submit data using forms GET GET   Web server has a special directory called cgi-bin  Two variables: var1=1 var2=4 Special characters are encoded  ~ %7E  ~ would be encoded as %7E (% followed by ASCII code)

GET So variables from the forms go on URL The environment variable is:  $QUERY_STRING Most browsers limit the size of URLs (256 chars, some more, e.g., IE is 2083 chars) POST When you have too much data, use POST instead...

HTML Multiply example – the HTML file get <form method=" get " action=" Number 1: Number 2:

Multiply example Action=" multiply is an executable under:/var/www/cgi-bin/ with x permissions for all! Variables in URL:  After submission, URL becomes: 

Example SERVER-SIDE: Response CLIENT-SIDE

Multiply example – the C file #include #include //for Windows operating system – Sleep() int main(void) { char *data; long m,n; printf("%s%c%c\n","Content-Type:text/html;charset=iso ",13,10); printf(" Multiplication results \n"); data = getenv("QUERY_STRING");//here it is your data!!! if(data == NULL) printf(" Error!"); else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2)//check for 2 inputs printf(" Error! Invalid data."); else printf(" %ld * %ld = %ld.",m,n,m*n); //Sleep(1000); // uncomment that to see who runs the process... return 0; } //from (July2010)

sscanf() Recall the sscanf() function in C On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned. int sscanf ( const char * str, const char * format,...); Read formatted data from string

char * getenv ( const char * name ); Get environment string Retrieves a C string containing the value of the environment variable whose name is specified as argument. If the requested variable is not part of the environment list, the function returns a NULL pointer. The string pointed by the pointer returned by this function shall not be modified by the program. The same memory location may be used in subsequent calls to getenv, overwriting the previous content. getenv() getenv() function in C

char * fgets ( char * str, int num, FILE * stream ); Get string from stream Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first. A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str. A null character is automatically appended in str after the characters read to signal the end of the C string. fgets() fgets() function in C

POST (GET was originally used only to get data from server) data is passed via standard input stream (stdin) the length (in bytes) of the data passed via $CONTENT_LENGTH. If the program reads more than the length, ...unpredictable behaviour may happen!

Multiply example – the HTML file post <form method=" post " action=" Number 1: Number 2:

Multiply with POST – C file...#define MAXLEN 80 int main(void) { char *lenstr; char input[MAXLEN]; long m,n, len; printf("%s%c%c\n","Content-Type:text/html;charset=iso ",13,10); lenstr = getenv("CONTENT_LENGTH"); if(lenstr == NULL || sscanf(lenstr,"%ld",&len)!=1 || len > MAXLEN) printf(" There was an error in the content sent to Apache."); else { fgets(input, len+1, stdin); printf(" Form received by Apache. "); printf("The form contains %ld bytes. ",len); printf(" Apache received this: %s ",input); if(sscanf(input,"m=%ld&n=%ld",&m,&n)!=2) printf(" An error occurred, both variables must be numeric."); else printf(" %ld * %ld = %ld. ",m,n,m*n); } return 0; } //adapted from (July2010)

Self-generating form in C #include int main(void) { char *data; long m,n; printf("%s%c%c\n","Content-Type:text/html;charset=iso ",13,10); printf(" Multiplicand 1: Multiplicand 2: "); printf(" Multiplication results "); data = getenv("QUERY_STRING"); if(data == NULL) printf(" Error! Error in passing data from form to script."); else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2) printf(" Error! Invalid data. Data must be numeric."); else printf(" The product of %ld and %ld is %ld.",m,n,m*n); return 0; }

Self-generating form in C #include int main(void) { char *data; long m,n; static int flag=0; printf("%s%c%c\n","Content-Type:text/html;charset=utf-8",13,10); getmultiply2_utf8 printf(" Multiplicand 1: Multiplicand 2: "); printf(" Multiplication results "); getenv data = getenv("QUERY_STRING"); if(data == NULL) { if( !flag ){ printf(" nothing to compute yet."); } else { printf(" Error! Error in passing data from form to script."); } sscanf } else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2) { printf(" Error! Invalid data. Data must be numeric."); } else { printf(" The product of %ld and %ld is %ld.",m,n,m*n); flag = 1; } return 0; }

Handling Special Characters decode void decode(char *src, char *last, char *dest){ for(; src != last; src++, dest++) if(*src == '+') *dest = ' '; else if(*src == '%') { int code; if(sscanf(src+1, "%2x", &code) != 1) code = '?'; *dest = code; src +=2; } else *dest = *src; *dest = '\n'; *++dest = '\0'; }

Problems with CGI Each a time request is made, a new process is spawned on the server This can quickly overwhelm sites that get a large number of hits One solution is to install libraries directly callable by the web server mod_perl mod_python

CGI can be inefficient... The executable is loaded in the server's memory every time it is called Multiple copies API would be more efficient...  Bad idea to do that using C/C++  Unstable environment (crash the entire server) Apache offers modules with Perl and Python APIs Scripting languages such as ASP and PHP

Security problems with CGI Program is running in your server... Suppose you want the user to run:  system "whois $username" ; But what if the user actually sends:  "john; rm -rf "  system "whois john; rm -rf " ; The administrator: “Oh dear!Where are all my files?” In Linux For Windows,

Extra Windows

Sample Result

Server-side programming Better to use a language specially designed for server-side programming See PHP programming next...

References