Thread Closed  Topic Closed
  PowerBASIC Forums
  Source Code
  CGI in PBDLL (Putting it all together)

Post New Topic  
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   CGI in PBDLL (Putting it all together)
Paul Dwyer
Member
posted October 03, 2002 11:21 PM     Click Here to See the Profile for Paul Dwyer     Edit/Delete Message   Reply w/Quote
Okay!

This is about 3/4 code from this forum and 1/4 mine but I wanted to know how to do CGI in PBDLL because I'm not a fan of ISAPI. The reason is that for testing you need to keep stopping your web server or if your app crashes or goes into an infinite loop you can't kill it and it trashed your server. The simple reason is that an ISAPI DLL runs in the memory space of the web server itself. Also, There are a lot of CGI bits and pieces on this forum but for those who have put something together an not really had a clue about STDIO (like me) it was very difficult

Thank you Jokob and Don for your STD I/O code which is the meat here and I have added a GetPostVal() function to massage the output of the env var.

Here is a full working piece with the calling HTML commented at the bottom.

Share and Enjoy


#Compile Exe "CGITest1.exe"
#Include "win32api.inc"

Declare Function GetStdHandle Lib "KERNEL32.DLL" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Declare Function webdataStdIn() As String
Declare Sub StdInit
Declare Sub STDOUT (Buffr$)
Declare Sub StdShutdown
Declare Function GetPostVal(ParamName As String, CGIPostText As String) As String

Global Cons&

Function PbMain

Dim CGIPostText As String
StdInit

CGIPostText = webdataStdIn()

StdOut "Content-type: text/html"
StdOut ""
StdOut "<html>"
StdOut "<head><title>PB/Win or PB/DLL CGI test</title></head>"
StdOut "<body>"
StdOut "<h3>PB/Win or PB/DLL CGI test</h3>"

' ENV ==========================================

StdOut "Environment variables:"
Stdout "<ul>"
x& = 0
Do
Incr x&
a$ = Environ$(x&)
If a$ <> "" Then StdOut "<li>"+a$
Loop While a$ <> ""
StdOut "</ul>

' Post VARS ==========================================

StdOut "T1= " & GetPostVal("T1", CGIPostText) & "<P>" & crlf$
StdOut "T2= " & GetPostVal("T2", CGIPostText) & "<P>" & crlf$
stdout CGIPostText & "<P>" & crlf$ 'Raw Input

StdOut "</body>"
StdOut "</html>"

StdShutdown
End Function


Sub StdInit
Dim STD_OUTPUT_HANDLE As Long
Dim STD_INPUT_HANDLE As Long
Dim hFile As Long
STD_OUTPUT_HANDLE = -11&
STD_INPUT_HANDLE = -10&
hFile = GetStdHandle(STD_OUTPUT_HANDLE)
Cons& = FreeFile
Open Handle hFile For Output As #Cons&
End Sub

Sub StdShutdown
Close #Cons&
End Sub

Sub STDOUT (Buffr$)
Print #Cons&, Buffr$

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' webdataStdIn
'
' Replacement for StdIn Line to read standard input.
' StdIn Line has a limit
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function webdataStdIn() As String

' Dim iFF as Long
Dim hInput As Long
Dim iToRead As Long
Dim iRead As Long
Dim iResult As Long
Dim sBuffer As String
Dim sOutBuffer As String

iToRead = Val(Environ$("CONTENT_LENGTH"))
hInput = GetStdHandle(%STD_INPUT_HANDLE)
If hInput Then
Do
If iToRead > 32000 Then
sBuffer = Space$(32000)
ElseIf iToRead = 0 Then
Exit Do
Else
sBuffer = Space$(iToRead)
End If

iResult = ReadFile(hInput, ByVal StrPtr(sBuffer), _
Len(sBuffer), iRead, ByVal %NULL)

'- If there was an error, return nothing
If iResult = 0 Then
Exit Do

'- We're done if iRead is 0
ElseIf iRead < 1 Then
Exit Do

'- Otherwise, accumulate the buffer
Else
sOutBuffer = sOutBuffer + Left$(sBuffer, iRead)
If Len(sOutBuffer) >= iToRead Then Exit Do

End If
Loop
End If

Function = sOutBuffer

End Function

Function GetPostVal(ParamName As String, CGIPostText As String) As String

Dim PreppedParam As String
Dim StartPos As Long
Dim endPos As Long
Dim ParamLen As Long
Dim ValueLen As Long

PreppedParam = ParamName & "="
ParamLen = Len(PreppedParam)

StartPos = InStr(CGIPostText,PreppedParam)
If StartPos < 1 Then 'not found
GetPostVal = ""
Exit Function
Else
endPos = InStr(StartPos + ParamLen, CGIPostText, "&")
If endpos < 1 Then 'last param
endpos = Len(CGIPostText)
End If 'else endpos is correct

StartPos = StartPos + ParamLen
ValueLen = endpos - StartPos

GetPostVal = Mid$(CGIPostText,StartPos, ValueLen )
End If


End Function

'CALLING HTML OR ASP PAGE! Assumes same DIR as exe so please change the path

'<html>
'<head>
'<title>CGI Test</title>
'</head>
'
'<body>
'
'<form method="POST" action="cgitest1.exe">
' <p><input type="text" name="T1" size="20"><input type="text" name="T2" size="20"><input
' type="submit" value="Test CGI" name="B1"></p>
'</form>
'</body>
'</html>


------------------
Paul Dwyer
Network Engineer
Aussie in Tokyo

[This message has been edited by Paul Dwyer (edited October 03, 2002).]

IP: Logged

Paul Dwyer
Member
posted October 03, 2002 11:27 PM     Click Here to See the Profile for Paul Dwyer     Edit/Delete Message   Reply w/Quote
Last things to do,

Change "+" for " " and I think there is a code snippet here on this forum for changing the %## codes but that's fairly easy too so if I get that done I'll put it all back here as PBCGI.inc

------------------
Paul Dwyer
Network Engineer
Aussie in Tokyo

IP: Logged

Tony A Kirk
Member
posted November 06, 2002 11:34 AM     Click Here to See the Profile for Tony A Kirk     Edit/Delete Message   Reply w/Quote
Now, is there a way to turn this into a DLL instead of an exe file? That's what I've been looking for and can't find anywhere. Using PB/Win7 of course.

-Tony

------------------
-Tony

IP: Logged

Don Dickinson
Member
posted November 06, 2002 05:13 PM     Click Here to See the Profile for Don Dickinson     Edit/Delete Message   Reply w/Quote
quote:

Now, is there a way to turn this into a DLL instead of an exe file? That's what I've been looking for and can't find anywhere. Using PB/Win7 of course.


Do you mean an isapi dll? if no, the short answer is no. You can do the equivalent thing with an isapi, but the code would not look like this.
I think Lothar posted an isapi wrapper/toolkit somewhere on this site.
Also, I have isapi headers and wrappers available from my site:
dickinson.basicguru.com

--Don

------------------
dickinson.basicguru.com

IP: Logged

Paul Dwyer
Member
posted November 06, 2002 06:38 PM     Click Here to See the Profile for Paul Dwyer     Edit/Delete Message   Reply w/Quote
Get in a quick post before Lance tells us to go to a different forum

Don, Not quite sure what you mean. I don't really like ISAPI much (loved your code though) because it's in process and no convenient for things that need to be updated regularly. But if you wanted to make a CGI.dll with finctions for getting env vars, and having the stdio output etc then just call it from your exe. There is not that much code in this though so I can't really see having a DLL being critical but you could do some customisations and use it for serval apps. Personall I think an INC finle would be better.

Or Tony, is this not what you mean? Are you refering to have a code reuse method for the functions or calling the dll directly?

------------------
Paul Dwyer
Network Engineer
Aussie in Tokyo

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Open Topic | Archive/Move | Delete Topic
Post New Topic  
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c