CSC 546: Client/Server Fundamentals
Fall 2000

HW2: CGI Programming


This assignment builds upon the CGI program from HW1 to complete a full-featured email address search engine (similar to many found on the Web). In addition to the features described in HW1, your program must (1) produce a more readable page, (2) accept first and last names as search strings, and (3) handle special characters. It is strongly recommended that you complete one modification/addition before beginning the next.

  1. As is, your program produces straightforward text, which is displayed by the browser without any special formatting. In general, the output of a CGI program can be a full-featured Web page that uses HTML tags to specify formatting. Modify your program so that it outputs the results of its search embedded in a more readable Web page. In particular, there should be HTML tags delimiting the HEAD and BODY of the page, and the HEAD should contain an appropriate TITLE for the page. The resulting page should display the search string and all email addresses as links.

    For example, a search for "Dave" using the faculty email database might produce the following output:

    Content-Type: text/html <HTML> <HEAD><TITLE>Math/CS Email Search</TITLE></HEAD> <BODY> Search results for: Dave <BR> <BR> Davender Malik: <A HREF='mailto:malik@creighton.edu'>malik@creighton.edu</A><BR> Dave Reed: <A HREF='mailto:davereed@creighton.edu'>davereed@creighton.edu</A><BR> </BODY><BR> </HTML>

  2. As is, your program takes a single string and reports all names that contain that string. Unfortunately, there is no way to specify that the string should appear in the last name as opposed to the first name (or vice versa). Modify your program so that it accepts input containing both a first and a last name (in that order). The program should report all names whose first and last names both match the corresponding search strings.

    Recall that the HTTP POST method encodes all form elements in a single string, which is then passed to the CGI program as input. Thus, the Web page on the left might produce the string on the right:

        first=Dave&last=R

  3. As is, your program does not handle special characters (other than spaces) in the search string. Recall that text from the HTML form is URL-encoded so that spaces are replaced by plus signs. Similarly, other characters are encoded using a percent sign and their ASCII codes (written as two-digit hexadecimal numbers). For example, the name "O'Toole" would be URL-encoded as "O%27Toole" since the single quote character has the ASCII value 39, which is written as 27 in hex. Modify your program so that special characters in the search string are handled correctly.

    Hint 1: The task of translating a single hex character to its decimal equivalent can be simplified using strings. By storing all of the hex digits in a string (with index corresponding to value), the value of a hex digit can be determined using find. For example,

    const string HEX_DIGITS = "0123456789ABCDEF"; int hexValue1 = HEX_DIGITS.find('9'); // assigns value 9 int hexValue2 = HEX_DIGITS.find('A'); // assigns value 10

    Hint 2: To convert a two-digit hex number to decimal, simply multiply the first digit by 16 and add the second digit. For example,

    27 (in hex) --> 2 * 16 + 7 = 39 (in decimal) 3A (in hex) --> 3 * 16 + 10 = 58 (in decimal)

    Hint 3: To convert an ASCII code into its corresponding character, simply cast that code to a char. For example,

    char quote = (char)39; // assigns the variable to be '