Page 1 of 2

The CGI path: can anyone shine a light?

Posted: Thu Mar 13, 2008 12:50 pm
by Seymour Clufley
Folks on the forum have said it is possible to make CGI programs with PB. Rings provides code for doing it in this thread. This appeals to me because I'd like to have a dynamic website running PB code - which I know well - instead of PHP/JavaScript/Perl/etc.

But how the hell do you get started?!

I've been trying to work this out all day!

For example, is it possible to test CGI offline? If so, do I need to install PHPDev to do it? And what's the deal with Perl - does it need to be installed to use CGI even when the thing was made in PB? And should I be worried that my hoster allows CGI scripts but makes no mention of executables?

I'm new to this and the whole area seems murky. Tutorials are sparse and seem geared to CGI "scripts" (not executables).

Can somebody explain what is and isn't possible with a website running PB executables? And can you test what you make on an offline PC?

Posted: Thu Mar 13, 2008 1:17 pm
by pdwyer
You need a web server that has a directory that you keep your PB executables in, lets say "cgi-bin" then the exe's in there need to have executable permissions set. If you are running IIS6 you will need to explicitly allow cgi to run.

Ok, so you set you virtual directory or web sub directory up and put your exe in there. The exe only has to do one thing and that is write to the std out ( print() and printn() ) whatever it spits out will go to the browser so you need to pass it an HTML page of some sort.

You exe can be passed parameters (7bit ascii only I think so you will need to transliate the hex if you want other bytes) Post and get params are passed differently

You can collect environment variables to find out about the client, its IP etc etc.

To test I guess you could run your exe and pipe the output to an html file and open it in a browser but you won't have access to the web server environment vars.

The HTML code can be a pain, lots of quotes and double quotes. What I often did is create a template file which is an html file I wrote in a wysiwyg editor then put some replace tags in there like <%NAME%> Then in your code, load the template and do a replace on that text with the name you want to add, this way when you want to make a cosmetic change you just update the template and don't have to recompile the exe

Rings seems to have most of the coding details covered though

Posted: Thu Mar 13, 2008 1:17 pm
by Num3
Cgi scripts can be or not executables, it depends on your webserver and OS platform...

Most hosts don't allow you to upload executable scripts, for security reasons, so using pb for CGI is not possible!

If you want to test a nice portable apache server, download xampp, you can select quite a few packages for it, including php, perl, mysql etc...
I was able to cram the server into a 128mb pen drive with mysql and php5 and runs on all machines i tested :D

quite nice to make websites!

Posted: Thu Mar 13, 2008 1:19 pm
by pdwyer
you can have a bat file if you like and have it output a web page with echo statements :)

Done a LOT of this

Posted: Wed Mar 26, 2008 2:16 am
by StanDan
I started a project to have a unified PB CGI/Apache mod/ISAPI library. I finished the CGI portion of the code and part of the Apache 1.x module.

The idea is:

Write your .dll/.so file in PB so it has a single Handler() procedure. This acts as a handler that can be called from an ApacheAPI .so, ISAPI .dll or a CGI .exe that sets up a similar environment to the Apache/IIS one and calls Handler(). They all set up the same environment for your code. If you want to access a parameter called "foo" then you can do:

Code: Select all

Procedure Handler(*r.Request)
  SetContentType("text/html")
  PrintN("<p>"+GetParam("foo")+"</p>")
  ProcedureReturn #OK
EndProcedure
Wouldn't this be COOL!!

The thing is, writing a multipart/form-data parser is a incredible pain. So much so that you can't even find a MIT/BSD licensed parser anywhere. After reading twenty or so RFCs and coding for three weeks I had a broken parser that worked sometimes and failed others. So I gave up.

If I ever get it finished I'll release it under the MIT license. It's the kind of thing that would require many hands to make perfect.

Anyway...

Writing a CGI executable is really easy. The simplest PB one is:

Code: Select all

OpenConsole()
PrintN("Content-type: text/html")
PrintN("") ; Important, the header is "Content-type: text/html\r\n\r\n"
PrintN("<p>Hello World!</p>")
Just drop the .exe into the cgi-bin directory in an a fresh Apache install and then (if Apache is installed on your machine and started) go to:

http://localhost/cgi-bin/myscript.exe

You should see "Hello World". Although you might need to add an "AddHandler cgi-script .exe" directive to the httpd.conf file.

Then in your program you can get CGI parameters from the environment. If you go to:

http://localhost/cgi-bin/myscript.exe?foo=bar&baz=bat

Then when you do a:

Code: Select all

PrintN(GetEnvironmentVariable("QUERY_STRING"))
It will print "foo=bar&baz=bat" to the screen. So all you've gotta do is extract the keys and values from the string.

PB is a great alternative for CGI programming because it's FAST, small and one of the easiest compiled languages to handle text with.

Posted: Wed Mar 26, 2008 4:01 am
by Seymour Clufley
Hi StanDan,

If you don't mind me saying, that post was quite a joy to read! Your enthusiasm is luminous.

Since starting this thread I have come to grips with the subject to a degree. Spent a week trying to get Xampp working only to find I was making a mistake so mundane that I now can't even remember what it was! All sorted out now and Xampp works a treat.

So, thanks for the "quickstart" guide to doing PB CGI but I've pretty much got it sussed now. It will undoubtedly be useful to others in the future, though, as other threads on the subject are comparatively long-winded.

The only question I still have about PB/CGI is how do you change the URL? I mean, when you hit a button, it starts the PB executable which generates a new page... that page has .exe at the end of its url, which looks bizarre. Does anybody know a way round this?

Posted: Wed Mar 26, 2008 4:46 am
by pdwyer
I don't think it has to be an exe in name, if you rename it to not having an extention then the web server needs to know that it's an exe to execute it like one and not all web servers have that feature.

Another way around is, if it's the only exe in the that path eg \Cgi-bin\Startup\ you can set the default return page (usually index.html or default.asp etc) to be Myprog.exe and it will not show in the url.

I understand why you are doing this, having "exe" in the URL can invite hackers as they may think since it's a windows server so not as well maintenained for security. This stigma is starting to fade though as windows web server become more common.

For ISAPI, personally, I'm not a big fan of it. Its definately faster as it's pre-loaded in memory etc, but IIS tends to require a web site shut down in order to unload it and update it. exe's and scripts don't have this problem.

Posted: Fri Mar 28, 2008 4:21 am
by Seymour Clufley
pdwyer wrote:Another way around is, if it's the only exe in the that path eg \Cgi-bin\Startup\ you can set the default return page (usually index.html or default.asp etc) to be Myprog.exe and it will not show in the url.
Thanks. When you say "set the default return page", do you mean setting the default index page in an htaccess file in that directory?
I understand why you are doing this, having "exe" in the URL can invite hackers
Well there's certainly that too, but my primary reason is that it just looks so weird. Perhaps to people less familiar with programming, who don't even know what an "exe" is, it would look less strange. To me it jumps out a mile as being profoundly "un-webpage".


There is one other thing that's come up. Forgive me if I'm being dim and forgotten something obvious, but how would one go about "returning a graphic" instead of returning html? Say you had drawn an image with the 2D drawing lib, what would you then need to do to return it, and in a web-compatible form?

Posted: Fri Mar 28, 2008 5:48 am
by pdwyer
Not sure about htaccess, I think that an apache conf file? In IIS which is the only web server I know, in the properties for the web site, go to the "documents" tab and set the order there. If you then have a URL like: Http://mydomain.com/path/ in the "path" properties you can have documents set and IIS will search them in order and the first one it finds it will send/execute. Exe's will need to be set with execute only permissions, if set with read permissions too then they can be downloaded (stolen).

For graphics, the easiest way is to output the file name to a file and pass the html back that links it. I know ASP has (similar to document.write) a data.write or something similar to pass binary back as a name to link into html but I'm not sure how to do that in CGI. Looking at the http spec might give you hint on how to header it.

Exe's looks strange but I've seen them from time to time. you just used to asp, php, pl etc which are just script formats. Just try killing the exe extention and setting the permissions to execute and see what happens ;)

"Installing" a CGI executable in my webspace...

Posted: Mon Jan 12, 2009 7:50 am
by Seymour Clufley
Last September I bought a domain name and began to set up a website.

My main priority was that the hosting service allow me to use CGI executables (made in PureBasic). So I needed the server to be Windows-based. The company 1&1 offered this and they specifically say the following on their website:
You can install virtually any application on your web space so long as you do not require root access for the installation
(from here)

I've uploaded several HTML pages that link to each other and that, very simple, stuff works fine.

Now I just uploaded a PB CGI executable along with a test html page.

No go!

Copied the exe into the "cgi-bin" directory. Again, no success.

So I phone up 1&1 and (after asking what the exe was for and why I needed it etc., fair enough questions) the guy told me the server does not recognise exe files and I would have to "install" this exe.

This is the first I've heard about this and I don't know what to do. I asked him and he said this was outside the scope of their technical assistance so I would have to find out myself. He did promise to look for some "helpful material" and email it to me but so far nothing's arrived.

So I did some googling. Unfortunately CGI done with any language other than Perl seems to be a rarity. Everything I can find relates to specific circumstances such as a particular host or a particular CGI script. What I need is a generic solution. Just a normal exe being allowed to run on a web server.

Does anyone have experience of this? What does "installing" a CGI executable entail or even mean?!

Posted: Mon Jan 12, 2009 8:23 am
by blueznl
CGI is one of the subjects I've been thinking to discuss on the Survival Guide. Unfortunately, I haven't got a clue myself, and I'm still struggling with graphics and games stuff...

Now, if you people work out the kinks, then I can claim the credit :-)

Posted: Mon Jan 12, 2009 8:32 am
by Seymour Clufley
I'm absolutely furious.

I just phoned again and got a rather clearer explanation:

"No executable files will work on our servers. For this you would need a dedicated server where you install everything yourself."

So even though their site states "you can install virtually any application on your web space", you actually can't.

To pour salt into the wound, I was so reassured by that quote on their site that back in September I foolishly signed up for a cheap 2-year deal. If I cancel now, they'll keep taking money out of my account every month until September 2010!

I'm sorry to vent spleen like this but I feel like a complete idiot. Talk about learning a lesson the hard way! What a complete a**e I've been.

Well, lesson learned.

Now I don't know how to proceed. Do I have any recourse at all to getting the contract terminated sooner? Probably not.

Any wisdom would be VERY much appreciated at this point.

Thanks for reading this tirade anyway,
Seymour.

Posted: Mon Jan 12, 2009 8:36 am
by pdwyer
so what apps can you make? ASP? PHP? Perl?

What is the OS of the web server and what is the web server (apache?). PB might not work but there may be other options here.

You could always ask for a refund since you found their service description deceptive.

Posted: Mon Jan 12, 2009 11:04 am
by Trond
What provider is this? Surely the should have some clear information available somewhere on what works on what does not.

Posted: Mon Jan 12, 2009 11:21 am
by JCV
Seymour Clufley wrote:The company 1&1 offered this and they specifically say the following on their website: ...
1and1.com