MSc Internet Computing CSYM010 Server Side Software Lecturer: Espen Svennevik
Content Web Servers Common Gateway Interface Server side scripting languages
Scheme of work Tuesday 7th Feb Webservers, CGI & server side scripting languages- PHP 14th Feb PHP 21st Feb PHP & MySQL 28th Feb 7th March JSP & JDBC or AJAX 14th March ASSIGNMENT 21st March ASSIGNMENT – Hand into AHO by Friday 25th March Easter Break
Webserver Apache - Multiplatform Microsoft IIs Jakarta Tomcat HTTP Protocol Typically TCP Port 80
HTTP HyperText Transaction Protocol Version 1.0 or 1.1 Simple text based protocol Client requests Server responds (possibly with an error)
HTTP continued - Example Client: GET /~espen/CSYM010/test.html HTTP/1.0 Server: HTTP/1.0 200 OK Or HTTP/1.0 404 Not Found
HTTP continued - Example espen:~ espen$ telnet www.eng.nene.ac.uk 80 Trying 194.81.104.27... Connected to my23-1.eng.nene.ac.uk. Escape character is '^]'. GET /~espen/CSYM010/test.html HTTP/1.0 HTTP/1.1 200 OK Date: Tue, 07 Feb 2006 10:36:14 GMT Server: Apache/2.0.53 (Unix) PHP/4.3.10 Last-Modified: Tue, 07 Feb 2006 10:36:15 GMT ETag: W/"90db-16-624db300" Accept-Ranges: bytes Content-Length: 22 Connection: close Content-Type: text/html X-Pad: avoid browser bug <b><i>CSYM0101</i></b>Connection closed by foreign host. espen:~ espen$
HTTP continued Common status codes 200 OK The request succeeded, and the resulting resource (e.g. file or script output) is returned in the message body. 404 Not Found The requested resource doesn't exist. 301 Moved Permanently 302 Moved Temporarily 303 See Other (HTTP 1.1 only) The resource has moved to another URL (given by the Location: response header), and should be automatically retrieved by the client. This is often used by a CGI script to redirect the browser to an existing file. 500 Server Error An unexpected server error. The most common cause is a server-side script that has bad syntax, fails, or otherwise can't run correctly.
HTTP continued Other methods apart from GET are: POST - Similar to GET HEAD - Gets header only - no body
Common Gateway Interface (CGI) Interface to server side scripts Client (browser) request a script Server executes scripts Output of scripts are returned to the client Client (browser) displays (renders) output
CGI Example Written in UNIX shell #!/bin/sh echo "Content-Type: text/html" echo NOW=`date` echo "<h1>Todays date and time is $NOW</h1>"
CGI Example - continued Output Content-Type: text/html <h1>Todays date and time is Tue Feb 7 11:48:09 GMT 2006</h1>
Second CGI example #!/bin/sh echo "Content-Type: text/html” echo for i in 1 2 3 4 5 6 do echo "<h$i>Header $i</h$i>” done
Second CGI example - cont’ Content-Type: text/html <h1>Header 1</h1> <h2>Header 2</h2> <h3>Header 3</h3> <h4>Header 4</h4> <h5>Header 5</h5> <h6>Header 6</h6>
Apache environment variables Apache passes data to scripts in environment variables. These include: DOCUMENT_ROOT="/var/www/html” GATEWAY_INTERFACE="CGI/1.1" HTTP_ACCEPT="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*” HTTP_ACCEPT_CHARSET="iso-8859-1,*,utf-8” HTTP_ACCEPT_ENCODING="gzip" HTTP_ACCEPT_LANGUAGE="en” HTTP_CONNECTION="Keep-Alive" HTTP_HOST="194.81.104.115” HTTP_USER_AGENT="Mozilla/4.74 (Macintosh; U; PPC)” PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin” QUERY_STRING="" REMOTE_ADDR="194.81.104.118"
Environment variables REMOTE_PORT="1740" REQUEST_METHOD="GET” REQUEST_URI="/cgi-bin/printenv” SCRIPT_FILENAME="/var/www/cgi-bin/printenv” SCRIPT_NAME="/cgi-bin/printenv” SERVER_ADDR="194.81.104.115” SERVER_ADMIN="root@localhost" SERVER_NAME="radon” SERVER_PORT="80" SERVER_PROTOCOL="HTTP/1.0” SERVER_SIGNATURE="<ADDRESS>Apache/1.3.12 Server at radon Port 80</ADDRESS>\n” SERVER_SOFTWARE="Apache/1.3.12 (Unix) (Red Hat/Linux) mod_ssl/2.6.6 OpenSSL/0.9.5a DAV/1.0.1 PHP/4.0.2 mod_perl/1.24"
CGI script & environment #!/bin/sh echo "Content-Type: text/html” echo echo "<h1>Your browser is $HTTP_USER_AGENT</h1>” echo "<h2>Your IP address is $REMOTE_ADDR</h2>” echo "<h2>Your PORT number is $REMOTE_PORT</h2>"
CGI script & environment • Previous script produces this output in the browser window: Your browser is Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1 Your IP address is 194.81.104.118 Your PORT number is 51046