CGI Scripts: Indian Institute of Technology Kharagpur
CGI Scripts: Indian Institute of Technology Kharagpur
CGI Scripts: Indian Institute of Technology Kharagpur
CGI Scripts
1
Introduction
2
¾icons
This contains the icons that Apache will use
when displaying information or error messages.
¾images
This will contain the image files that will be used
in the web site.
¾logs
This will contain the log files: the access_log
and error_log.
3
“GET” Request Method
4
To Summarize
• For GET
¾Data are read from QUERY_STRING
environment variable.
• For POST
¾Data are read from STDIN.
¾Number of bytes to be read is obtained
from CONTENT_LENGTH.
• Both data available in same format:
var1=value1&var2=value2&……
name=niloy & rollno=7312 & age=24
URL Encoding
5
• The process of decoding back:
¾Separate out the variables.
¾Replace all ‘+’ signs by spaces.
¾Replace all %## with the corresponding
ASCII character.
6
• A point to note:
¾When the server passes data using the
POST method, the scripts checks the
environment variable CONTENT_TYPE.
¾If the value of CONTENT_TYPE is
application/x-www-form-urlencoded
the data needs to be decoded before use.
• Step 1: Initialization
¾Check REQUEST_METHOD.
¾Parse string and extract variables
depending on “GET” or “POST”.
¾Check CONTENT_TYPE, to find out if
the string is URL-encoded.
• Step 2: Processing
¾Process the input data.
¾Output the results (MIME-type header,
and the contents).
7
• Step 3: Termination
¾Release the system resources.
¾Terminate the program.
• CONTENT_LENGTH
¾Length of URL-encoded data in bytes.
• CONTENT_TYPE
¾Specifies the type of data as a MIME header.
• QUERY_STRING
¾Information at the end of the URL after ‘?’.
• REMOTE_ADDR
¾IP address of the client making the request.
• REMOTE_HOST
¾Resolved host name of the client.
8
• REQUEST_METHOD
¾“GET” or “POST”.
• SERVER_NAME
¾Web server’s host name, or IP address.
• SERVER_PROTOCOL
¾Say, HTTP/1.0
• SERVER_PORT
¾Port number on server that received the
HTTP request.
• SCRIPT_NAME
¾Name of the CGI script being run.
Response Header
Content-Type: text/plain
text/html
image/gif
video/avi
9
• A complete MIME header looks like this:
Content-Type: text/plain;
charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Description: Postscript
• Search Engine
• Page-hit Counter
• Student Registration
• On-line Booking of Tickets
• On-line Purchase of Items
• E-mail Gateways
• Feedback Scripts
• Web-based Games
10
Security with CGI Scripts
• An example
¾Suppose that you have a CGI script that
lets users run the “finger” command on
your host.
¾In Perl, there can be a line:
system “finger $username”
¾A malicious user may enter
isg; rm –r /
as the username.
¾The result --- all files will get deleted.
11
Enter UserId isg; rm –r /
12
• What this program does?
¾Sends the contents of a file residing on
the server back to the browser.
• How to invoke?
<A HREF="/cgi-bin/test1.sh?
/home/user1/public_html/text-file.txt">
Click here to activate</A>
$1
Another Example
#!/bin/sh
echo Content-type: text/html
echo ""
13
<SMALL>
<PRE>
EOM
/bin/cat /home/user1/public_html/text-file.txt
CAT << EOM
</PRE>
</SMALL> <P>
</BODY>
</HTML>
EOM
14
E-mail Gateways: an Example
15
Email
Browser Mail Server
Gateway
16
SOLUTIONS TO QUIZ
QUESTIONS ON
LECTURE 18
17
Quiz Solutions on Lecture 18
18
Quiz Solutions on Lecture 18
(0,0) (50,0)
TOP
BOTTOM
(0,50) (50,50)
19
Quiz Solutions on Lecture 18
QUIZ QUESTIONS ON
LECTURE 19
20
Quiz Questions on Lecture 19
21