Spiderbasic invents query strings...?

Just starting out? Need help? Post your questions and find answers here.
kpeters58
Posts: 19
Joined: Fri Dec 07, 2018 7:27 pm
Location: BC, Canada

Spiderbasic invents query strings...?

Post 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
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Spiderbasic invents query strings...?

Post 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
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: Spiderbasic invents query strings...?

Post 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)
kpeters58
Posts: 19
Joined: Fri Dec 07, 2018 7:27 pm
Location: BC, Canada

Re: Spiderbasic invents query strings...?

Post 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...
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: Spiderbasic invents query strings...?

Post 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.
kpeters58
Posts: 19
Joined: Fri Dec 07, 2018 7:27 pm
Location: BC, Canada

Re: Spiderbasic invents query strings...?

Post 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 ...
kpeters58
Posts: 19
Joined: Fri Dec 07, 2018 7:27 pm
Location: BC, Canada

Re: Spiderbasic invents query strings...?

Post 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!
Post Reply