TCGI

BlitzMax Forums/BlitzMax Programming/TCGI

The following is the beginning of my meager attempts at getting a BMax CGI app working.
It is however not functioning correctly.

If I run it locally, it works (although there is no QUERY_STRING environment variable when run locally).

If I pass the app a URL encoded string ( cgi-bin/pertCGI.exe?test=1234 ) it times out.

Strict
Framework BRL.Basic
Import BRL.Retro

Function HexToInt( HexStr:String )
	If HexStr.Find("$") <> 0 Then HexStr = "$" + HexStr$
	Return Int(HexStr)
End Function


Type THashEntry
	Field Key : String
	Field Value : String
End Type


Type THash Extends TList

	Method Add:TLink(Key:String, Value:String, AutoReplace=False)
		'need to add check to see if key already exists, 
		'if it exists and AutoReplace=True
		'then replace it, else exit the method 
		'returning the existing link
		Local tempEntry:THashEntry = New THashEntry
			tempEntry.Key = Key
			tempEntry.Value = Value
		
		Return InsertBeforeLink(tempEntry,_head )
	End Method


	Method Get:String(Key : String)
		Local result : String = ""
		Local he : THashEntry
			For he = EachIn Self
				If Upper(he.Key) = Upper(Key) Then
					result = he.Value
					Exit
				EndIf
			Next
		Return result
	End Method
	
End Type



Type TCGI
	'Environment Variables
	Field AUTH_TYPE : String
	Field CONTENT_TYPE : String
	Field CONTENT_LENGTH : String
	Field GATEWAY_INTERFACE : String
	Field HTTP_ACCEPT : String
	Field HTTP_ACCEPT_CHARSET : String
	Field HTTP_ACCEPT_LANGUAGE : String
	Field HTTP_COOKIE : String
	Field HTTP_FROM : String
	Field HTTP_HOST : String
	Field HTTP_RANGE : String
	Field HTTP_REFERER : String
	Field HTTP_USER_AGENT : String
	Field PATH_INFO : String
	Field PATH_TRANSLATED : String
	Field QUERY_STRING : String
	Field REMOTE_ADDR : String
	Field REMOTE_HOST : String
	Field REMOTE_IDENT : String
	Field REMOTE_USER : String
	Field REQUEST_METHOD : String
	Field SCRIPT_NAME : String
	Field SCRIPT_FILENAME : String
	Field SERVER_NAME : String
	Field SERVER_PORT : String
	Field SERVER_PROTOCOL : String
	Field SERVER_SOFTWARE : String
	Field QueryHash:THash
	
	
	Method FillHash()
		Local s : String
		Local pos : Int
		Local tempEntry : THashEntry
		
		s = getenv_("QUERY_STRING")'QUERY_STRING
		
		While (Len(s)>0)
			pos = s.Find("&")'find the first occurance of the delimiter
			If Pos > -1 Then
				tempEntry:THashEntry = SplitQuery(s[0..Pos-1])
				QueryHash.Add(tempEntry.Key, tempEntry.Value)'add the first variable, excluding the delimiter
				s = s[Pos+1..]'remove the first variable and the & delimiter
				Print s
			Else
				tempEntry:THashEntry = SplitQuery(s)
				QueryHash.Add(tempEntry.Key, tempEntry.Value)
				s = ""
			EndIf	
	
		Wend
   
	End Method
		
	Method SplitQuery:THashEntry(query:String)
		Local tempEntry:THashEntry = New THashEntry
		Local pos : Int
		
		While (Len(query)>0)
			pos = query.Find("=")'find the first occurance of the delimiter
			If Pos > -1 Then
				tempEntry.Key = DecodeString(query[0..Pos-1])
				tempEntry.Value = DecodeString(query[Pos+1..])
			EndIf			
		Wend	
		
		Return tempEntry
	End Method
	
	
	Method PrintHTMLHeader()
		Print "Content-type:  text/html~n~n"
	End Method
	
	
	Method DecodeString:String(EncStr:String)
		Local Pos : Int
		Local HexVal : String
		Local Result : String
		
		While Pos<Len(EncStr)
			If EncStr[Pos] = "%" Then
				HexVal = EncStr[Pos+2..Pos+3]
				Result :+ String(HexToInt(HexVal))
				Pos:+3
			Else
				Result :+ EncStr[Pos]
				Pos:+1
			EndIf
		Wend
		
		Return Result
	End Method
	
		
	Function Create:TCGI()
		Local tempCGI:TCGI = New TCGI
		'	tempCGI.AUTH_TYPE = getenv_("AUTH_TYPE")
		'	tempCGI.CONTENT_TYPE = getenv_("CONTENT_TYPE")
		'	tempCGI.CONTENT_LENGTH = getenv_("CONTENT_LENGTH")
		'	tempCGI.GATEWAY_INTERFACE = getenv_("GATEWAY_INTERFACE")
		'	tempCGI.HTTP_ACCEPT = getenv_("HTTP_ACCEPT")
		'	tempCGI.HTTP_ACCEPT_CHARSET = getenv_("HTTP_ACCEPT_CHARSET")
		'	tempCGI.HTTP_ACCEPT_LANGUAGE = getenv_("HTTP_ACCEPT_LANGUAGE")
		'	tempCGI.HTTP_COOKIE = getenv_("HTTP_COOKIE")
		'	tempCGI.HTTP_FROM = getenv_("HTTP_FROM")
		'	tempCGI.HTTP_HOST = getenv_("HTTP_HOST")
		'	tempCGI.HTTP_RANGE = getenv_("HTTP_RANGE")
		'	tempCGI.HTTP_REFERER = getenv_(".HTTP_REFERER")
		'	tempCGI.HTTP_USER_AGENT = getenv_("HTTP_USER_AGENT")
		'	tempCGI.PATH_INFO = getenv_("PATH_INFO")
		'	tempCGI.PATH_TRANSLATED = getenv_("PATH_TRANSLATED")

			tempCGI.QUERY_STRING = getenv_("QUERY_STRING")

		'	tempCGI.REMOTE_ADDR = getenv_("REMOTE_ADDR")
		'	tempCGI.REMOTE_HOST = getenv_("REMOTE_HOST")
		'	tempCGI.REMOTE_IDENT = getenv_("REMOTE_IDENT")
		'	tempCGI.REMOTE_USER = getenv_("REMOTE_USER")
		'	tempCGI.REQUEST_METHOD = getenv_("REQUEST_METHOD")
		'	tempCGI.SCRIPT_NAME = getenv_("SCRIPT_NAME")
		'	tempCGI.SCRIPT_FILENAME = getenv_("SCRIPT_FILENAME")
		'	tempCGI.SERVER_NAME = getenv_("SERVER_NAME")
		'	tempCGI.SERVER_PORT = getenv_("SERVER_PORT")
		'	tempCGI.SERVER_PROTOCOL = getenv_("SERVER_PROTOCOL")
		'	tempCGI.SERVER_SOFTWARE = getenv_("SERVER_SOFTWARE")
			
			tempCGI.QueryHash:THash = New THash
			tempCGI.FillHash()
			Return tempCGI
	End Function
End Type


Rem
Test it
EndRem

Local myCGI:TCGI = TCGI.Create()
	myCGI.PrintHTMLHeader()
	Print getenv_("QUERY_STRING")
	myCGI.QueryHash.Add("test","432~n<br>")
Print myCGI.QueryHash.Get("test")

Release myCGI