Page 1 of 1

Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 3:48 am
by kpeters58
I point this request @ a cgi program created with PB (all of this under Windows 10, running Abyss as Web Server)

HTTPRequest(#PB_HTTP_Get, "http://127.0.0.1:8080/cgi-bin/cgiprinter", "", @HttpGetEvent())

and it fails with HTTP Error - the log entry for this attempt:

127.0.0.1 - - [25/May/2021:20:32:43 -0700] "GET /cgi-bin/cgiprinter?%20&_=1621999960984 HTTP/1.1" 200 645 "http://127.0.0.1:9080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"

When I paste the URL from the request into any browser it works and the access log shows:
127.0.0.1 - - [25/May/2021:20:44:51 -0700] "GET /cgi-bin/cgiprinter HTTP/1.1" 200 597 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"

I am awfully tempted to blame this on the query string that Spiderbasic seems to insert out of the blue...

What is going on here? Thanks for any help

Re: Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 6:27 am
by Peter
I don't see any error message in your post, but a look into my crystal ball shows me that you might get a CORS error message?

In this case it would help if you add the following line to your CGI:

Code: Select all

WriteCGIHeader("Access-Control-Allow-Origin:", "*", #PB_CGI_LastHeader)
Greetings ... Peter

Re: Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 6:55 am
by the.weavster
I doubt it's the query string, I think that just gets assigned to an environment variable that your CGI can access if required.

What do you get from:

Code: Select all

Debug HTTPInfo(HTTPRequest, #PB_Http_ErrorMessage)

Re: Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 7:45 pm
by kpeters58
Thanks for your suggestions, but I am still stuck - guess I'll have to post the full details:

Everything below running on the same Windows 10 PC, Webserver is Abyss running on 8080

Spider client

Code: Select all

EnableExplicit
Global GadgetID, WindowID

#Window = 1
#Button = 2

Procedure ShowWindow()
  If OpenWindow(#Window, 200, 200, 300, 100, "CGI Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered);
    ButtonGadget(#Button, 30,  30,  60,  30, "Get")
    ProcedureReturn WindowID(#Window)
  EndIf
EndProcedure

Procedure HttpGetEvent(Success, Result$, UserData)
  If Success
    Debug Result$
  Else
    Debug "HTTPRequest(): Error"
  EndIf
 EndProcedure
 
Procedure GadgetEvent()
  Protected URL.s = "http://127.0.0.1:8080/cgi-bin/cgiprinter"
  
  GadgetID = EventGadget()
  Select GadgetID
    Case #Button
      HTTPRequest(#PB_HTTP_Get, URL, "", @HttpGetEvent())
  EndSelect
EndProcedure

If ShowWindow()
  BindEvent(#PB_Event_Gadget, @GadgetEvent())
EndIf
Purebasic CGI

Code: Select all

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

WriteCGIHeader("Access-Control-Allow-Origin:", "*")
WriteCGIHeader(#PB_CGI_HeaderContentType, "text/html", #PB_CGI_LastHeader) 

WriteCGIString("<html><title>PureBasic CGI</title><body>")  

Procedure WriteCGIConstant(Constant$)
  WriteCGIString(Constant$ + ": " + CGIVariable(Constant$)+"<br>")
EndProcedure

WriteCGIConstant(#PB_CGI_AuthType)
WriteCGIConstant(#PB_CGI_ContentLength)
WriteCGIConstant(#PB_CGI_HeaderContentType)
WriteCGIConstant(#PB_CGI_ServerSoftware)
WriteCGIConstant(#PB_CGI_HttpHost)

WriteCGIString("</body></html>")
Abyss Access Log
127.0.0.1 - - [26/May/2021:12:37:32 -0700] "GET /cgi-bin/cgiprinter HTTP/1.1" 200 191 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
127.0.0.1 - - [26/May/2021:12:38:20 -0700] "GET /cgi-bin/cgiprinter?_=1622057898450 HTTP/1.1" 200 191 "http://127.0.0.1:9080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
The first line is from pasting the URL from the Spiderbasic code into a browser and returns the expected results.
The second line is from running the Spider client.

When I said 'Error' in my first post, I referred to

Code: Select all

 Debug "HTTPRequest(): Error"
There are no errors in any Abyss logs I can see.

I hope this clears things up! Again, I am puzzled by that query string & value, SB seems to add out of thin air as well as the port 9080 that shows in the log - where is this (wrong?) port number coming from?

Again, thanks ofr any light you can shed...

Re: Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 8:24 pm
by the.weavster
I think you need to include the file extension in the URL. That worked for me with Apache, without the extension I got 404.

Code: Select all

Protected URL.s = "http://127.0.0.1:8080/cgi-bin/cgiprinter.exe"
Sometimes you see a CGI's extension edited to be .cgi in order to keep the client side scripts platform agnostic.

It's useful to open the developer tools in your browser and select the "Network" tab when you're trying to figure out what's going wrong in scenarios like this, that way you can see all the details of the request and the response including headers and status codes.

Re: Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 10:05 pm
by kpeters58
cgiprinter does not have any extension at all - I renamed the exe file PB created.

Will check if the dev. tools reveal anything else ...

Re: Spiderbasic invents query strings...?

Posted: Wed May 26, 2021 10:40 pm
by kpeters58
Finally its working!

Peter's crystal ball had it right - however he suggested:

WriteCGIHeader("Access-Control-Allow-Origin:", "*", #PB_CGI_LastHeader)

Apparently, the colon should not be included (looks like the library code adds it). The browser tools showed a message complaining about multiple CORS entries ': *' with only one being allowed.

So, my thanks go to both of you for your help!