2006 Pearson Education, Inc. All rights reserved Web Programming
2006 Pearson Education, Inc. All rights reserved. 2 The longest part of the journey is said to be the passing of the gate. — Marcus Terentius Varro Railway termini... are our gates to the glorious and unknown. Through them we pass out into adventure and sunshine, to them, alas! we return. — E. M. Forster There comes a time in a man’s life when to get where he has to go—if there are no doors or windows—he walks through a wall. — Bernard Malamud This is the common air that bathes the globe. — Walt Whitman
2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn: The Common Gateway Interface (CGI) protocol. The Hypertext Transfer Protocol (HTTP) and HTTP headers. Web server's functionality. The Apache HTTP Server. To request documents from a Web server. To implement CGI scripts. To send input to CGI scripts using XHTML forms.
2006 Pearson Education, Inc. All rights reserved Introduction 19.2 HTTP Request Types 19.3 Multitier Architecture 19.4 Accessing Web Servers 19.5 Apache HTTP Server 19.6 Requesting XHTML Documents 19.7 Introduction to CGI 19.8 Simple HTTP Transactions 19.9 Simple CGI Scripts Sending Input to a CGI Script Using XHTML Forms to Send Input Other Headers Case Study: An Interactive Web Page Cookies Server-Side Files Case Study: Shopping Cart Wrap-Up Internet and Web Resources
2006 Pearson Education, Inc. All rights reserved Introduction Web servers – Respond to client requests by providing resources User requests by entering a uniform resource locator (URL) Server returns the requested document – Communication is performed through Hypertext Transfer Protocol (HTTP) Protocol for transferring requests and files Platform-independent
2006 Pearson Education, Inc. All rights reserved HTTP Request Types Request types (a.k.a. request methods) – Specify how a client sends requests to a server – Often processed by a server-side form-handler program – Most common request types are get and post Retrieve and send client form data from and to web server – Contain information entered from graphical-user- interface components get request – Retrieve information (e.g., a document or an image) – Sends form data as query string in the URL
2006 Pearson Education, Inc. All rights reserved HTTP Request Types (Cont.) Request types (Cont.) post request – Sends data to the server – Often used for posting messages, authentication, etc. Browser caching – Browsers often save Web pages on disk for quick reloading Mostly used for get requests – Are typically static Not used for post requests – Contents may change frequently
2006 Pearson Education, Inc. All rights reserved. 8 Software Engineering Observation 19.1 The data sent in a post request is not part of the URL and cannot be seen by users. Forms that contain many fields often are submitted to Web servers via post requests. Sensitive form fields, such as passwords, should be sent using this request type.
2006 Pearson Education, Inc. All rights reserved. 9 Fig | HTTP’s other request types.
2006 Pearson Education, Inc. All rights reserved Multitier Architecture A multitier application – Divides functionality into separate tiers – A Web server is an example – Bottom tier Also called information tier or data tier Maintains data for the application – Typically stored in a relational database management system (RDBMS)
2006 Pearson Education, Inc. All rights reserved Multitier Architecture (Cont.) A multitier application (Cont.) – Middle tier Also called business logic tier Implements business logic – Controls interactions between clients and data Enforces business rules – Ensures data is reliable – Dictates how clients can access data
2006 Pearson Education, Inc. All rights reserved. 12 Fig | Three-tier application model.
2006 Pearson Education, Inc. All rights reserved Multitier Architecture (Cont.) A multitier application (Cont.) – Top tier Also called client tier Is the user interface – Makes requests with lower tiers – Displays retrieved data to the user
2006 Pearson Education, Inc. All rights reserved Accessing Web Servers Web servers – Local Web servers Reside on users’ machines – Remote Web servers Reside on machines across a network Host – Computer that stores and maintains resources localhost – Host name that references local machine
2006 Pearson Education, Inc. All rights reserved Accessing Web Servers (Cont.) Domain – Represents a group of hosts on the Internet – Each domain has a domain name or Web address Fully qualified domain name (FQDN) – Also known as the machine name – Contains A host name A domain name, including – A top-level domain (TLD) Often describes type of organization owning the domain
2006 Pearson Education, Inc. All rights reserved Accessing Web Servers (Cont.) Fully qualified domain name (Cont.) – Example – Host name is www – Top-level domain is com Usually refers to a commercial business
2006 Pearson Education, Inc. All rights reserved Accessing Web Servers (Cont.) IP (Internet Protocol) address – Used to locate other computers on the Internet – Each FQDN corresponds to an IP address – Comprised of four sets of numbers separated by periods Such as – The loopback address IP address of localhost is always
2006 Pearson Education, Inc. All rights reserved Accessing Web Servers (Cont.) Domain Name System (DNS) server – Computer that maintains a database of FQDNs and corresponding IP addresses – DNS lookup Translating FQDNs to IP addresses Example – (FQDN) translates to (IP address)
2006 Pearson Education, Inc. All rights reserved Apache HTTP Server The Apache HTTP server – Maintained by the Apache Software Foundation – Most popular Web server because of its: Stability Cost Efficiency Portability – Is open-source (source code is freely available)
2006 Pearson Education, Inc. All rights reserved Apache HTTP Server (Cont.) The Apache HTTP server (Cont.) – Runs on platforms: UNIX Linux Windows – To download/install the Apache HTTP server httpd.apache.org/
2006 Pearson Education, Inc. All rights reserved Requesting XHTML Documents XHTML documents in the Apache HTTP server – Must be saved in the htdocs directory On Windows platforms, htdocs is in C:\Program Files\Apache Group\Apache2 On Linux platforms, htdocs is in /usr/local/httpd – Root directory of the Web server refers to default directory htdocs Do not have to enter htdocs before a file name in a URL
2006 Pearson Education, Inc. All rights reserved. 22 Fig | Requesting test.html from Apache.
2006 Pearson Education, Inc. All rights reserved Common Gateway Interface Common Gateway Interface (CGI) – Standard protocol for enabling applications to interact with Web servers and clients Applications are called CGI programs or CGI scripts – Often used to generate dynamic Web content Generated programmatically when page is requested – Not specific to any operating system or programming language – Developed in 1993 by NCSA (National Center for Supercomputing Applications) For use with its HTTPd Web server
2006 Pearson Education, Inc. All rights reserved Simple HTTP Transactions An XHTML document – Plain text file that contains markings (markup or elements) Markings describe structure of data Example – My Web Page start tag My Web Page is the title of the Web page end tag Also can contain hypertext information (hyperlinks) – Links to other Web pages or other locations
2006 Pearson Education, Inc. All rights reserved Simple HTTP Transactions (Cont.) A Web XHTML file’s URL – Example – Protocol of the resource is http – Machine name is – Name of the requested resource is /books/downloads.html Resource name is downloads.html Path is /books – A virtual directory Alias or fake name for physical directory on disk Server translates virtual path into real location
2006 Pearson Education, Inc. All rights reserved Simple HTTP Transactions (Cont.) An HTTP transaction – GET /books/downloads.html HTTP/1.1 Host: GET indicates a get request Name and path of resource Protocol’s name and version number
2006 Pearson Education, Inc. All rights reserved. 27 Fig | Client interacting with server and Web server. Step 1: The get request, GET /books/downloads.html HTTP/1.1. (Part 1 of 2.)
2006 Pearson Education, Inc. All rights reserved. 28 Fig | Client interacting with server and Web server. Step 2: The HTTP response, HTTP/ OK. (Part 2 of 2.)
2006 Pearson Education, Inc. All rights reserved Simple HTTP Transactions (Cont.) An HTTP transaction (Cont.) – Server’s response Indicates the HTTP version Followed by numeric code and phrase for the status – HTTP/ OK Indicates success – HTTP/ Not found Indicates resource was not found Then sends HTTP headers – Provides information about the data being sent – Example Content-Type: text/html
2006 Pearson Education, Inc. All rights reserved Simple HTTP Transactions (Cont.) An HTTP transaction (Cont.) Server sends blank line to indicate headers are finished Server finally sends contents of requested document MIME (Multipurpose Internet Mail Extensions) – Identified in the Content-Type header – Helps browser determine how to process data – Examples text/plain – Data contains text, without XHTML markup image/gif – Content is a GIF image
2006 Pearson Education, Inc. All rights reserved Simple CGI Scripts CGI Scripts – Executing a C++ program as a CGI script Place compiled C++ executable file in Web server’s cgi-bin directory – Executable file extension.exe may be changed to.cgi Standard output is redirected (piped) to Web server – Web server sends output to Web browser Detected by Web server depending on server configuration – Special filename extension (.cgi or.exe ) – Located in a specific directory ( cgi-bin ) Web server automatically adds HTTP-transaction-status message
2006 Pearson Education, Inc. All rights reserved. 32 Outline localtime.cpp (1 of 2) Declare time_t variable currentTime Get the current time and store it in currentTime CGI script must output the Content-Type header and the subsequent blank line
2006 Pearson Education, Inc. All rights reserved. 33 Outline localtime.cpp (2 of 2) Output a string containing the “ broken- down ” version of the local time
2006 Pearson Education, Inc. All rights reserved. 34 Fig | Step 1: The get request, GET /cgi-bin/localtime.cgi HTTP/1.1. (Part 1 of 4.)
2006 Pearson Education, Inc. All rights reserved. 35 Fig | Step 2: The Web server starts the CGI script. (Part 2 of 4.)
2006 Pearson Education, Inc. All rights reserved. 36 Fig | Step 3: The script output is sent to the Web server. (Part 3 of 4.)
2006 Pearson Education, Inc. All rights reserved. 37 Fig | Step 4: The HTTP response, HTTP/ OK. (Part 4 of 4.)
2006 Pearson Education, Inc. All rights reserved. 38 Common Programming Error 19.1 Forgetting to place a blank line after a header is a syntax error.
2006 Pearson Education, Inc. All rights reserved. 39 Fig | Output of localtime.cgi when executed from the command line.
2006 Pearson Education, Inc. All rights reserved Simple CGI Scripts (Cont.) Environment variables – Contain information about client and server environment Such as type of Web browser or location of document – Are server-specific – Retrieved by calling function getenv of Takes a char * string naming the environment variable Returns associated value of the environment variable as a char * string – Returns null pointer if the environment variable does not exist
2006 Pearson Education, Inc. All rights reserved. 41 Outline environment.cpp (1 of 4) Initialize an array of string objects with the names of the CGI environment variables
2006 Pearson Education, Inc. All rights reserved. 42 Outline environment.cpp (2 of 4) Output the name of the environment variable in an XHTML table-data element Attempt to retrieve the value associated with the environment variable Output the environment-variable value returned by getenv
2006 Pearson Education, Inc. All rights reserved. 43 Outline environment.cpp (3 of 4)
2006 Pearson Education, Inc. All rights reserved. 44 Outline environment.cpp (4 of 4)
2006 Pearson Education, Inc. All rights reserved Sending Input to a CGI Script Environment variable QUERY_STRING – Contains information that is appended to a URL in a get request Example – www. site.com/cgi-bin/script.cgi?state=Cali www. site.com/cgi-bin/script.cgi?state=Cali Requests CGI script cgi-bin/script.cgi With query string state=Cali The query string following the ? delimiter is stored in environment variable QUERY_STRING
2006 Pearson Education, Inc. All rights reserved. 46 Outline querystring.cpp (1 of 3) Pass "QUERY_STRING" to function getenv Assign the returned query string to string variable query
2006 Pearson Education, Inc. All rights reserved. 47 Outline querystring.cpp (2 of 3) Test if query contains data Output the contents of the query string
2006 Pearson Education, Inc. All rights reserved. 48 Outline querystring.cpp (3 of 3)
2006 Pearson Education, Inc. All rights reserved Using XHTML Forms to Send Input XHTML form element – Encloses an XHTML form – Generally takes two attributes action specifies server resource to execute when user submits the form method identifies type of HTTP request to use when submitting the form – May contain any number of internal elements Using XHTML form with get – Names of input fields and user-inputted values are passed as a query string Ampersand-separated list of pairs, each with = between the name and the value
2006 Pearson Education, Inc. All rights reserved. 50 Fig | XHTML form elements. (Part 1 of 2)
2006 Pearson Education, Inc. All rights reserved. 51 Fig | XHTML form elements. (Part 2 of 2)
2006 Pearson Education, Inc. All rights reserved. 52 Outline getquery.cpp (1 of 4)
2006 Pearson Education, Inc. All rights reserved. 53 Outline getquery.cpp (2 of 4) Attribute method has the value "get" Attribute action has the value "getquery.cgi" Single-line text field named word A button, labeled Submit Word, to submit the form data Search query for the first occurrence of word= and add 5 Take the substring in query containing the user ’ s favorite word Output the word entered by the user
2006 Pearson Education, Inc. All rights reserved. 54 Outline getquery.cpp (3 of 4) Query string is appended to the URL with a question mark ( ? ) in front of it
2006 Pearson Education, Inc. All rights reserved. 55 Outline getquery.cpp (4 of 4)
2006 Pearson Education, Inc. All rights reserved Using XHTML Forms to Send Input (Cont.) Using XHTML form with post – Data is sent to CGI script via standard input Data is encoded the same as with the get method – Name-value pairs with equals signs and ampersands CONTENT_LENGTH environment variable set to indicate number of characters sent – Use cin.read instead of stream extraction on cin CGI specification does not require a newline to be appended to the end of the form data – Stream extraction operation may not terminate if no newline is there
2006 Pearson Education, Inc. All rights reserved. 57 Outline post.cpp (1 of 5) Determine whether CONTENT_LENGTH contains a value Read in the value of CONTENT_LENGTH and convert it to an integer Use function cin.read to read form data from standard input
2006 Pearson Education, Inc. All rights reserved. 58 Outline post.cpp (2 of 5) Check if any form data was sent
2006 Pearson Education, Inc. All rights reserved. 59 Outline post.cpp (3 of 5)
2006 Pearson Education, Inc. All rights reserved. 60 Outline post.cpp (4 of 5)
2006 Pearson Education, Inc. All rights reserved. 61 Outline post.cpp (5 of 5)
2006 Pearson Education, Inc. All rights reserved Using XHTML Forms to Send Input (Cont.) URL encoding – Web browsers “URL encode” the XHTML form data they send Spaces are replaced with plus signs Other symbols are translated into hexadecimal ASCII values preceded with % – Because URLs cannot contain certain characters
2006 Pearson Education, Inc. All rights reserved Other Headers Other HTTP headers – Refresh header Redirects client to new location after specified amount of time Example – Refresh: "5; URL = Fives seconds after browser receives this header, it requests the resource at specified URL If no URL is specified, current page is refreshed
2006 Pearson Education, Inc. All rights reserved Other Headers (Cont.) Other HTTP headers (Cont.) – Location header Redirects client to new location, immediately Example – Location: If used with relative or virtual URL ( Location: /newpage.html ), redirection is performed server-side – Location header is not sent to client – Specified resource is sent as if it were the one requested
2006 Pearson Education, Inc. All rights reserved Other Headers (Cont.) Other HTTP headers (Cont.) – Status header Instructs server to output specified status header line Example – Status: 204 No Response Could be used to indicate that request was successful, but no new page should be displayed
2006 Pearson Education, Inc. All rights reserved. 66 Outline travel.html (1 of 2) POST data to the portal.cgi CGI script Fields to collect the user ’ s name and password
2006 Pearson Education, Inc. All rights reserved. 67 Outline travel.html (2 of 2)
2006 Pearson Education, Inc. All rights reserved. 68 Outline portal.cpp (1 of 5)
2006 Pearson Education, Inc. All rights reserved. 69 Outline portal.cpp (2 of 5) Determine the start and end locations of the user ’ s name and password in string dataString Assign the form-field values to variables nameString and passwordString
2006 Pearson Education, Inc. All rights reserved. 70 Outline portal.cpp (3 of 5) Use nameString to output a personalized greeting to the user Output an additional special if the member password is correct
2006 Pearson Education, Inc. All rights reserved. 71 Outline portal.cpp (4 of 5)
2006 Pearson Education, Inc. All rights reserved. 72 Outline portal.cpp (5 of 5)
2006 Pearson Education, Inc. All rights reserved. 73 Performance Tip 19.1 It is always much more efficient for the server to provide static content rather than execute a CGI script, because it takes time for the server to load the script from hard disk into memory and execute the script (whereas an XHTML file needs to be sent only to the client). It is a good practice to use a mix of static XHTML (for content that generally remains unchanged) and CGI scripting (for dynamic content). This practice allows the Web server to respond to clients more efficiently than if only CGI scripting were used.
2006 Pearson Education, Inc. All rights reserved Cookies Cookies – Small text files that server sends to browser to save on local computer – Used to track user’s progress through a site or customize the site Can be used to identify and track user visits and purchases – Considered a security and privacy concern – Browsers allow users to disable cookies
2006 Pearson Education, Inc. All rights reserved. 75 Outline cookieform.html (1 of 2)
2006 Pearson Education, Inc. All rights reserved. 76 Outline cookieform.html (2 of 2)
2006 Pearson Education, Inc. All rights reserved. 77 Outline writecookie.cpp (1 of 4) Store the expiration date of the cookie
2006 Pearson Education, Inc. All rights reserved. 78 Outline writecookie.cpp (2 of 4)
2006 Pearson Education, Inc. All rights reserved. 79 Outline writecookie.cpp (3 of 4) Output the Set-Cookie: header Store the user ’ s data in the cookie Set expiration date and the path of the server domain for the cookie
2006 Pearson Education, Inc. All rights reserved. 80 Outline writecookie.cpp (4 of 4)
2006 Pearson Education, Inc. All rights reserved Cookies (Cont.) Setting a cookie – Cookie information must be output before header is written to client – Set-Cookie: header – Three attributes, separated by semi-colons Data Expiration date – Determines how long the cookie resides on the computer – Can be absolute date or relative value URL of server domain for which the cookie is valid
2006 Pearson Education, Inc. All rights reserved. 82 Portability Tip 19.1 Web browsers store the cookie information in a vendor-specific manner. For example, Microsoft’s Internet Explorer stores cookies as text files in the Temporary Internet Files directory on the client’s machine. Netscape stores its cookies in a single file named cookies.txt.
2006 Pearson Education, Inc. All rights reserved Cookies (Cont.) Reading a cookie – Applicable cookies are sent by browser to server as part of a request to the server – Environment variable HTTP_COOKIE stores user’s cookies Calling function getenv with HTTP_COOKIE environment variable as the parameter retrieves cookie data
2006 Pearson Education, Inc. All rights reserved. 84 Outline readcookie.cpp (1 of 3) Retrieve the cookie data stored in environment variable HTTP_COOKIE Decode the name-value pairs
2006 Pearson Education, Inc. All rights reserved. 85 Outline readcookie.cpp (2 of 3)
2006 Pearson Education, Inc. All rights reserved. 86 Outline readcookie.cpp (3 of 3)
2006 Pearson Education, Inc. All rights reserved. 87 Software Engineering Observation 19.2 Cookies present a security risk. If unauthorized users gain access to a computer, they can examine the local disk and view files, which include cookies. For this reason, sensitive data, such as passwords, social security numbers and credit card numbers, should never be stored in cookies.
2006 Pearson Education, Inc. All rights reserved Server-Side Files Server-side files – Files that are located on the server or the server’s network Only someone with access and permission to change files on the server can alter files More secure than storing user data in cookies
2006 Pearson Education, Inc. All rights reserved. 89 Outline savefile.html (1 of 2)
2006 Pearson Education, Inc. All rights reserved. 90 Outline savefile.html (2 of 2)
2006 Pearson Education, Inc. All rights reserved. 91 Outline savefile.cpp (1 of 6)
2006 Pearson Education, Inc. All rights reserved. 92 Outline savefile.cpp (2 of 6)
2006 Pearson Education, Inc. All rights reserved. 93 Outline savefile.cpp (3 of 6)
2006 Pearson Education, Inc. All rights reserved. 94 Outline savefile.cpp (4 of 6) Open server-side file clients.txt for appending data
2006 Pearson Education, Inc. All rights reserved. 95 Outline savefile.cpp (5 of 6) Append user ’ s data to server- side file clients.txt
2006 Pearson Education, Inc. All rights reserved. 96 Outline savefile.cpp (6 of 6)
2006 Pearson Education, Inc. All rights reserved. 97 Fig | Contents of clients.txt data file.
2006 Pearson Education, Inc. All rights reserved. 98 Outline login.cpp (1 of 11)
2006 Pearson Education, Inc. All rights reserved. 99 Outline login.cpp (2 of 11) Determine whether data was posted to the program
2006 Pearson Education, Inc. All rights reserved. 100 Outline login.cpp (3 of 11) Output an XHTML form to the user
2006 Pearson Education, Inc. All rights reserved. 101 Outline login.cpp (4 of 11) This else block executes if the user entered data Open the file that contains all existing usernames and passwords Determine whether the new user ’ s name is already taken
2006 Pearson Education, Inc. All rights reserved. 102 Outline login.cpp (5 of 11) Output an appropriate error message Add the new user information to the file Provide a hyperlink to the shopping script Provide a hyperlink back to the login form
2006 Pearson Education, Inc. All rights reserved. 103 Outline login.cpp (6 of 11) Attempt to locate the existing username in the file Determine whether the password entered matches the password stored in the file Provide a link to the shopping script
2006 Pearson Education, Inc. All rights reserved. 104 Outline login.cpp (7 of 11) Provide appropriate messages and links to reattempt the login process Output necessary XHTML header information for the login page
2006 Pearson Education, Inc. All rights reserved. 105 Outline login.cpp (8 of 11) Write a cookie which is used by the other scripts to store the contents of the user ’ s shopping cart
2006 Pearson Education, Inc. All rights reserved. 106 Outline login.cpp (9 of 11)
2006 Pearson Education, Inc. All rights reserved. 107 Outline login.cpp (10 of 11)
2006 Pearson Education, Inc. All rights reserved. 108 Outline login.cpp (11 of 11)
2006 Pearson Education, Inc. All rights reserved. 109 Outline shop.cpp (1 of 5)
2006 Pearson Education, Inc. All rights reserved. 110 Outline shop.cpp (2 of 5) Output a table to display the available books
2006 Pearson Education, Inc. All rights reserved. 111 Outline shop.cpp (3 of 5) Output a form containing the submit button for adding each book to the shopping cart Hidden form fields provide form data invisible to the user
2006 Pearson Education, Inc. All rights reserved. 112 Outline shop.cpp (4 of 5) Output necessary XHTML header information for the shopping page
2006 Pearson Education, Inc. All rights reserved. 113 Outline shop.cpp (5 of 5)
2006 Pearson Education, Inc. All rights reserved. 114 Outline viewcart.cpp (1 of 7)
2006 Pearson Education, Inc. All rights reserved. 115 Outline viewcart.cpp (2 of 7) Read the value of the cookie Store the ISBN number of the requested book
2006 Pearson Education, Inc. All rights reserved. 116 Outline viewcart.cpp (3 of 7) Write the ISBN number into cookieString Append the ISBN to the existing cookie data in cookieString Display the contents of the shopping cart, if any
2006 Pearson Education, Inc. All rights reserved. 117 Outline viewcart.cpp (4 of 7) Provide links back to the shopping page or to the check-out page
2006 Pearson Education, Inc. All rights reserved. 118 Outline viewcart.cpp (5 of 7) Get each book ’ s information from the file
2006 Pearson Education, Inc. All rights reserved. 119 Outline viewcart.cpp (6 of 7) Count how many times the current ISBN appears in the shopping cart Display the book ’ s information and the number of copies the user has chosen to purchase
2006 Pearson Education, Inc. All rights reserved. 120 Outline viewcart.cpp (7 of 7)
2006 Pearson Education, Inc. All rights reserved. 121 Outline checkout.cpp (1 of 2) Erase the current information in the shopping cart
2006 Pearson Education, Inc. All rights reserved. 122 Outline checkout.cpp (2 of 2)
2006 Pearson Education, Inc. All rights reserved. 123 Fig | Contents of catalog.txt.
2006 Pearson Education, Inc. All rights reserved Internet and Web Resources Apache – httpd.apache.org/ httpd.apache.org/ Product home page for the Apache HTTP server Users may download Apache from this site – Contains articles about Apache jobs, product reviews and other information – Contains an article about the Apache HTTP server and supporting platforms Contains links to other Apache articles
2006 Pearson Education, Inc. All rights reserved Internet and Web Resources (Cont.) CGI – Contains a free open-source CGI library for creating C++ CGI scripts – Contains a rich collection of scripts using CGI – Contains a brief explanation of CGI – Discusses CGI security issues – Contains information on HTTP and links to news, mailing lists and published articles