C++ Programming As A Set Of Problems/CGI Script program

From Wikibooks, open books for an open world
Jump to: navigation, search

simple script[edit]

/* cgi_hello.cpp
public domain
WARNING: untested code.
*/
void send_CGI_header(){
    cout << "Content-type: text/html\n";
    cout << "\n"; // this blank line is a necessary part of the CGI protocol.
}
void send_contents(){
    cout << "<!doctype html>\n"
    cout << "<html>\n"
    cout << "<title>Hello</title>\n"
    cout << "<p>Hello, world!\n"
    // C strings can, with care, contain double-quote characters, newlines, etc.
    cout << "<p><a href=\"http://validator.w3.org/check?uri=referer\">"
    cout << "Valid.</a>\n"
    cout << "</html>\n"
    cout << endl;
}
 
int main(){
    send_CGI_header();
    send_contents();
    return 0;
}

Compile this source code into an executable named "test_script.cgi".

setup[edit]

Nearly all web servers support the Common Gateway Interface. Here is some brief notes on how to put a CGI script written in C++ on an Apache server; other web servers are similar. For more details, see the Apache wikibook, section ... , or the analogous documentation for your favorite web server.

... security implications: user "nobody" ...

CGI scripts are used to deal with form data submission. Here is an example HTML file that lets the user submit data to a "test_script.cgi" program.

<!-- test_form.html -->
<html>
 <form action="test_script.cgi" method="get">
   Favorite animal: <input name="favorite_animal" type="text" />
   <input type="submit" />
 </form>
</html>

Put the compiled "test_script.cgi" in the appropriate "cgi-bin" directory. Put the "test_form.html" in some convenient location -- typically some other directory on the same server, but it also works if it's on some other server or a file on your local machine. Tweak the "action" property to give a path to the CGI script.

testing[edit]

Fire up your favorite web browser, and point it at the "test_form.html". Click the submit button. The output from the CGI script should be displayed. Click the "Valid" link.

static HTML[edit]

The above script always gives the same output -- if that's really what you want, then a simple static HTML page would give the same results for less effort:

<!doctype html>
<!-- static_hello.html -->
<html>
<title>Hello</title>
<p>Hello, world!
<p><a href="http://validator.w3.org/check?uri=referer">Good.</a>
</html>

The power of CGI scripts is in doing things that are not possible with a static HTML page.

getting input to the script[edit]

There are several libraries already available to handle the tricky bits of splitting the CGI input into a list of name/val pairs.


/* cgi_hello.cpp
public domain
WARNING: untested code.
*/
...
int main(){
    send_CGI_header();
    send_contents();
    return 0;
}

A CGI script doesn't use the command-line arguments "argc" or argv". Instead, it gets its input from environment variables -- and, in the case of HTTP PUT or HTTP POST, the program reads user-submitted data from the standard input.

Compile this source code into an executable named "test_script.cgi". Use the same test_form.html to test it.

Further reading[edit]