【 WSHをCGIとして使う 】
1. HTTP サーバへの登録
2. WSH のスケルトン
3. 表示テスト
4. FORM method=get をテスト
5. FORM method=post をテスト
6. 入力処理の為の標準化

AN HTTP Server
  • IIS があるのならば、ASP を使えば良いので AN HTTP Server を使う事に
    します

  • 設定は、一般タブで .wsf を登録して実行プログラム欄に以下のように
    入力します

  •  
     cscript.exe //Nologo
    


    ' ***********************************************************
    ' wsh で job 要素は実行する為に最低限必要です
    ' ***********************************************************
    <job>
     
    ' ***********************************************************
    ' オブジェクト要素で、使用するオブジェクトを定義
    ' ***********************************************************
    <object id="Cn" progid="ADODB.Connection" />
    <object id="Rs" progid="ADODB.Recordset" />
    <object id="Form" progid="Scripting.Dictionary" />
    <object id="QueryString" progid="Scripting.Dictionary" />
    <object id="Shell" progid="WScript.Shell" />
     
    ' ***********************************************************
    ' リファレンス要素で、タイプライブラリを使用します
    ' ***********************************************************
    <reference object="ADODB.Connection" />
     
    ' ***********************************************************
    ' スクリプトをインクルードする為の記述です
    ' ***********************************************************
    <script language="VBScript" src="wsh\function.vbs"></script>
     
    ' ***********************************************************
    ' スクリプトの開始
    ' ***********************************************************
    <script language="VBScript">
    print "Content-Type: text/html; Charset=Shift_JIS"
    print ""
     
    </script>
    </job>
    
  • print は function.vbs で定義している sub です

  • WScript.Shell は、環境変数の取得に使用します

  • Scripting.Dictionary は、連想配列として使用します


  • ' ******************************************************
    ' 標準出力
    ' ******************************************************
    sub print( strValue )
     
    	WScript.echo strValue
     
    end sub
    


    print "Content-Type: text/html; Charset=Shift_JIS"
    print ""
     
    print "<TABLE border=0 cellpadding=5 cellspacing=1 bgcolor=black>"
    print "<TH bgcolor=silver>対象</TH><TH bgcolor=silver>値</TH>"
     
    ' ----------------------------------------------------------
    print "<TR>"
    td "スクリプト名"
    td Wscript.ScriptFullName
    print "</TR>"
     
    ' ----------------------------------------------------------
    print "<TR>"
    td "WSH バージョン"
    td Wscript.Version
    print "</TR>"
     
    ' ----------------------------------------------------------
    ' タイプライブラリが有効か確認
    print "<TR>"
    td "adOpenKeyset"
    td adOpenKeyset
    print "</TR>"
     
    ' ----------------------------------------------------------
    ' タイプライブラリが有効か確認
    print "<TR>"
    td "adOpenDynamic"
    td adOpenDynamic
    print "</TR>"
     
    ' ----------------------------------------------------------
    ' 環境変数 --> Process を使用
    Set Env = Shell.Environment("Process")
    print "<TR>"
    td "REQUEST_METHOD"
    td Env("REQUEST_METHOD")
    print "</TR>"
     
    print "</TABLE>"
    
  • 以下は実行結果です
  • 対象
    スクリプト名 D:\winofsql\cmd\sample\wsh01.wsf
    WSH バージョン 5.6
    adOpenKeyset 1
    adOpenDynamic 2
    REQUEST_METHOD GET


    ' ******************************************************
    ' TD
    ' ******************************************************
    sub td( strValue )
     
    	WScript.echo "<TD bgcolor=white>" & strValue & "</TD>"
     
    end sub
    


    print "Content-Type: text/html; Charset=Shift_JIS"
    print ""
     
    ' ----------------------------------------------------------
    ' フォーム
    print "<FORM method=get >"
    print "<INPUT type=text name='Field1'>"
    print "<INPUT type=text name='Field2'>"
    print "<INPUT type=submit name=send value='送信'>"
    print "</FORM>"
     
    ' ----------------------------------------------------------
    ' テーブル
    print "<TABLE border=0 cellpadding=5 cellspacing=1 bgcolor=black>"
    print "<TH bgcolor=silver>対象</TH><TH bgcolor=silver>値</TH>"
     
    ' ----------------------------------------------------------
    ' 環境変数 --> Process を使用
    Set Env = Shell.Environment("Process")
    print "<TR>"
    td "QUERY_STRING"
    td Env("QUERY_STRING")
    print "</TR>"
     
    ' ----------------------------------------------------------
    ' GET データ
    aData = Split( Env("QUERY_STRING"), "&" )
     
    ' ----------------------------------------------------------
    if Ubound( aData ) >= 0 then
    	' デコードデスト
    	print "<TR>"
    	td "Field1"
    	aKeyValue = Split(aData(0), "=" )
    	td Decode(aKeyValue( 1 ))
    	print "</TR>"
     
    	print "<TR>"
    	td "Field2"
    	aKeyValue = Split(aData(1), "=" )
    	td Decode(aKeyValue( 1 ))
    	print "</TR>"
     
    	print "<TR>"
    	td "send"
    	aKeyValue = Split(aData(2), "=" )
    	td Decode(aKeyValue( 1 ))
    	print "</TR>"
     
    end if
     
    print "</TABLE>"
    


  • おきまりの、%XX+デコード関数は以下のようになります
  • ' ******************************************************
    ' % DECODE と + の変換
    ' ******************************************************
    function Decode( strValue )
     
    	Dim strChar,strConvert,i,nCode
     
    	strConvert = ""
     
    	For i = 1 to Len( strValue )
    		strChar = Mid( strValue, i, 1 )
    		if strChar = "%" then
    			i = i + 1
    			strChar = Mid( strValue, i, 2 )
    			nCode = Cint( "&H" & strChar )
    			if &H81 <= nCode and nCode <= &H84 or _
    				&H88 <= nCode and nCode <= &H9f or _
    				&HE0 <= nCode and nCode <= &HEA then
    				i = i + 2
    				if Mid( strValue, i, 1 ) = "%" then
    					i = i + 1
    					strChar = strChar & Mid( strValue, i, 2 )
    					nCode = Cint( "&H" & strChar )
    					strConvert = strConvert & Chr(nCode)
    					i = i + 1
    				else
    					strChar = strChar & Hex(Asc( Mid( strValue, i, 1 ) ))
    					nCode = Cint( "&H" & strChar )
    					strConvert = strConvert & Chr(nCode)
    				end if
    			else
    				strConvert = strConvert & Chr(nCode)
    				i = i + 1
    			end if
    		else
    			if strChar = "+" then
    				strConvert = strConvert & " "
    			else
    				strConvert = strConvert & strChar
    			end if
    		end if
    	Next
     
    	Decode = strConvert
     
    end function
    


  • 以下は実行結果です
  • 対象
    QUERY_STRING Field1=%8A%BF%8E%9A&Field2=%95%5C%8E%A6&send=%91%97%90M
    Field1 漢字
    Field2 表示
    send 送信


    print "Content-Type: text/html; Charset=Shift_JIS"
    print ""
     
    ' ----------------------------------------------------------
    ' フォーム
    print "<FORM method=post >"
    print "<INPUT type=text name='Field1'>"
    print "<INPUT type=text name='Field2'>"
    print "<INPUT type=submit name=send value='送信'>"
    print "</FORM>"
     
    ' ----------------------------------------------------------
    ' テーブル
    print "<TABLE border=0 cellpadding=5 cellspacing=1 bgcolor=black>"
    print "<TH bgcolor=silver>対象</TH><TH bgcolor=silver>値</TH>"
     
    ' ----------------------------------------------------------
    ' 環境変数 --> Process を使用
    Set Env = Shell.Environment("Process")
    if Env("REQUEST_METHOD") = "POST" then
     
    	' ----------------------------------------------------------
    	' POST データ
    	strPost = Wscript.StdIn.ReadAll
    	print "<TR>"
    	td "Form"
    	td strPost
    	print "</TR>"
     
    	aData = Split( strPost, "&" )
     
    	' ----------------------------------------------------------
    	' デコードデスト
    	print "<TR>"
    	td "Field1"
    	aKeyValue = Split(aData(0), "=" )
    	td Decode(aKeyValue( 1 ))
    	print "</TR>"
     
    	print "<TR>"
    	td "Field2"
    	aKeyValue = Split(aData(1), "=" )
    	td Decode(aKeyValue( 1 ))
    	print "</TR>"
     
    	print "<TR>"
    	td "send"
    	aKeyValue = Split(aData(2), "=" )
    	td Decode(aKeyValue( 1 ))
    	print "</TR>"
     
    end if
     
    print "</TABLE>"
    
  • Wscript.StdIn.ReadAll は、標準入力の読み込みです
  • 以下は実行結果です
  • 対象
    Form Field1=%8A%BF%8E%9A&Field2=%95%5C%8E%A6&send=%91%97%90M
    Field1 漢字
    Field2 表示
    send 送信


    ' ******************************************************
    ' 初期処理
    ' ******************************************************
    sub InitForCgi
     
    	Dim i,aData,aKeyValue
     
    	Set Env = Shell.Environment("Process")
     
    	aData = Split( Env("QUERY_STRING"), "&" )
    	For i = 0 to Ubound( aData )
    		aKeyValue = Split(aData(i), "=" )
    		Call QueryString.Add( aKeyValue(0), Decode(aKeyValue(1)) )
    	Next
     
    	if Env("REQUEST_METHOD") = "POST" then
    		aData = Split( Wscript.StdIn.ReadAll, "&" )
    		For i = 0 to Ubound( aData )
    			aKeyValue = Split(aData(i), "=" )
    			Call Form.Add( aKeyValue(0), Decode(aKeyValue(1)) )
    		Next
    	end if
     
    end sub
    
  • この関数を実行する事によって以下のように書けるようになります
  • print "Content-Type: text/html; Charset=Shift_JIS"
    print ""
     
    ' ----------------------------------------------------------
    ' CGI 用
    InitForCgi
     
    ' ----------------------------------------------------------
    ' フォーム
    print "<FORM method=post>"
    print "<INPUT type=text name='Field1' value='" & Form("Field1") & "'>"
    print "<INPUT type=text name='Field2' value='" & Form("Field2") & "'>"
    print "<INPUT type=submit name=send value='送信'>"
    print "</FORM>"
     
    ' ----------------------------------------------------------
    ' テーブル
    print "<TABLE border=0 cellpadding=5 cellspacing=1 bgcolor=black>"
    print "<TH bgcolor=silver>対象</TH><TH bgcolor=silver>値</TH>"
     
    ' ----------------------------------------------------------
    if Env("REQUEST_METHOD") = "POST" then
     
    	' ----------------------------------------------------------
    	' ディクショナリテスト
    	print "<TR>"
    	td "Field1"
    	td Form( "Field1" )
    	print "</TR>"
     
    	print "<TR>"
    	td "Field2"
    	td Form( "Field2" )
    	print "</TR>"
     
    	print "<TR>"
    	td "send"
    	td Form( "send" )
    	print "</TR>"
     
    end if
     
    print "</TABLE>"
     
    
  • 以下は実行結果です


  • 対象
    Field1 表示
    Field2 テスト
    send 送信