QUERY_STRINGに ?code=<値> を設定することで、<値> をHTTPステータスコードとして応答するサンプル。異常系の考慮はまったくないテスト用。
batを使う場合
IIS > ハンドラーマッピング > スクリプトマップの追加
要求パス: *.bat
実行可能ファイル: C:\Windows\System32\cmd.exe /c "%s"
名前: CGI-bat
@echo off
rem CGI(BAT) SAMPLE. %QUERY_STRING%= code=<statuscode>
rem get code from environment.
for /f "tokens=2 delims==" %%a in ("%QUERY_STRING%") do (
set "code=%%a"
)
rem make response.
echo status: %code%
echo Content-Type: text/plain
echo.
echo STATUSCODE = %code%
echo %date% %time:~0,8%
PowerShellを使う場合
IIS > ハンドラーマッピング > スクリプトマップの追加
要求パス: *.ps1
実行可能ファイル: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -ExecutionPolicy Bypass -NoProfile -NonInteractive -File "%s"
名前: CGI-PowerShell
IIS > CGI > 起動ごとに新しいコンソールを使用する をTrue にする
※これをしないと 502.2 BadGatewayでハマる。batは No でも動く。
# CGI(PowerShell) SAMPLE. $QUERY_STRING= code=<statuscode>
# get code from environment.
$code = ($env:QUERY_STRING -split "=")[1]
# make response.
Write-Output "status: $code"
Write-Output "Content-Type: text/plain"
Write-Output ""
Write-Output "STATUSCODE = $code"
Get-Date -Format "yyyy/MM/dd HH:mm:ss"
Linux/Apache HTTP Server を利用する場合
<IfModule alias_module>
・・・
#
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client. The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI ←★ここを追加
Require all granted
</Directory>
<IfModule mime_module>
・・・
# AddHandler allows you to map certain file extensions to "handlers":
# actions unrelated to filetype. These can be either built into the server
# or added with the Action directive (see below)
#
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
AddHandler cgi-script .sh ←★コメントを外して、拡張子を編集
・・・
</IfModule>
/var/www/http の Directoryタグに AddHanderとOptions ExecCGI追加して、script.sh を配置するのでもOK
/var/www/cgi-bin/ に script.sh を作成し、chmod +x で実行権限を付与。
#!/bin/bash
# CGI(bash) SAMPLE. $QUERY_STRING= code=<statuscode>
# get code from environment.
code=$(echo $QUERY_STRING | cut -d= -f2)
# make response.
echo status: $code
echo Content-Type: text/plain
echo
echo STATUSCODE = $code
date +"%Y/%m/%d %H:%M:%S"