/* * This header file declares a few functions that can be useful * when writing CGI programs in C++. Functions are provided for * getting data submitted from an HTML form. There is also a * function for retrieving the values of the basic CGI variables * such as REMOTE_ADDR, and a few miscellaneous function. * * The data from a form consists of a number of name/value pairs. * Each pair comes from one item, such as a text-input box, in the * form. The item has a name to identify it and a value that is * usually set in some way by the user. The data is delivered * in an encoded form. The functions declared in this header file * will decode the data and make the name/value pairs easily * available to your program. */ #include /* * Values of type CGIVariable represent basic CGI variables that * provide a CGI program with information about the browser request * that caused the program to be executed. The CGIVariable type is * only used as the parameter type in the getCGIVariable function. */ enum CGIVariable { REQUEST_METHOD, // Indicate the method used to submit the // form data, as specified in the HTML
// tag. This could be either "GET" or "POST". REMOTE_ADDR, // The IP address of the computer on which the // browser. This is given as a string such // as "172.30.10.23". HTTP_USER_AGENT, // A string containing information about the // that was used (Internet Explorer, Netscape, // Mozilla, etc.). This could be used to // customize different responses for different // browsers. HTTP_REFERER, // Might contain the URL of the Web page containing // the form or link that the user used to send // the request. // (You are unlikely to use the remaining variables. You can // find descriptions of them in any good HTML reference.) QUERY_STRING, // (Data for a GET request.) CONTENT_LENGTH, CONTENT_TYPE, HTTP_ACCEPT, HTTP_HOST, REMOTE_PORT, SCRIPT_FILENAME, GATEWAY_INTERFACE, SERVER_PROTOCOL, HTTP_ACCEPT_LANGUAGE, REQUEST_URI, SCRIPT_NAME, SERVER_SOFTWARE, SERVER_PORT, DOCUMENT_ROOT, PATH_INFO, REMOTE_USER, AUTH_TYPE, REMOTE_IDENT }; /* * Returns the value of the CGI Variable that is specified by the * parameter. Note that the return value can be an empty string. */ string getCGIVariable(CGIVariable varName); /* * The number of name/value pairs in the HTML form data. */ int formItemCount(); /* * Returns the value from one of the name/value pairs in the HTML * form data. The parameter is the name, and the associated value, * if any, is returned. If there is no name/value pair with the * specified name, then the empty string is returned. Note that * the empty string is also a legitimate value in a name value * pair. For example, the user might leave a text-input box empty. * This function does not distinguish between a name whose associated * value is empty and a name that does not occur in any name/value * pair. Use the isFormName() function if you want to distinguish * these two cases. */ string getFormItemValue(string itemName); /* * Checks whether itemName occurs as the name in one of the * name/value pairs in the form data. (This function could be * used to check for data from checkboxes and radio buttons.) */ bool isFormName(string itemName); /* * Name/value pairs from the form data are stored in an array. * You can access the data based on position in the array. * You could use these method to access the data when you don't * know what names are used on the form. Use getFormItemName(i) * to get the name from position i in the array. Use * getFormItemValue(i) to get the value. Numbering starts from * zero. The length of the array is returned by the * function formItemCount(). */ string getFormItemName(int itemNum); string getFormItemValue(int itemNum); /* * This function converts a string such as "27" or "-892" into the * corresponding integer. The return value indicates success or * failure: If the value of numString is not a valid integer, then * num is set to 0 and the return value of the function is false. * If the string is a valid integer, then num is set to the integer * value and the return value is true. This function can be useful * in processing form data, since the data is always delivered in * the form of a string, even if the value is supposed to be a number. */ bool string2int(string numString, int &num); /* * Converts a string such as "83.2" or "15e-5" or "17" into the * corresponding real number. The return value indicates success * or failure. */ bool string2double(string numString, double &num); /* * This function returns a copy of its parameter in which the * HTML codes <, >, &, and " have been substituted * for the characters <, >, &, and ". These characters have special * meaning in HTML, so they should not appear in an HTML file. * If you want to put a string that might contain these characters * on a web page, you can pass it through this function first to * prevent them from messing up your HTML. This might apply, for * example, to strings that were typed into a form by a user. */ string htmlEscape(string str);