Reputation: 23
My question is regarding cgi. I wondered if I have a Perl script saved as a .cgi file, what is the actual mechanism that lets the server know it's a Perl script and not something else? I know there is a #!/usr/local/bin/perl
at the beginning of the source file but that's also part of the Perl programing language, so the server has to know it's a Perl script before it starts execution.
Upvotes: 2
Views: 944
Reputation: 126772
An HTTP server will normally just send the specified file to the client. The server's configuration dictates which files will instead be run and their output returned.
For example, the popular Apache httpd server's configuration file is httpd.conf
, or specifically one of the file Include
d from there. It varies greatly with software version, host operating system and distribution.
The criteria can be based either on where the file is in the file structure, or what its name is.
A ScriptAlias directive like
ScriptAlias /cgi-bin
says that all files in the /cgi-bin
directory will be treated as a CGI script.
Alternatively, an AddHandler directive like
AddHandler cgi-script .cgi
says that all files with a .cgi
extension will be run as a CGI script wherever they are.
Upvotes: 3
Reputation: 263647
If the server is configured to recognize the .cgi
suffix, then it simply executes the file, probably using one of the exec*()
functions.
On a Unix-like system, if the file is a text file starting with #!
, then the system kernel itself will use that line to determine which interpreter to launch to run the file as a script.
You can write CGI scripts in any interpreted language (Perl, Python, bash, sh, ruby, etc., etc.).
For that matter, you can use any executable file as a CGI script program. If you compile, say, a C, C++, or Ada program to create an executable file, then install it in an appropriate directory with a .cgi
suffix on the file name, the web server can execute it directly. It's not commonly done, because scripting languages tend to be move convenient for this kind of thing, but the web server doesn't care what kind of executable file it's invoking, or how the kernel invokes it.
(The details are likely to be a bit different on Windows, which usually uses the file extension to determine how to launch an executable.)
Upvotes: 1