3. Output from the Common Gateway Interface
3.1 Overview
As described in Chapter 3, Output from the Common Gateway Interface, CGI programs are requested like any other regular documents. The difference is that instead of returning a static document, the server executes a program and returns its output. As far as the browser is concerned, however, it expects to get the same kind of response that it gets when it requests any document, and it's up to the CGI program to produce output that the browser is comfortable with.
The most basic output for a CGI program is a simple document in either plain text or HTML, which the browser displays as it would any document on the Web. However, there are other things you can do, such as:
- Return graphics and other binary data
- Tell the browser whether to cache the virtual document
- Send special HTTP status codes to the browser
- Tell the server to send an existing document
Each of these techniques involves knowing a little bit about returning additional headers from the CGI program.
3.2 CGI and Response Headers
By now, you should be reasonably comfortable designing CGI programs that create simple virtual documents, like this one:
#!/usr/local/bin/perl print "Content-type: text/html", "\n\n"; print "<HTML>", "\n"; print "<HEAD><TITLE>Simple Virtual HTML Document</TITLE></HEAD>", "\n"; print "<BODY>", "\n"; print "<H1>", "Virtual HTML", "</H1>", "<HR>", "\n"; print "Hey look, I just created a virtual (yep, virtual) HTML document!", "\n"; print "</BODY></HTML>", ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access