13.1. CGI

A glance at comp.lang.perl.misc reveals that many people don't understand the difference between the CGI protocol and the Perl scripts that use it. We therefore consider it our civic duty to take the time to explain it here, even though we're talking about something that is not Perl. (That's the Perl connection—that it has nothing to do with Perl. It's all very Gödel-like.)

The Common Gateway Interface is a specification for how a Web server can deliver dynamic content. It says that you can have a URL that points not to a static file but to a program whose output will be sent to the browser. The user of the browser cannot tell the difference between a CGI program and a static file by looking at what the server returns; the only clue is that the URL usually looks different. (Instead of ending in .html, it probably ends in .cgi or contains /cgi-bin/. There's nothing hard and fast about this rule though. It's easy to configure a Web server to return static pages whose URLs look like CGI programs, and vice-versa.) The point, however, is that they shouldn't care what produced the output.

The CGI specification spells out how a program invoked in this way can have inputs sent by a browser (as part of the URL using an HTTP GET command or in HTTP content with a POST command), how the program can see those inputs (in the QUERY_STRING environment variable or in their standard input respectively), how the inputs are encoded (a format that turns many characters into a hexadecimal representation), and what the program has to send in addition to the browser-visible content of its output (HTTP headers specifying the type of that content).

Nothing in the CGI specification specifies a particular language for CGI programs. This is why people who ask questions about CGI get attacked for asking them in a Perl newsgroup. The answer would be the same no matter what language the program was written in, and it wouldn't have anything to do with Perl. The newsgroup the attackers want those questions taken to is comp.infosystems.www.authoring.cgi.

So how can you tell whether your question or problem is Perl related or CGI related?

  • If your program produces apparently correct output when run from the command line, but the Web browser displays a 500 - Server Error message, it's a CGI problem. If you can't run the program from the command line on the Web server, see our tips that follow.

  • If your program is not picking up form inputs and you're using CGI.pm, the Web server has a problem. Test it with our cgi-test program that follows. If you're not using CGI.pm, few people are likely to assist you because you're making the task unnecessarily difficult.

  • If your program takes too long to produce any output, the browser will give up and display a “Document contained no data” message. You may need to use a nonparsed header (nph) script if your Web server otherwise buffers your output, or else figure out how to produce output before the browser times out.

See http://hoohoo.ncsa.uiuc.edu/cgi/interface.html for reference information on CGI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset