The ASP Emporium
Free Active Server Applications and Examples by Bill Gearhart
Online since Friday January 7, 2000

 home > code > code library > Debugging Classic ASP > Debugger Object

enter a phrase to search: (advanced search)


 h o m e 

 w h a t 's  n e w 

 a l l   c o d e 
  .net:
    • Fundamentals
    • C# Classes
  classic asp:
    • Code Library
    • ASP Apps
  general:
    • Tutorials
    • SQL

 d o w n l o a d s 

 u s e r   f o r u m s 

 l i n k s 

 s e a r c h 

 s u p p o r t 


Debugger Object   v1.0   [VBScript]

< prev proc
URLDecode Function
next proc >
GetHeaders Function

purpose:
It's easy to find out everything about your ASP environment. 
This class compresses all the throw-away code we write everyday 
into one object but it does it with a twist... This object makes 
heavy use of the Execute method of VBScript. If you aren't familiar
with that function, it allows you to execute ASP code as a string. 
If this doesn't make a lightbulb appear over your head, view the 
source to this and see how I used it to achieve 0 code reuse! 

10 methods cover everything from: 
 - dumping passed inputs (form/querystring/cookies/clientcertificates) 
 - checking all ASP property settings 
 - testing server I/O (delete/move/copy/read/write/create) with text files 
 - dumping persisting data and objects (session/application) 
 - gathering scripting engine info (VBScript/JScript). 

This object lets you know almost everything there is to know about 
your server's environment. 
syntax:
Set object = New Debugger
example usage:
Dim oDebug

Set oDebug = New Debugger
With oDebug
	.EndAfterDebug = False
	.DumpASPProperties
'	.DumpScriptingEngines
'	.DumpApplication
'	.DumpSession
'	.DumpForm
'	.DumpQueryStrings
'	.DumpClientCertificates
'	.DumpCookies
'	.DumpServerVariables
'	.TestServerIO
End With
Set oDebug = Nothing
source code:
'========== START CLASS DEFINITION ==================
'========== START DOCUMENTATION =====================
'****************************************************
'Debugger Object                                 v1.0
'http://www.aspemporium.com/
'****************************************************
' Purpose:
'  The debugger object allows an ASP programmer to see
'  most default settings in the ASP Environment including
'  all inputs passed to the current page, any persisting
'  session or application data, and lets a programmer
'  fully test server I/O for the directory where the 
'  class is being called from.
'  
'  
'  
' Version History:
'  Version 1.0    June 6, 2001
'   - initial release
'  
'  
'  
' Syntax:
'  Dim oDebug
'
'  Set oDebug as New Debugger
'  oDebug.[property[ = val]|method[()]]
'  
'  
'  
' Properties:
'  All properties are optional and have default values
'  if not explicitly set.
'
'   obj.EndAfterDebug[ = boolean]
'     Optional. Boolean. Defaults to false.
'     True will perform a response.end when the class
'     has been set to Nothing. Nothing below the call
'     to the class will execute. Useful for debugging.
'  
'  
'  
' Methods:
'  All methods work independently of each other and
'  are standalone procedures. They can be called in
'  any order and you need only call the one's you 
'  want to.
'
'   obj.TestServerIO()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Thoroughly tests server I/O in the calling
'     directory.
'     This method creates three tester text files
'     in the current directory (wherever the class
'     is called from). TestServerIO tests the 
'     following I/O functions:
'        delete, move, copy, read, write, create
'     TestServerIO returns information and/or errors
'     about each function if it fails. Very useful for 
'     making sure your IUSR_machinename account has 
'     the proper permissions in the virtual server.
'
'  
'   obj.DumpASPProperties()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns all property values for the ASP objects.
'     Returns property settings for these properties:
'
'       Session Properties
'         Session.CodePage
'         Session.LCID
'         Session.SessionID
'         Session.Timeout
'
'       Server Properties
'         Server.ScriptTimeout
'
'       Response Properties
'         Response.Buffer
'         Response.CacheControl
'         Response.Charset
'         Response.ContentType
'         Response.Expires
'         Response.ExpiresAbsolute
'         Response.IsClientConnected
'         Response.Status
'
'       Request Properties
'         Request.TotalBytes
'
'  
'   obj.DumpScriptingEngines()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns information about the VBScript and JScript
'     scripting engines on the server.
'  
'
'   obj.DumpApplication()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns information about application variables
'     and application scope object references.
'     
'  
'   obj.DumpSession()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns information about session variables
'     and session scope object references.
'  
'
'   obj.DumpForm()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns all form inputs available on the page.
'  
'
'   obj.DumpQueryStrings()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns all query string inputs available on the
'     page.
'  
'
'   obj.DumpClientCertificates()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns all client certificate available on the
'     page.
'  
'
'   obj.DumpCookies()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns all cookie available on the page.
'  
'
'   obj.DumpServerVariables()
'     Void. No Return Value. Prints results using
'     Response.Write()
'     Returns all server variables available on the
'     page.
'  
'  
'  
' Example Usage:
'  
'  1.) test I/O on the server.
'
'    Dim oDebug
'
'    Set oDebug = New Debugger
'    oDebug.EndAfterDebug = True
'    oDebug.TestServerIO
'    Set oDebug = Nothing
'    
'      -- other code ---
'
'  2.) show all form and query string inputs passed
'      to the page.
'
'    Dim oDebug
'
'    Set oDebug = New Debugger
'    oDebug.EndAfterDebug = True
'    oDebug.DumpForm
'    oDebug.DumpQueryStrings
'    Set oDebug = Nothing
'    
'      -- other code ---
'    
'  
'  
' Requirements:
'  VBScript 5.1 or better 
'  JScript 5.1 or better
'  ASP 2.0 or better
'  Scripting Runtime Library (required for 
'                 TestServerIO method only)
'  
'  
'  
' Known Bugs:
'  There are no known bugs.
'  
'  
'  
' Disclaimer/Notes:
'  View the license:
'    http://www.aspemporium.com/aspEmporium/help/license.asp
'
'  Report all bugs or suggestions to the forum for this example:
'    http://www.aspemporium.com/aspEmporium/forum/display_forum.asp?fid=7
'  
'  
'  
'****************************************************
'==========  END DOCUMENTATION  =====================
%>
<SCRIPT LANGUAGE=JScript RUNAT=SERVER>
function JSScriptEngine() {
	Response.Write
		(
			String.fromCharCode(9) + "<B>" + ScriptEngine() + "</B>" + 
			String.fromCharCode(13,10) + String.fromCharCode(9) + 
			String.fromCharCode(9) + ScriptEngineMajorVersion() + "." + 
			ScriptEngineMinorVersion() + "." + ScriptEngineBuildVersion() + 
			String.fromCharCode(13,10) + String.fromCharCode(13,10)
		 );
}
</SCRIPT>
<%
Class Debugger

	'####################################
	'#      Public Class Properties     #
	'####################################

	Public EndAfterDebug



	'####################################
	'#      Public Class Methods        #
	'####################################

	Public Sub DumpASPProperties()
		'go through each asp object property and print them all.
		Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
		Response.Write("<BIG>Session Properties</BIG>" & vbCrLf)

		'to facilitate results and eliminate duplicate code, I'm 
		'generating a string of asp code and then executing the 
		'string using the Execute procedure that is built into VBScript. 
		Execute ExecForASPProp("Session.CodePage")
		Execute ExecForASPProp("Session.LCID")
		Execute ExecForASPProp("Session.SessionID")
		Execute ExecForASPProp("Session.Timeout")
		Response.Write("</PRE>")
		Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
		Response.Write("<BIG>Server Properties</BIG>" & vbCrLf)
		Execute ExecForASPProp("Server.ScriptTimeout")
		Response.Write("</PRE>")
		Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
		Response.Write("<BIG>Response Properties</BIG>" & vbCrLf)
		Execute ExecForASPProp("Response.Buffer")
		Execute ExecForASPProp("Response.CacheControl") 
		Execute ExecForASPProp("Response.Charset")
		Execute ExecForASPProp("Response.ContentType")
		Execute ExecForASPProp("Response.Expires")
		Execute ExecForASPProp("Response.ExpiresAbsolute")
		Execute ExecForASPProp("Response.IsClientConnected")
		Execute ExecForASPProp("Response.Status")
		Response.Write("</PRE>")
		Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
		Response.Write("<BIG>Request Properties</BIG>" & vbCrLf)
		Execute ExecForASPProp("Request.TotalBytes")
		Response.Write("</PRE>")
	End Sub

	Public Sub TestServerIO()
		'test FSO I/O on server
		Const TestFile1 = "___debugger_tester1.txt"
		Const TestFile2 = "___debugger_tester2.txt"
		Const TestFile3 = "___debugger_tester3.txt"
		Dim oFSO, oFile, sTmp, dblSize

		On Error Resume Next

		Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
		Response.Write("<BIG>I/O Test Results</BIG>" & vbCrLf)

		Set oFSO = CreateObject("Scripting.FileSystemObject")

		'remove any old test files
		oFSO.DeleteFile Server.Mappath(TestFile1), True
		oFSO.DeleteFile Server.Mappath(TestFile2), True
		oFSO.DeleteFile Server.Mappath(TestFile3), True
		err.clear

		'test 1.) attempt to create a test file to work with
		Set oFile = oFSO.CreateTextFile(Server.MapPath(TestFile1), true)
		If Err Then
			HandlePrintIOErr oFile, oFSO, "Couldn't Create File"
			Err.Clear
			Exit Sub
		End If

		'test 2.) attempt to write to file
		oFile.WriteLine "test line 1 written"
		If Err Then
			HandlePrintIOErr oFile, oFSO, "Couldn't Write To File"
			Err.Clear
			Exit Sub
		End If
		oFile.Close
		Set oFile = Nothing

		'test 3.) attempt to read from file
		Set oFile = oFSO.OpenTextFile(Server.MapPath(TestFile1), 1, False)
		sTmp = oFile.ReadAll
		If Err Then
			HandlePrintIOErr oFile, oFSO, "Couldn't Read From File"
			Err.Clear
			Exit Sub
		End If
		oFile.Close
		Set oFile = Nothing

		'test 4.) copy file
		oFSO.CopyFile Server.Mappath(TestFile1), Server.Mappath(TestFile2), True
		If Err Then
			HandlePrintIOErr "", oFSO, "Couldn't Copy File"
			Err.Clear
			Exit Sub
		End If

		'test 5.) move file
		oFSO.MoveFile Server.Mappath(TestFile2), Server.Mappath(TestFile3)
		If Err Then
			HandlePrintIOErr "", oFSO, "Couldn't Move File"
			Err.Clear
			Exit Sub
		End If

		'test 6.) delete test files
		oFSO.DeleteFile Server.MapPath(TestFile1), True
		oFSO.DeleteFile Server.MapPath(TestFile3), True
		If Err Then
			HandlePrintIOErr "", oFSO, "Couldn't Delete File"
			Err.Clear
			Exit Sub
		End If

		Set oFSO = Nothing

		Response.Write("All I/O Tests Completed Successfully: " & _
			"delete, move, copy, read, write, create")
		Response.Write("</PRE>")
	End Sub

	Public Sub DumpScriptingEngines()
		'retrieve vbscript scripting engine and jscript engine data
		Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
		Response.Write("<BIG>Scripting Engine Information</BIG>" & vbCrLf)
		Response.Write( vbTab & "<B>" & ScriptEngine() & "</B>" & vbCrlf & vbTab & vbTab & _
			ScriptEngineMajorVersion() & "." & ScriptEngineMinorVersion() & _
			"." & ScriptEngineBuildVersion() & vbCrLf & vbCrLf)

		'call Jscript function to check jscript engine info
		Response.Write( JSScriptEngine() )
		Response.Write("</PRE>")
	End Sub

	Public Sub DumpApplication()
		'return application contents and staticobjects collections contents
		Execute ExecForAppSess("Application")
	End Sub

	Public Sub DumpSession()
		'return session contents and staticobjects collections contents
		Execute ExecForAppSess("Session")
	End Sub

	Public Sub DumpForm()
		'return form collection contents
		Execute ExecForRequest("Form")
	End Sub

	Public Sub DumpQueryStrings()
		'return querystrings collection contents
		Execute ExecForRequest("QueryString")
	End Sub

	Public Sub DumpClientCertificates()
		'return clientcertificates collection contents
		Execute ExecForRequest("ClientCertificate")
	End Sub

	Public Sub DumpCookies()
		'return cookies collection contents
		Execute ExecForRequest("Cookies")
	End Sub

	Public Sub DumpServerVariables()
		'return servervariables collection contents
		Execute ExecForRequest("ServerVariables")
	End Sub



	'####################################
	'#      Internal Class Routines     #
	'####################################

	Private Sub HandlePrintIOErr(ByRef oFile, ByRef oFSO, ByVal sMsg)
		'in the event of an I/O error, dead it with on error ...
		On Error Resume Next

		'print a message
		Response.Write("<B>I/O Error:</B> " & sMsg & vbCrLf)

		'free the file handle if opened before the error occurred
		If IsObject(oFile) Then
			oFile.Close

			'release handle
			Set oFile = Nothing
		End If

		'release object
		If IsObject(oFSO) Then Set oFSO = Nothing
		Response.Write("</PRE>")
	End Sub

	Private Function ExecForASPProp(byVal propName)

		'write out this as string so I can call execute on it
		'somewhere else and input a custom property without 
		'recoding these 2 lines over and over again for each
		'property: 
		'  response.write(vbTab & "<B>someproperty</B>" & vbcrlf) & vbcrlf
		'  response.write(vbTab & vbTab & someproperty & vbcrlf & vbcrlf) & vbcrlf

		Dim s
		s = ""
		s = s & "Response.Write(vbTab & ""<B>" & propName & "</B>"" & vbCrLf)" & vbCrLf
		s = s & "Response.Write(vbTab & vbTab & " & propName & " & vbCrLf & vbCrLf)" & vbCrLf
		ExecForASPProp = s
	End Function

	Private Function ExecForRequest(byval sTmpInput)

		'same deal as above. this time it's a nested looper to loop through
		'forms/querystrings/clientcertificates/cookies without re-writing
		'all this code 4 times. Welcome to the world of 0 code duplication...
		Dim s

		s = "Dim Item, SubItem" & vbCrLf
		s = s & "Response.Write(""<PRE STYLE=""""background-color" & _
			":#FFFFEE;font-size:11pt;"""">"")" & vbCrLf
		s = s & "Response.Write(""<BIG>Available " & sTmpInput & _
			" Inputs:</BIG>"" & vbCrLf)" & vbCrLf
		s = s & "If Request." & sTmpInput & ".Count > 0 Then" & vbCrLf
		s = s & "For Each Item In Request." & sTmpInput & vbCrLf
		s = s & "If Request." & sTmpInput & "(Item).Count > 1 Then" & vbCrLf
		s = s & "Response.Write(vbTab & ""Request." & sTmpInput & _
			"(""""<B>"" & Item & ""</B>"""")"" & vbCrLf)" & vbCrLf
		s = s & "For Each SubItem In Request." & sTmpInput & "(Item)" & vbCrLf
		s = s & "Response.Write(vbTab & vbTab & SubItem & vbCrLf)" & vbCrLf
		s = s & "Next" & vbCrLf
		s = s & "Response.Write(vbCrLf)" & vbCrLf
		s = s & "Else" & vbCrLf
		s = s & "Response.Write(vbTab & ""Request." & sTmpInput & _
			"(""""<B>"" & Item & ""</B>"""")"" & vbCrLf)" & vbCrLf
		s = s & "Response.Write(vbTab & vbTab & Request." & sTmpInput & _
			"(Item) & vbCrLf)" & vbCrLf
		s = s & "End If" & vbCrLf
		s = s & "Next" & vbCrLf
		s = s & "Response.Write(vbCrLf)" & vbCrLf
		s = s & "Else" & vbCrLf
		s = s & "Response.Write(vbTab & ""No passed " & sTmpInput & _
			" inputs"" & vbCrLf & vbCrLf)" & vbCrLf
		s = s & "End If" & vbCrLf
		s = s & "Response.Write(""</PRE>"")" & vbCrLf
		ExecForRequest = s
	End Function

	Private Function ExecForAppSess(ByVal aspobj)
		'once again same thing for session and application
		' print application/session.contents and application/session.staticobjects
		'without duplicating code.
		Dim s
		s = ""		
		s = s & "Dim Key" & vbCrLf
		s = s & "Response.Write(""<PRE STYLE=""""background-color:" & _
			"#FFFFEE;font-size:11pt;"""">"")" & vbCrLf
		s = s & "Response.Write(""<BIG>" & aspobj & " Contents</BIG>"" & vbCrLf)" & vbCrLf
		s = s & "If " & aspobj & ".Contents.Count > 0 Then" & vbCrLf
		s = s & "For Each Key in " & aspobj & ".Contents" & vbCrLf
		s = s & "Response.Write( vbTab & """ & aspobj & _
			"(<B>"""""" & Key & """"""</B>)"" & vbCrLf & " & _
			"vbTab & vbTab & server.htmlencode(" & _
			aspobj & "(Key)) & vbCrLf )" & vbCrLf
		s = s & "Next" & vbCrLf
		s = s & "Response.Write(vbCrLf)" & vbCrLf
		s = s & "Else" & vbCrLf
		s = s & "Response.Write(vbTab & ""No " & aspobj & _
			" variables initialized "" & vbCrLf & vbCrLf)" & vbCrLf
		s = s & "End If" & vbCrLf
		s = s & "Response.Write(""</PRE>"")" & vbCrLf
		s = s & "Response.Write(""<PRE STYLE=""""background-color:" & _
			"#FFFFEE;font-size:11pt;"""">"")" & vbCrLf
		s = s & "Response.Write(""<BIG>" & aspobj & _
			" Static Object Contents</BIG>"" & vbCrLf)" & vbCrLf
		s = s & "If " & aspobj & ".StaticObjects.Count > 0 Then" & vbCrLf
		s = s & "For Each Key in " & aspobj & ".StaticObjects" & vbCrLf
		s = s & "Response.Write( vbTab & ""<B>"" & Key & ""</B>"" & vbCrLf & " & _
			"vbTab & vbTab & ""<i>(object)</i>"" & vbCrLf )" & vbCrLf
		s = s & "Next" & vbCrLf
		s = s & "Response.Write(vbCrLf)" & vbCrLf
		s = s & "Else" & vbCrLf
		s = s & "Response.Write(vbTab & ""No " & aspobj & _
			" static objects initialized "" & vbCrLf & vbCrLf)" & vbCrLf
		s = s & "End If" & vbCrLf
		s = s & "Response.Write(""</PRE>"")" & vbCrLf
		ExecForAppSess = s
	End Function

	Private Sub Class_Initialize()
		EndAfterDebug = False
	End Sub

	Private Sub Class_Terminate()
		'if user has chosen the endafterdebug option, print a message and stop
		'executing asp code.
		If EndAfterDebug Then 
			Response.Write("<PRE STYLE=""background-color:#FFFFEE;font-size:11pt;"">")
			Response.Write("################ ")
			Response.Write("Application Terminated After Debug")
			Response.Write("################ ")
			Response.Write("</PRE>")

			'stop executing asp code
			Response.End
		End If
	End Sub
End Class
'==========  END CLASS DEFINITION  ==================
applicable help files:

Microsoft VBScript runtime (0x800A0046)
Permission denied

Server object error 'ASP 0177 : 800401f3'
Server.CreateObject Failed

Microsoft VBScript runtime error
Invalid Class String

Microsoft VBScript runtime error '800a01ad'
ActiveX component can't create object: 'class string'

< prev proc
URLDecode Function
next proc >
GetHeaders Function