blob: 2203ee5a0a7fffb63bc40f76f7b6e40e05bd3a06 [file] [log] [blame]
Denis Vlasenko136f42f2007-02-11 14:52:07 +00001<html><head><title>CGI Script output</title></head><body><h1><img alt="" src="out_files/CGIlogo.gif"> CGI Script Output</h1>
2<hr>
3
4<h2>Script output</h2>
5
6The script sends its output to stdout. This output can either be a
7document generated by the script, or instructions to the server for
8retrieving the desired output. <p>
9</p><hr>
10
11<h2>Script naming conventions</h2>
12
13Normally, scripts produce output which is interpreted and sent back to
14the client. An advantage of this is that the scripts do not need to
15send a full HTTP/1.0 header for every request. <p>
16<a name="nph">
17Some scripts may want to avoid the extra overhead of the server
18parsing their output, and talk directly to the client. In order to
19distinguish these scripts from the other scripts, CGI requires that
20the script name begins with nph- if a script does not want the server
21to parse its header. In this case, it is the script's responsibility
22to return a valid HTTP/1.0 (or HTTP/0.9) response to the client. </a></p><p>
23
24</p><hr>
25<h2><a name="nph">Parsed headers</a></h2>
26
27<a name="nph">The output of scripts begins with a small header. This header consists
28of text lines, in the same format as an </a><a href="http://www.w3.org/hypertext/WWW/Protocols/HTTP/Object_Headers.html">
29HTTP header</a>, terminated by a blank line (a line with only a
30linefeed or CR/LF). <p>
31
32Any headers which are not server directives are sent directly back to
33the client. Currently, this specification defines three server
34directives:</p><p>
35
36</p><ul>
37<li> <code>Content-type</code> <p>
38
39 This is the MIME type of the document you are returning. </p><p>
40
41</p></li><li> <code>Location</code> <p>
42
43 This is used to specify to the server that you are returning a
44 reference to a document rather than an actual document. </p><p>
45
46 If the argument to this is a URL, the server will issue a redirect
47 to the client. </p><p>
48
49 If the argument to this is a virtual path, the server will
50 retrieve the document specified as if the client had requested
51 that document originally. ? directives will work in here, but #
52 directives must be redirected back to the client.</p><p>
53
54
55</p></li><li> <a name="status"><code>Status</code></a><p>
56
57 This is used to give the server an HTTP/1.0 <a href="http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html">status
58line</a> to send to the client. The format is <code>nnn xxxxx</code>,
59where <code>nnn</code> is the 3-digit status code, and
60<code>xxxxx</code> is the reason string, such as "Forbidden".</p><p>
61
62</p></li></ul>
63
64<hr>
65<h2>Examples</h2>
66
67Let's say I have a fromgratz to HTML converter. When my converter is
68finished with its work, it will output the following on stdout (note
69that the lines beginning and ending with --- are just for illustration
70and would not be output): <p>
71
72</p><pre>--- start of output ---
73Content-type: text/html
74
75--- end of output ---
76</pre>
77
78Note the blank line after Content-type. <p>
79
80Now, let's say I have a script which, in certain instances, wants to
81return the document <code>/path/doc.txt</code> from this server just
82as if the user had actually requested
83<code>http://server:port/path/doc.txt</code> to begin with. In this
84case, the script would output: </p><p>
85</p><pre>--- start of output ---
86Location: /path/doc.txt
87
88--- end of output ---
89</pre>
90
91The server would then perform the request and send it to the client.
92<p>
93
94Let's say that I have a script which wants to reference our gopher
95server. In this case, if the script wanted to refer the user to
96<code>gopher://gopher.ncsa.uiuc.edu/</code>, it would output: </p><p>
97
98</p><pre>--- start of output ---
99Location: gopher://gopher.ncsa.uiuc.edu/
100
101--- end of output ---
102</pre>
103
104Finally, I have a script which wants to talk to the client directly.
105In this case, if the script is referenced with <a href="http://hoohoo.ncsa.uiuc.edu/cgi/env.html#protocol"><code>SERVER_PROTOCOL</code></a> of HTTP/1.0,
106the script would output the following HTTP/1.0 response: <p>
107
108</p><pre>--- start of output ---
109HTTP/1.0 200 OK
110Server: NCSA/1.0a6
111Content-type: text/plain
112
113This is a plaintext document generated on the fly just for you.
114
115--- end of output ---
116</pre>
117
118
119<hr>
120
121<a href="http://hoohoo.ncsa.uiuc.edu/cgi/interface.html"><img alt="[Back]" src="out_files/back.gif">Return to the
122interface specification</a> <p>
123
124CGI - Common Gateway Interface
125</p><address><a href="http://hoohoo.ncsa.uiuc.edu/cgi/mailtocgi.html">cgi@ncsa.uiuc.edu</a></address>
126</body></html>