CGI: VBScript CGI

From The Uniform Server Wiki
Revision as of 23:16, 7 November 2011 by Ric (talk | contribs) (Created page with "{{Nav CGI}} '''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 runn...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 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) 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 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 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 extention .fred all Apache knows is the extension is assocuted 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 informing 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 executable that will run the script in this case the Windows Script Host cscript.exe
The //nologo prevents cscript displaying a banner otherwise a malformed header error is produced.

Content-type

Following the shebang a content type header is required. This is obligatory otherwise again a malformed header error is 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 content 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

Top

Test Script 2

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

  • Create a new file test2.vbs with content 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 defined by your PC
Refresh page and note the time updates (dynamic page).

VBScript CGI Test 2

Today's date is: 21/09/2011

The current time is: 18:05:12

Top

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 break when opened in different browsers.

  • Create a new file test3.vbs with content 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
browser title bar. There are no major changes in output.

VBScript CGI Test 3

Today's date is: 21/09/2011

The current time is: 18:05:12

Top

Summary

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

Page template shown below:

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

Running VBScripts on Uniform Server and Coral-mini requires an .htaccess file 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

Top

Where to next

Next page covers adding a banner and footer using include files.

Top