2

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.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • 4
    The `#!` thing isn't part of perl. It's called a "shebang". It's a Unix convention used for any sort of text "script". – Mat Jul 29 '12 at 15:47
  • so what if i wanted to use cgi in some other operating system? – Faruk Mustafic Jul 29 '12 at 15:48
  • 4
    Depends on the webserver. Either configure it to treat .cgi files as perl, or it already reads the first line to try and determine what type of file it is. – Mat Jul 29 '12 at 15:49
  • 2
    See [Why do people write #!/usr/bin/env python on the first line of a Python script?](http://stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script/2429515#2429515) and [How do I ignore the Perl shebang on Windows with Apache 2?](http://stackoverflow.com/questions/2036577/how-do-i-ignore-the-perl-shebang-on-windows-with-apache-2/2036584#2036584) – Sinan Ünür Jul 29 '12 at 16:55
  • @Mat your comments should be an answer – Jim Garrison Jul 29 '12 at 18:19
  • 1
    @Mat: Yes, the shebang is a feature of the kernel on Unix-like systems -- but the Perl interpreter also pays attention to it. See [`perldoc perlrun`](http://perldoc.perl.org/perlrun.html). – Keith Thompson Jul 29 '12 at 22:25

2 Answers2

3

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 Included 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.

daxim
  • 38,078
  • 4
  • 57
  • 123
Borodin
  • 123,915
  • 9
  • 66
  • 138
  • This doesn't answer the question: "what is the actual mechanism that lets the server know it's a Perl script and not something else?" – brian d foy Jul 30 '12 at 16:22
  • @brian: I took that to mean "how does the server know it's a CGI script and not a text resource". Perhaps I'm wrong. Hopefully the OP will tell us. – Borodin Jul 30 '12 at 16:32
1

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.)

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578