CGI: VBScript CGI

Revision as of 20:35, 11 November 2011 by BobS (talk | contribs) (Minor cleanup)

VBScript CGI

This page covers running VBScrpits as CGI scripts and introduces the CGI shebang. It assumes the Apache server has been configured to permit the running of CGI scripts, and test folder vbs_test and .htaccess have been created as explained on the previous page.

Test Script 1

Inside folder vbs_test, create a new file test1.vbs with the following content:

Wscript.Echo "<h1>VBScript CGI Test</h1>"
Wscript.Quit 0

This script writes (echoes) the string "<h1>VBScript CGI Test</h1>" to the standard output stream followed by exit code 0 (Wscript.Quit 0).

Run Script on server

Run the script as follows:

  • Start the Apache server (double click on server_start.bat)
  • In your browser type: http://localhost:8081/vbs_test/test1.vbs

Note: If you are using The Uniform Server, the port is not required. Instead type the following into your browser: http://localhost/vbs_test/test1.vbs

The script fails

Browser message and entry in Apache log file are shown on the right.

Reason script fails; Apache does not know how to locate the program that will run this script!

Windows uses file association to appropriately run programs; it checks the file extension and runs the appropriate program that has been assigned to that extension. Apache does not use this mechanism, CGI scripts can have any file extension for example a script written in VBScript has the extension .vbs one written in Vfred may have the extension .fred ; all Apache knows is that the extension is associated with a CGI script.

In reality, Apache does know where to find the executable program! Every CGI script must contain a line pointing to the program that will be used to run the script. This line is referred to as the shebang.

### Browser ###
500 Internal Server Error:
The server encountered an internal error or misconfiguration
and was unable to complete your request.

Please contact the server administrator, admin@localhost and
inform them of the time the error occurred, and anything you
might have done that may have caused the error.

More information about this error may be available in the
server error log.

### Apache Log file ###
[Mon Aug 29 13:25:00 2011] [error] [client 127.0.0.1]
(9)Bad file descriptor: don't know how to spawn child process:
C:UniServer/www/vbs_test/test1.vbs

Add a shebang

A CGI VBScript requires a shebang to inform Apache what program is required to run the script. It has the following format:

'!c:/windows/system32/cscript //nologo

Note:

The shebang is '! followed by the path to the executable that will run the script, which in this case is the Windows Script Host cscript.exe
The //nologo prevents cscript from displaying a banner, otherwise a malformed header error would be produced.

Content-type

Following the shebang, a content type header is required. This also is obligatory, otherwise a malformed header error would be produced.

Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Note:

The second vbLF produces a blank line. This is required before any content is output.

Complete test script

  • Edit file test1.vbs to have the contents as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test1.vbs
    (or refresh browser page)
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Wscript.Echo "<h1>VBScript CGI Test 1</h1>"
Wscript.Quit 0

The script produces output as shown on right.

VBScript CGI Test 1

Test Script 2

The above example, although functional, produces only static content and you would be better off using a standard html page. The real reason for writing CGI scripts is to produce dynamic content. The following script expands on the above template (test1.vbs) to display the current date and time.

  • Create a new file test2.vbs with the contents as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test2.vbs
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Wscript.Echo "<h1>VBScript CGI Test 2</h1>"
WScript.Echo "<p>Today's date is: " & Date & "</p>"
WScript.Echo "<p>The current time is: " & Time & "</p>"
Wscript.Quit 0

The script produces output as shown on right.
Note: Date and time are defined by your PC
Refresh page and note that the time updates (dynamic page).

VBScript CGI Test 2

Today's date is: 21/09/2011

The current time is: 18:05:12

Test Script 3

Although the majority of html tags are optional you do need to
include a minimum set of tags as per a standard html page.
Otherwise the page may fail when opened in different browsers.

  • Create a new file test3.vbs with the contents as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test3.vbs
'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

WScript.Echo "<html>"
WScript.Echo "<title>Test 3</title>"
WScript.Echo "<body>"

Wscript.Echo "<h1>VBScript CGI Test 3</h1>"
WScript.Echo "<p>Today's date is: " & Date & "</p>"
WScript.Echo "<p>The current time is: " & Time & "</p>"

WScript.Echo "</body>"
WScript.Echo "</html>"

Wscript.Quit 0

The script produces output as shown on right.

Note: The page title is now displayed on the browser tab and in
the browser title bar. There are no other changes in output.

VBScript CGI Test 3

Today's date is: 21/09/2011

The current time is: 18:05:12


Summary

Every CGI script must contain a shebang as the first line of any script. This informs Apache what scripting engine to use. The Content-type header for the page must follow this line.

The page template is shown below:

'!c:/windows/system32/cscript //nologo
Wscript.Echo "Content-type: text/html" & vbLF & vbLF

Running VBScripts on The Uniform Server and Coral-Mini requires an .htaccess file be placed in the directory containing the scripts. This file enables CGI script execution and has the following content:

AddHandler cgi-script .vbs 
Options +ExecCGI
Options +FollowSymlinks

Where to next

The next page covers adding a banner and footer using include files.