Page 1 of 1

Server side (xampp): how to run external CGI/ exe

Posted: Sat Jul 29, 2017 12:09 pm
by morosh
Hello:
I'm trying to run an external exe from browser server side, I found two snippets in another post (http://forums.spiderbasic.com/viewtopic.php?f=6&t=292), I tried them in xampp, it didn't works, it seems I'm missing something:
SpiderBasic file "Test.sb":

Code: Select all

Procedure UpperWeb(Success, Result$)
  If Success
    SetGadgetText(3,Result$)
  Else
    Debug "Failed"
  EndIf
EndProcedure


Procedure getbutton()
  temp$=GetGadgetText(3) 
  HTTPRequest(#PB_HTTP_Get, "http://127.0.0.1/cgi-bin/upper.pbe?string="+temp$, "", @UpperWeb())
 
EndProcedure

If OpenWindow(1, 100, 100, 250, 75, "Test", #PB_Window_TitleBar)
  ButtonGadget(2, 20, 30, 75, 30, "To Upper")
  StringGadget(3, 120, 30, 75, 30, "abc")
  BindGadgetEvent(2,@getbutton(), #PB_EventType_LeftClick )
EndIf
I made "Create App", this generates "test.html" and "test.js", I've copied this two files to c:\xampp\htdocs\test\, I've copied there the spiderbasic librairies as well.

then in PureBasic (v5.30) , I've created "upper.pb" (CGI libs already installed)

Code: Select all

CGI_In()
string$=CGI_Val("string")
string$=UCase(string$) 
CGI_Header()
CGI_Out(string$)
End
I create the executable "upper.exe" and put it in c:\xampp\cgi-bin

Going back to C;\xampp\htdocs\test\, clicking on test.html, then inside to "To Upper" button gives "Failed".

I didn't understand the line; HTTPRequest(#PB_HTTP_Get, "http://127.0.0.1/upper.pbe?string="+temp$, "", @UpperWeb())
what upper.pbe stands for?

any help is appreciated.

Re: Server side (xampp): how to run external CGI/ exe

Posted: Sat Jul 29, 2017 12:36 pm
by Peter
morosh wrote:what upper.pbe stands for?
this might be the name of the executable.

so you have to rename your "upper.exe" to "upper.pbe" or you have to change your HttpRequest from "...upper.pbe?string..." to "...upper.exe?string..."

Greetings ... Peter

Re: Server side (xampp): how to run external CGI/ exe

Posted: Sat Jul 29, 2017 2:04 pm
by morosh
Yes, that what I supposed
I already renamed upper.exe to upper.pbe, but it didn't works

Re: Server side (xampp): how to run external CGI/ exe

Posted: Sat Jul 29, 2017 9:55 pm
by Peter
this is a working example

PureBasic 5.60 (with native CGI support):

Code: Select all

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

Define myString.s = CGIParameterValue("string")

WriteCGIHeader("Access-Control-Allow-Origin", "*")

WriteCGIHeader(#PB_CGI_HeaderContentType, "text/plain", #PB_CGI_LastHeader)

WriteCGIString(UCase(myString))
SpiderBasic:

Code: Select all

Procedure HTTPRequestCallback(Success, Result.s)
  Debug Success
  Debug Result
EndProcedure

Define myString.s = "uppercase"

HTTPRequest(#PB_HTTP_Get, "http://localhost/cgi-bin/upper.exe?string=" + myString, "", @HTTPRequestCallback())
Greetings ... Peter

Re: Server side (xampp): how to run external CGI/ exe

Posted: Sun Jul 30, 2017 8:43 am
by morosh
Works great!!!
Thank you very much Peter!!

Re: Server side (xampp): how to run external CGI/ exe

Posted: Sun Jul 30, 2017 9:56 am
by Peter
morosh wrote:Works great!!!
Fine! :)

If you want, you can try SpiderBite (sorry, the readme is in german, but you can use google translate)

With SpiderBite you are able to write something like this:

Code: Select all

XIncludeFile "[YourPathTo]/SpiderBite.sbi"

#SpiderBite_Profile = "default"

EnablePbCgi
  
  Procedure.s Upper(myString.s)
    ProcedureReturn UCase(myString)
  EndProcedure
  
DisablePbCgi

Define myString.s
myString = "uppercase"
Debug Upper(myString)
Greetings ... Peter

Re: Server side (xampp): how to run external CGI/ exe

Posted: Sun Jul 30, 2017 11:50 am
by morosh
Wonderful, very interesting
Go on Peter!! :D