C++ Programming As A Set Of Problems/CGI Script program
simple script
[edit | edit source]/* cgi_hello.cpp
public domain
NB: tested code.
*/
#include <iostream>
using namespace std;
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 | edit source]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 | edit source]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 | edit source]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 | edit source]There are several libraries already available to handle the tricky bits of splitting the CGI input into a list of name/val pairs.
- getcgivars.c from http://www.jmarshall.com/easy/cgi/getcgi.c.txt
- ... other CGI libraries in C and C++ ...
/* 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 | edit source]- the GET Wikibook looks at CGI scripts from the user side
- "The World Wide Web Security FAQ": CGI (Server) Scripts
- "CGI Made Really Easy: or, Writing CGI scripts to process Web forms" by James Marshall.
- Many people prefer to write CGI scripts in Perl (Perl Programming/CGI), PHP (PHP Programming), Rebol (Rebol Programming), etc.
- Wikipedia: Common Gateway Interface
- Wikipedia: query string