Class HttpRequest '############################################################## '## ## '## A class to simplify fetching parameters from Post/Get-requests ## '## The class also supports fetching cookies from a request ## '## ## '## Example of use: ## '## Dim request As New HttpRequest ## '## ## '## Dim action As String ## '## action = request.parameter( "action" ) ## '## 'e.g. ?OpenAgent&action=hello&debug ## '## If request.hasParameter( "debug" ) Then ## '## Print "Action: " + action ## '## End If ## '## ## '## 'To get a cookie: cookieValue = request.cookie( "cookieName" ) ## '## ## '## 'To get a report of all CGI-variables/request-content ## '## Print request.htmlReport (64k limited) ## '## 'To print a non-64k limited report ## '## Call request.printHtmlReport ## '## ## '## Made by Tommy Valand/DontPanic, http://dontpanic82.blogspot.com ## '## ## '############################################################## Private session As NotesSession Private context As NotesDocument Private request As String Private request_method As String Private requestParameterList List As Variant Private cookieList List As String Sub New On Error Goto bubbleError Set session = New NotesSession Set context = session.DocumentContext '// Check that session has DocumentContext If context Is Nothing Then Error 1001, "The session doesn't have DocumentContext (agent not run on the web?)" Me.request_method = context.GetItemValue( "request_method" )(0) '// Check that context has request-method (agent run by URL) If context.HasItem( "request_method" ) Then '// Get request-string If method = "GET" Then Me.request = context.GetItemValue( "query_string_decoded" )(0) Else Me.request = Me.requestContent() End If Else Error 1001, "Couldn't get request method (agent not run on the web?)" End If '// Extract all parameters into a String List Dim requestArr As Variant, parameterName As String, parameterValue As String requestArr = Split( Me.request, "&" ) Forall parameter In requestArr parameterName = Strtoken( parameter, "=", 1 ) parameterValue = Replace( parameter, parameterName + "=", "" ) If Iselement( requestParameterList( parameterName ) ) Then '// Parameter passed several times - multivalue Dim currentValue As Variant currentValue = Me.requestParameterList( parameterName ) '// A String - convert to a dynamic array If Datatype( currentValue ) = 8 Then currentValue = Split( currentValue, "¤%#¤%" ) '// Append value to array Me.requestParameterList( parameterName ) = Arrayappend( currentValue, parameterValue ) Else '// First occurence of parameter - add to list as string Me.requestParameterList( parameterName ) = parameterValue End If End Forall '// Extract all cookies into a String List Dim cookies As String, cookiesArr As Variant cookies = context.GetItemValue( "HTTP_COOKIE" )(0) cookiesArr = Split( cookies, "; " ) Forall cookie In cookiesArr Dim cookieName As String cookieName = Strtoken( cookie, "=", 1 ) cookieList( cookieName ) = Replace( cookie, cookieName + "=", "" ) End Forall Exit Sub bubbleError: Error Err, Me.errorMessage() End Sub '// Check to see that a parameter exists. More or less equal to isset in PHP Property Get hasParameter( Byval parameterName As String ) As Boolean hasParameter = ( Iselement( Me.requestParameterList( parameterName ) ) ) End Property '// Returns the value of the parameter specified by name. If not found it returns an empty string '// If the parameter is a result of a multi-select item, the property is a dynamic array, else a String Property Get parameter( Byval parameterName As String ) As Variant On Error Goto bubbleError If Me.hasParameter( parameterName ) Then parameter = Me.requestParameterList( parameterName ) Else parameter = "" End If Exit Property bubbleError: Error Err, Me.errorMessage() End Property '// Check to see if a cookie is set Property Get hasCookie( Byval cookieName As String ) As Boolean hasCookie = ( Iselement( Me.cookieList( cookieName ) ) ) End Property '// Returns the value of the cookie specified by name, or an empty string Property Get cookie( Byval cookieName As String ) If Me.hasCookie( cookieName ) Then cookie = Me.cookieList( cookieName ) End Property '// Returns the request-string Property Get content As String content = Me.request End Property '// Returns the request-method Property Get method As String method = Me.request_method End Property '// Function to determine if requestContent is longer than 64k/combine the fields Private Function requestContent() As String On Error Goto bubbleError If Me.context.hasItem( "request_content" ) Then requestContent = Me.context.getItemValue( "request_content" )(0) Exit Function End If Dim requestContentFieldNames As Variant requestContentFieldNames = Evaluate( |@Trim( @Word( @DocFields ; "REQUEST_CONTENT_" ; 2 ) )|, Me.context ) Dim stringBuffer As NotesStream Set stringBuffer = Me.session.CreateStream Forall requestContentFieldName In requestContentFieldNames Call stringBuffer.WriteText( context.GetItemValue( "REQUEST_CONTENT_" + requestContentFieldName )(0) ) End Forall stringBuffer.Position = 0 requestContent = stringBuffer.ReadText Exit Function bubbleError: Error Err, Me.errorMessage() End Function '// Prints full report - No 64k limit Function printHtmlReport On Error Goto bubbleError Dim report As String report = Me.htmlReport() Dim position As Long, increment As Long position = 1 increment = 60000 Print |

Content length: | & Format( Len( report ), "Standard" ) & | characters

| If Len( report ) < increment Then Print report Else Print |Number of request_content fields: | & Join( Evaluate( |@Elements( @Trim( @Word( @DocFields ; "REQUEST_CONTENT_" ; 2 ) ) )|, Me.context ) ) & || While position < Len( report ) Print Mid( report, position, increment ) position = position + increment Wend Print Mid( report, position ) End If Exit Function bubbleError: Error Err, Me.errorMessage() End Function Function htmlReport As String On Error Goto handleErr Dim stringbuffer As NotesStream Set stringbuffer = Me.session.CreateStream Call stringbuffer.WriteText( | | ) Call stringbuffer.WriteText( | | ) Call stringbuffer.WriteText( || ) Forall item In context.Items Call stringbuffer.WriteText( || ) End Forall Call stringbuffer.WriteText( |
Request content
| + item.name + || ) Forall itemval In item.Values Call stringbuffer.WriteText( itemval ) End Forall Call stringbuffer.WriteText( |
Routine created by Tommy Valand/DontPanic
| ) 'Extract stream content to string stringbuffer.Position = 0 htmlReport = stringbuffer.ReadText() Exit Function handleErr: Print Me.errorMessage() Resume Next End Function Private Function errorMessage As String '// Simple function to generate more readable errors when dealing with error-bubbling Dim message As String message = Error If Cstr( Getthreadinfo(10) ) = "INITIALIZE" Then errorMessage = "Error " & Err & " on line " & Erl & " in function " & Getthreadinfo( 10 ) & ": " + Error Else errorMessage = Chr(13) + Chr(9) + "Error " & Err & " on line " & Erl & " in function " & Getthreadinfo( 10 ) & ": " + Error$ End If End Function End Class