FastGCI with PB

Share your advanced PureBasic knowledge/code with the community.
Motu
Enthusiast
Enthusiast
Posts: 160
Joined: Tue Oct 19, 2004 12:24 pm

FastGCI with PB

Post by Motu »

finaly it's working :-)

Here is my little program - it works well on windows server 2008 with iis7
You will need the libfcgi.dll to make it work.
Thanks to the other people posting on the board - it really helped alot :-)

Code: Select all

EnableExplicit

#FCGI_LoggingLevel = 0 ; Increate Value to decrease Loging, set to -1 to disable

Global *FCGI_Accept
Global *FCGI_Finish
Global *FCGI_StartFilterData
Global *FCGI_SetExitStatus
Global *FCGI_puts
Global *FCGI_gets

Global FCGI_Lib.i

Global LineBreak.s
Global LogFile.s
Global ErrFile.s

; Will handle logging
Procedure FCGI_Log(String.s,Level)
CompilerIf #FCGI_LoggingLevel > -1
 Define File.i
 
 If #FCGI_LoggingLevel <= Level
  File = OpenFile(#PB_Any,LogFile)
   FileSeek(File,FileSize(LogFile))
   WriteStringN(File,String)
  CloseFile(File)
 EndIf
CompilerEndIf
EndProcedure
; End FCGI_Log(String.s)

; Will display an error Message
Procedure FCGI_ErrMsg(String.s)
 Define File.i
 File = CreateFile(#PB_Any,ErrFile)
 If File <> 0
  WriteStringN(File,String)
  CloseFile(File)
 Else
  ; You'll never know what happend...
 EndIf
 End
EndProcedure
; End Err_Msg(String.s)

; Will make sure all function are avaible
Procedure FCGI_GetFunctionSave(Lib.i,Name.s)
 Define Function.i
 
 Function = GetFunction(Lib, Name)
 If Function = 0
  FCGI_ErrMsg("Cannot find pointer to function " + Name)
 Else
  FCGI_Log("Function " + Name + " found",10)
  ProcedureReturn Function
 EndIf
EndProcedure
; End FCGI_GetFunctionSave(Lib.i,Name.s)

; Will do some Init stuff
Procedure FCGI_Init()
 ; Init logging and error display
 LogFile = "log.txt"
 ErrFile = "err.txt"
 DeleteFile(LogFile)
 DeleteFile(ErrFile)
 FCGI_Log("Startup",10)
 
 LineBreak = Chr(13) + Chr(10)
 
 FCGI_Lib = OpenLibrary(#PB_Any, "libfcgi.dll")
 If FCGI_Lib = 0
  FCGI_ErrMsg("Cannot open libfcgi.dll")
 EndIf
 *FCGI_Accept          = FCGI_GetFunctionSave(FCGI_Lib, "FCGI_Accept")
 *FCGI_Finish          = FCGI_GetFunctionSave(FCGI_Lib, "FCGI_Finish")
 *FCGI_StartFilterData = FCGI_GetFunctionSave(FCGI_Lib, "FCGI_StartFilterData")
 *FCGI_SetExitStatus   = FCGI_GetFunctionSave(FCGI_Lib, "FCGI_SetExitStatus")
 *FCGI_puts            = FCGI_GetFunctionSave(FCGI_Lib, "FCGI_puts")
 *FCGI_gets            = FCGI_GetFunctionSave(FCGI_Lib, "FCGI_gets")

 FCGI_Log("Library load complete",10)
 
EndProcedure
; End FCGI_Init()

; Removes program from memory
Procedure FCGI_ReInit()
 CloseLibrary(0)
 End
EndProcedure
; End FCGI_ReInit()

; Print a String without Linebreak
Procedure FCGI_Print(String.s)
 CallCFunctionFast(*FCGI_puts, @String)
EndProcedure
; End FCGI_Print(String.s)

; Print a String with Line Break
Procedure FCGI_PrintN(String.s)
 If Len(String) > 0
  CallCFunctionFast(*FCGI_puts, @String)
 EndIf
 CallCFunctionFast(*FCGI_puts, @LineBreak)
EndProcedure
; End FCGI_PrintN(String.s)

; The Main Loop
Procedure FCGI_Main()
 Define Result.i
 
 Repeat
  Result = CallCFunctionFast(*FCGI_Accept)
  If Result => 0
   FCGI_Log("FCGI_Accept Return Value:" + Str(Result),1)
   
 ;  CallCFunctionFast(*FCGI_gets,@String) ; no use yet
   
;   FCGI_PrintN("HTTP/1.1 200 OK") ; On Windows Server 2008 with IIS7 this crashes your application...
   FCGI_PrintN("Content-type: text/html")
   FCGI_PrintN("")
   FCGI_PrintN("Hello World")
  EndIf
 Until Result < 0
 FCGI_Log("FCGI_Accept < 0: " + Str(Result),10)

 CallCFunctionFast(*FCGI_SetExitStatus, 0)
 
EndProcedure
; End Main()

FCGI_Init()
FCGI_Main()
FCGI_ReInit()
Amundo
Enthusiast
Enthusiast
Posts: 200
Joined: Thu Feb 16, 2006 1:41 am
Location: New Zealand

Re: FastGIC with PB

Post by Amundo »

Thanks for sharing, but for newbies like myself, I'm assuming this is something to do with CGI (or FastCGI)?

A little more description for people who have no idea what it's all about, would be helpful :-)

P.S. I assume the title should be "FastCGI with PB"?
Win10, PB6.x, okayish CPU, onboard video card, fuzzy monitor (or is that my eyesight?)
"When the facts change, I change my mind" - John Maynard Keynes
Post Reply