How to use CGI / FastCGI

Just starting out? Need help? Post your questions and find answers here.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

How to use CGI / FastCGI

Post by Ajm »

Hi,

I am using the Abyss web server by Aprellium and would like to know if anybody has managed to get purebasic CGI / FASTCGI to work with it?

I would like to write an application that uses Spiderbasic as the front end and a purebasic cgi app as the backend, is this possible?

I have tried without any success with the examples in the CGI pages of the help file. I constantly get the error broken pipe in my cgi logfile.

Does anyone have any small examples or can maybe point me in the right direction?
Regards

Andy

Image
Registered PB & PureVision User
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: How to use CGI / FastCGI

Post by Paul »

Here is a short example:
Abyss is installed on local computer
D:\Abyss Web Server

I created a folder for the cgi apps
D:\Abyss Web Server\cgi-bin

In Abyss console I selected "Configure" button for default host on port 80
Under "Alias" I added virtual path
/cgi-bin
and real path
D:\Abyss Web Server\cgi-bin

Under "Scripting Parameters" I added a virtual path under Script Path
/cgi-bin

Now I compiled this PB code as "cgitest.exe" with executable format as Console (under compiler options)
and placed it in D:\Abyss Web Server\cgi-bin

Code: Select all

If Not InitCGI() Or Not ReadCGI()
  End
EndIf

name$=CGIParameterValue("name")

WriteCGIHeader(#PB_CGI_HeaderContentType,"text/html",#PB_CGI_LastHeader)
WriteCGIString("Hello "+name$)
Open a web browser and type this link
http://localhost/cgi-bin/cgitest.exe?name=Paul

If all is set up correctly your webpage should display
Hello Paul

Now to test with SpiderBasic app, compile this code with SpiderBasic

Code: Select all

;/ Created with PureVision v5.40
;/ Mon, 09 Nov 2015 11:45:45
;/ by Paul Leischow


Define EventID,MenuID,GadgetID,WindowID

;- Window Constants
Enumeration 1
  #Window_Main
EndEnumeration

;- Gadget Constants
Enumeration 1
  ;Window_Main
  #Gadget_Main_Text2
  #Gadget_Main_Name
  #Gadget_Main_OK
EndEnumeration

Procedure.i Window_Main()
  If OpenWindow(#Window_Main,158,117,335,84,"CGI Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      TextGadget(#Gadget_Main_Text2,15,15,60,15,"Name:")
      StringGadget(#Gadget_Main_Name,15,30,230,20,"")
      ButtonGadget(#Gadget_Main_OK,255,30,60,20,"OK")
      HideWindow(#Window_Main,0)
    ProcedureReturn WindowID(#Window_Main)
  EndIf
EndProcedure

Procedure HttpGetEvent(Success, Result$, UserData)
  If Success
    Debug Result$
    Else
    Debug "HTTPRequest(): Error"
  EndIf
 EndProcedure
 
 

Procedure CloseWindowEvent()
  WindowID=EventWindow()
  If WindowID=#Window_Main
    CloseWindow(#Window_Main)
  EndIf
EndProcedure


;- GadgetEvent Loop
Procedure GadgetEvent()
  GadgetID=EventGadget()

  Select GadgetID
    Case #Gadget_Main_OK
      HTTPRequest(#PB_HTTP_Get, "/cgi-bin/cgitest.exe?name="+GetGadgetText(#Gadget_Main_Name), "", @HttpGetEvent())
  EndSelect
EndProcedure




;- Main Loop
If Window_Main()
  BindEvent(#PB_Event_Gadget,@GadgetEvent())
EndIf
I renamed the compiled file "SpiderBasic_Compilation0.html" to "cgitest.html" and placed this file in
D:\Abyss Web Server\htdocs

I also created the folder
D:\Abyss Web Server\htdocs\spiderbasic
D:\Abyss Web Server\htdocs\spiderbasic\libraries
and then copied the JavaScript folder from my C:\SpiderBasic\Libraries\JavaScript
and placed it in D:\Abyss Web Server\htdocs\spiderbasic\libraries\javascript

Now test by opening web browser and typing
http://localhost/cgitest.html

Enter your name and press OK, debug window should display
Hello your name
Image Image
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Re: How to use CGI / FastCGI

Post by Ajm »

Thanks for the detailed response Paul.

I think I've got it set up as you but I keep getting the following error in my CGI error log.

[09/Nov/2015:19:13:06 +0000] CGI: [D:/Cgi/cgitest.exe ] URI: /cgi-bin/cgitest.exe?name=Paul Broken pipe
[09/Nov/2015:19:13:09 +0000] CGI: [D:/Cgi/cgitest.exe ] URI: /cgi-bin/cgitest.exe?name=Paul Broken pipe


I will keep trying over the next couple of days and let you know how I get on.
Regards

Andy

Image
Registered PB & PureVision User
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: How to use CGI / FastCGI

Post by Paul »

When setting up your Scripting Parameters, did you stick with all the defaults and only add a Virtual Path?
The above example uses standard CGI

Image

Image

Image
Image Image
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Re: How to use CGI / FastCGI

Post by Ajm »

Hi Paul,

I'm still struggling with this one.
I've created a brand new vanilla site in Abyss and added nothing but the script parameter/script path and the alias but still get the same Broken pipe error.

I'm running Abyss on Windows 10 and I've compiled the test cgi app on a windows 10 VM using 5.40 LTS (x86).

Code: Select all

If Not InitCGI() Or Not ReadCGI()
  End
EndIf

name$=CGIParameterValue("name")

WriteCGIHeader(#PB_CGI_HeaderContentType,"text/html",#PB_CGI_LastHeader)
WriteCGIString("Hello "+name$)

I'm at a loss as to why its not working.

Could you possibly send me a link to download the test app you've used to rule out something I'm doing wrong.
Regards

Andy

Image
Registered PB & PureVision User
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: How to use CGI / FastCGI

Post by the.weavster »

I'm not in a position to test this at the moment but my guess is ReadCGI() is for reading the body of a POST request but you're sending a GET request therefore ReadCGI() is returning #False and your program is ending right at the start.

Broken pipe certainly means the process ended without writing anything back to the server.
User avatar
Ajm
Enthusiast
Enthusiast
Posts: 242
Joined: Fri Apr 25, 2003 9:27 pm
Location: Kent, UK

Re: How to use CGI / FastCGI

Post by Ajm »

Finally I've found the issue. By default I have my PB set to compile thread safe executables. Now I've un ticked this it's working fine.

I would have thought you should be able to use a thread safe exe as a CGI or FastCGI app. Should I post this as a bug?

Thanks Paul for your help.

the.weavster I think the ReadCGI() must be for both GET and POST.
Regards

Andy

Image
Registered PB & PureVision User
User avatar
the.weavster
Addict
Addict
Posts: 1576
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: How to use CGI / FastCGI

Post by the.weavster »

Ajm wrote:the.weavster I think the ReadCGI() must be for both GET and POST.
The laptop I've got with me doesn't have PB on so it was a guess, I was trying to think what it could be making your exe halt before the write commands got called.
Ajm wrote:I would have thought you should be able to use a thread safe exe as a CGI or FastCGI app. Should I post this as a bug?
I wouldn't have thought you'd use threads in a CGI application because it's launched as a new process that just handles one client request then ends but a FastCGI app could possibly use threads.
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: How to use CGI / FastCGI

Post by Rinzwind »

Is FastCGI supported in Abyss? Cannot find out how to set the required port...
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: How to use CGI / FastCGI

Post by kpeters58 »

Paul wrote: Now to test with SpiderBasic app, compile this code with SpiderBasic

Code: Select all

;/ Created with PureVision v5.40
;/ Mon, 09 Nov 2015 11:45:45
;/ by Paul Leischow


Define EventID,MenuID,GadgetID,WindowID

;- Window Constants
Enumeration 1
  #Window_Main
EndEnumeration

;- Gadget Constants
Enumeration 1
  ;Window_Main
  #Gadget_Main_Text2
  #Gadget_Main_Name
  #Gadget_Main_OK
EndEnumeration

Procedure.i Window_Main()
  If OpenWindow(#Window_Main,158,117,335,84,"CGI Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      TextGadget(#Gadget_Main_Text2,15,15,60,15,"Name:")
      StringGadget(#Gadget_Main_Name,15,30,230,20,"")
      ButtonGadget(#Gadget_Main_OK,255,30,60,20,"OK")
      HideWindow(#Window_Main,0)
    ProcedureReturn WindowID(#Window_Main)
  EndIf
EndProcedure

Procedure HttpGetEvent(Success, Result$, UserData)
  If Success
    Debug Result$
    Else
    Debug "HTTPRequest(): Error"
  EndIf
 EndProcedure
 
Procedure CloseWindowEvent()
  WindowID=EventWindow()
  If WindowID=#Window_Main
    CloseWindow(#Window_Main)
  EndIf
EndProcedure

;- GadgetEvent Loop
Procedure GadgetEvent()
  GadgetID=EventGadget()

  Select GadgetID
    Case #Gadget_Main_OK
      HTTPRequest(#PB_HTTP_Get, "/cgi-bin/cgitest.exe?name="+GetGadgetText(#Gadget_Main_Name), "", @HttpGetEvent())
  EndSelect
EndProcedure

;- Main Loop
If Window_Main()
  BindEvent(#PB_Event_Gadget,@GadgetEvent())
EndIf
I renamed the compiled file "SpiderBasic_Compilation0.html" to "cgitest.html" and placed this file in
D:\Abyss Web Server\htdocs

I also created the folder
D:\Abyss Web Server\htdocs\spiderbasic
D:\Abyss Web Server\htdocs\spiderbasic\libraries
and then copied the JavaScript folder from my C:\SpiderBasic\Libraries\JavaScript
and placed it in D:\Abyss Web Server\htdocs\spiderbasic\libraries\javascript

Now test by opening web browser and typing
http://localhost/cgitest.html

Enter your name and press OK, debug window should display
Hello your name
Paul, you forgot to mention that the corresponding spiderbasic.js file also needs to be copied to the htdocs folder.

Just tripped me up for a minute

Cheers,
Kai
PB 5.73 on Windows 10 & OS X High Sierra
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: How to use CGI / FastCGI

Post by karmacomposer »

I know this thread is very old, but I am needing to send emails from a Spiderbasic program (on a webpage) and so far, no luck.

I am using a standard apache linux shared server to put the web pages and cgi scripts on.

I was advised to create a simple cgi script in Purebasic to serve the emails. I am at a loss at how to do this with Purebasic on a linux server. I don't think a .exe file will run on a linux server and I do not have a Linux computer to run Purebasic Linux on.

How would I go about this? The main programming is in Spiderbasic and the only thing not yet working is emailing the form data.

Thank you for your help.

Mike
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to use CGI / FastCGI

Post by idle »

if you're on a shared server I don't think you can use CGI with your own exe. you'd have to use PHP

You can try atomic web server and run it from home or on a virtual server and then you don't need CGI at all as it's an application server. just forward port 80 and 443 to the box you run it on and your application is served.

https://www.purebasic.fr/english/viewtopic.php?t=83780

The test site
https://atomicwebserver.com

the code
https://github.com/idle-PB/atomic_webserver_3

I use a smtp2go.com to send emails

Code: Select all

Procedure SendEmail(mailto.s,subject.s,message.s)
  
  Protected mail
  Protected from.s ="mywebsite.com"
  Protected Pass.s = "xxxxxxxxxx"
  
  Protected result,progress  
  mail = CreateMail(#PB_Any, "admin@" + from, subject)
  If mail 
    SetMailBody(mail, message) 
    AddMailRecipient(mail, mailto, #PB_Mail_To)
    Result = SendMail(mail,"mail.smtp2go.com",2525,0,from,Pass)
    Repeat
      Progress = MailProgress(mail)
      Delay(30)
    Until Progress = #PB_Mail_Finished Or Progress = #PB_Mail_Error
    FreeMail(mail) 
    If Progress = #PB_Mail_Finished
      ProcedureReturn #True 
    EndIf   
  EndIf
  
EndProcedure

;...
server1 = Atomic_Server_Init(title,"./www/",#Server_IP,"atomicwebserver.com",443)  
Atomic_Server_Init_TLS(server1,"./certs/","atomicwebserver.com","certificate.crt","private.key","ca_bundle.crt")   
tid = CreateThread(@updateDNS(),600000)

OpenConsole()
Atomic_Server_start(server1,1,1) ;
Repeat 
Until Input() = "quit" 
gquit =1 
Atomic_Server_Exit(server1)
CloseConsole() 
 
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: How to use CGI / FastCGI

Post by karmacomposer »

Yes, I am on a shared server. I'll look into Atomic server but I'd rather do it with php then. Do you know of any Any code examples between PHP and spider basic?

Mike
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: How to use CGI / FastCGI

Post by karmacomposer »

Atomic Web Server is very interesting. You can serve your Purebasic and Spiderbasic Apps?

How does that work and how do I get it working on a virtual server and where do I even get a virtual server?

Mike
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to use CGI / FastCGI

Post by idle »

karmacomposer wrote: Wed Jul 17, 2024 8:07 pm Atomic Web Server is very interesting. You can serve your Purebasic and Spiderbasic Apps?

How does that work and how do I get it working on a virtual server and where do I even get a virtual server?

Mike

Yes you can serve PB and spider basic apps
no you don't want a virtual server but eventually you might want a cloud virtual machine.

long answer:
Yes that's what its designed to do, Atomic webserver is just an application server, you can use it like a regular web server serving files or embed the files with the packer add it to a data section and serve it all from memory or you can make a fully dynamic server where your endpoints URI's are just Function calls to generate the html, if a html file is saved as .htm or .pbh it will scan the file for runtime functions "<PB? Some function()/>" so you can insert dynamic content within a page, like fill out a table or generate images... which just replaces the need for server side scripting or cgi.
So yes your PB application is the server and you have all the power of PB at your command. The same can be achieved via CGI but then you'd be running on a server with a lamp stack, nginx or IIS and having to deal with their configurations.

And No you don't want a virtual server you want a cloud vm but do you need one? probably not immediately, you can easily host from the office or home while developing and deploying to a cloud vm is just a case of copying the folder and pointing your dns record to the cloud vm when you need to expand. There is nothing to configure as such just copy, run, done!

I guess the important thing to consider in this is who you use for your domain names and that you have easy access to add and edit DNS records with DDNS support so you can host from home or the office if your on a dynamic IP which is more likely than not. Then you just use a thread and update your IP address with a http Request to keep your dns up to date.

SSL certificate management is easy to achieve via services like zeroSSL but you have to manually renew certs every 3 months unless you pay for their services, then you can also use a http request to renew your cert automatically.

Also with the reverse proxy you can host multiple domains via the same machine running in their own process or proxy to another machine remotely. it's just a line of code Atomic_Server_Add_Proxy(server,"myotherdomain.com","127.0.0.1",8081)
Post Reply