Apache + CGI再入門

いつもフレームワークの上でしか書いてないので、きちんと理解したい。

CGI Specification

http://httpd.apache.org/docs/2.0/en/howto/cgi.html

The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs, which are often referred to as CGI programs or CGI scripts. It is the simplest, and most common, way to put dynamic content on your web site. This document will be an introduction to setting up CGI on your Apache web server, and getting started writing CGI programs.

つまり、CGIというのは外部プログラムとの協調方法を定義するもの(=仕様)


http://www.ietf.org/rfc/rfc3875.txt

The CGI defines the abstract parameters, known as meta-variables,
which describe a client's request. Together with a concrete
programmer interface this specifies a platform-independent interface
between the script and the HTTP server.

The server is responsible for managing connection, data transfer,
transport and network issues related to the client request, whereas
the CGI script handles the application issues, such as data access
and document processing.


'meta-variable'
A named parameter which carries information from the server to the
script. It is not necessarily a variable in the operating
system's environment, although that is the most common
implementation.

'script'
The software that is invoked by the server according to this
interface. It need not be a standalone program, but could be a
dynamically-loaded or shared library, or even a subroutine in the
server. It might be a set of statements interpreted at run-time,
as the term 'script' is frequently understood, but that is not a
requirement and within the context of this specification the term
has the broader definition stated.

'server'
The application program that invokes the script in order to
service requests from the client.

単純化すれば、CGIはリクエストをmeta-variablesで表現し、HTTPサーバはリクエストを処理し、CGI scriptをinvokeし、データをクライアントに戻す責務を持つ、と。


で、Apacheのドキュメントを再度みてみる。
http://httpd.apache.org/docs/2.0/ja/howto/cgi.html

サーバとクライアント間のもう一つの通信は、標準入力 (STDIN)と標準出力 (STDOUT) を通じて行なわれます。通常の文脈において、STDIN はキーボードやプログラムが動作するために与えられるファイルを意味し、 STDOUT は通常コンソールまたはスクリーンを意味します。

ウェブフォームから CGI プログラムへPOST したとき、フォームのデータは特別なフォーマットで束ねられ、 STDIN を通して、CGI プログラムに引き渡されます。 プログラムはデータがキーボード もしくはファイルから来ていたかのように処理することができます。

なるほど。完全に理解。

exec CGI

CentOS + ApacheperlpythonのCGI scriptを試してみる。
まず、/etc/httpd/conf/httpd.confにScriptAliasを設定する

ScriptAlias /cgi-bin/ /home/kotaro/cgi-bin/


次いで、/home/kotaro/cgi-binにperl.cgiとpython.cgiを作成。もちろんpermissionは適切に設定する。

#!/usr/bin/perl

print "Content-type: text/html\n\n";
for my $key (keys %ENV) {
    printf "%s => %s<br>", $key, $ENV{$key};
}
#!/usr/bin/python
import os

print "Content-type: text/html\n\n"
for k, v in os.environ.iteritems():
    print "%s: %s<br>\n" % (k, v)

これでhttp://localhost/cgi-bin/perl.cgi, http://localhost/cgi-bin/python.cgiにアクセスすれば、環境変数が表示される。

まとめ

  • CGIはHTTPリクエストに対して、CGI scriptをinvokeし、環境変数と標準入力を渡す
  • 環境変数にはREQUEST_URI, QUERY_STRINGなどが入り、標準入力にはPOSTデータが入る
  • CGI scriptはContent-TypeとHTTP Bodyを標準出力に出す