この文書は Runtime社のホームページにある、http://www.runrev.com/revolution/developers/articles/tipoftheweek/6.html を邦訳したものです。この文書の文責はUDIにあり、またUDIはこの文書についての一切の債務を負いません。 間違いがありましたら eudio@chabashira.co.jp までお知らせ下さい。この文書は必要と思われる時に適宜アップデートされます。
Developers> Articles> Tip of the Week!
Tip of the Week! 6
サーバサイドスクリプティングの基礎
この Tip of the Week は開発者コミュニティメンバーの寄稿による。
これは Revolution をフェースレスアプリケーションとして使うための、サーバサイドスクリプティングの基礎である。
1 プロバイダ、またはウェブサーバで使っているプラットフォーム向けの Revolutionエンジンを、以下のURLからダウンロードして解凍する:
http://www.runrev.com/revolution/engines11
サーバの cgi-bin ディレクトリにエンジンをアップロードし、そのエンジンのパーミッションを CHMOD 755 にセットする。
2 スクリプトの第1行目には、アップロードした Revolutionエンジンのサーバ上のパスを入れる。例えば:
#!/home/httpd/cgi-bin/revolution
3 スクリプトには必ず "on startUp" ハンドラを入れる。 UNIX サーバは CGI リクエストが来る度にアプリケーションから最小限の「実体」を作ってキャッシュに入れるので、心配することはない。スクリプトはこんな感じになる:
#!/home/httpd/cgi-bin/revolution
-- これは "images" フォルダからランダムにイメージを取り出して、
-- 他のファイルに貼り付けるスクリプトである。
-- このイメージを埋め込んだ html ファイルは毎回違うイメージを表示する。
-- 例えばサイトに毎日違うイメージを表示するというようなことも可能である。
on startUp
set the directory to "../images"
put random(50) into tFileNo
put "artfile" &tFileNo&".gif" into tArtFile
put URL ("binfile:art_header/"&tArtFile) into URL ("binfile:art_output.gif")
put "Done" into tResult
put "Content-Type: text/html" & cr
put "Content-Length:" && the length of theResult & cr & cr
put theResult
end startUp
|
このスクリプトを動かすために、このファイル(訳注:拡張子 ".cgi" を付けた、LF 改行のテキストファイル)を cgi-bin ディレクトリに保存して、同じようにパーミッション CHMOD 755 をセットする。
サンプルをもうひとつ:
#!/home/httpd/cgi-bin/revolution
-- これは簡単な加入申し込みフォームを処理して、
-- メールを送り返すスクリプトである。
on startUp
read from stdin until empty
put urlDecode (it) into tDataIn
split tDataIn by "&" and "="
put isWellFormedMailtoScheme (tDataIn ["email"]) into emailCheck
-- 訳注: stdin は「標準入力」。ここではインラインで指定された文字列を取り込んでいる。
-- split は文字列を配列に分ける命令。分割後の要素は例えば tDataIn[2] のように配列として
-- アクセス可能だが、ここではsplit に第2キーワードを指定することで、連想配列のような
-- ものを作っている。例えば "email=" に続く単語は tDataIn ["email"] で取得可能。
switch emailCheck
case "False"
put badEmail(fixYourEmail) into tResponse
break
case "True"
put tDataIn ["subscribe_rev"] into tRemit
put tDataIn ["email"] after tRemit
put "---Valid Web Subscription--" & cr before tRemit
put "/usr/lib/sendmail -t" into mprocess
open process mprocess for write
write "From:" && "webmaster@whatever.com" & cr to process mprocess
write "To:" && "mylistAdmin@someListServe.org" & cr to process mprocess
write "Subject:" && "Subscriber to Something" & cr & cr to process
mprocess
write tRemit to process mprocess
close process mprocess
wait until the openprocesses is empty
break
-- HTTP を使って承認メールを送る
put url "file:../revolution/subscr-rev_thankyou.shtml" into tResponse
replace "***Subscriber***" with tDataIn ["email"] in tResponse
end switch
put "Content-Type: text/html" & cr
put "Content-Length:" && the length of tResponse & cr & cr
put tResponse
end startUp
-- この関数はいつか正規表現を使って書き直されることになるだろう。
function isWellFormedMailtoScheme email
-- email が妥当な URL なら TRUE を返し、そうでなければ FALSE を返す
-- 実際にそのアドレスを調べているのではなく、単に構文を見ているだけである。
-- アドレス構文規則 RFC822: Standard for ARPA Internet Text Messages
-- http://www.w3.org/Protocols/rfc822/Overview.html
-- 基本的な構文は、@ の前にいくつかの文字があるというものである
split email by "@"
if extents(email) <> "1,2" then return false -- 署名には @ がひとつある
put email[2] into hostanddomain
-- 訳注: split は文字列を配列に分ける命令。分割後の要素は email[2] のように配列としてアクセス可能。
-- extents は配列の要素番号の範囲を返す関数で、"1,2" なら配列の要素は1から2までの2つ。
-- つまりここでは email 内に @ が1つあることを確認し、2つめの要素を hostanddomain に入れている。
-- ドメイン名のチェックには、domain-literal(厳密なドメイン名)と、
-- domain-logical(論理的なドメイン名)の2つの方法がある:
-- domain-literal チェック:
-- [###.###.###.###] で表されるネットワークホストアドレス。
-- 0 < # < 256
if char 1 of hostanddomain = "[" then
if not last char of hostanddomain = "]" then return false
delete char 1 of hostanddomain
delete last char of hostanddomain
set the itemDel to "."
if the num of items of hostanddomain <> 4 then return false
repeat with x = 1 to 4
if not isNumber(item x of hostanddomain) then return false
if item x of hostanddomain > 255 or item x of hostanddomain < 1 then
return false
end repeat
return true
end if
-- domain-logical チェック(一般的な形式):
-- この形式は "." キャラクタを使っていくつかの部分に分けることが出来、
-- その最後がドメイン名になる。
set the itemDel to "."
put the num of items of hostanddomain into hostanddomainItems
if hostanddomainItems = 0 then return false
if hostanddomain contains ".." then return false -- empty hosts not allowed
repeat with x = length(hostanddomain) down to 1
if not ("0123456789.-abcdefghijklmnopqrstuvwxyz_" contains char x of hostanddomain) then return false
end repeat
return true
end isWellFormedMailtoScheme
function badEmail
put "<html><head><Title>メールアドレスが間違ってる!"\
& "</TITLE></HEAD><CENTER><BODY BGCOLOR=#ffffff> <FONTcolor=FF0000><h3>ごめんよ、"\
& "あんたの書いたメールアドレスをうまく処理出来なかった。<br>"\
& "戻ってやり直してくれるかな。悪いね。</H3></FONT></CENTER></BODY></HTML>" into fixYourEmail
return fixYourEmail
end badEmail
|
邦訳/文責:UDI
2002.09.12
2002.10.27