Page 1 of 1

How to ? : CGI, Websites and native Purebasic use

Posted: Sun Dec 17, 2006 6:59 pm
by Rings
as i need some cgi support (and don't want use a userlib) ,
i prefer using PB's native Input/writestring commands.
(crossplattform perhaps)
are there any examples out there how to write a simple
cgi.exe with a simple html example ?
cannot found any in the example-folder........


That is what i have today:

Purebasic (Api) Code:

Code: Select all

hInput = GetStdHandle_(#STD_INPUT_HANDLE)

SetConsoleMode_(hInput, #ENABLE_LINE_INPUT|#ENABLE_ECHO_INPUT|#ENABLE_PROCESSED_INPUT)

*ContentLength = AllocateMemory(128)

ContentLength = Val(PeekS(*ContentLength, GetEnvironmentVariable_("CONTENT_LENGTH", *ContentLength, 128)))

FreeMemory(*ContentLength)

*Buffer = AllocateMemory(ContentLength)

ReadFile_(hInput, *Buffer, ContentLength, @bRead, 0)

Input$ = PeekS(*Buffer)

FreeMemory(*Buffer)

CloseHandle_(hInput)

hOutput = GetStdHandle_(#STD_OUTPUT_HANDLE)

HttpAnswer$ = "Content-type: text/html"+Chr(10)+Chr(13)+Chr(10)+Chr(13)+"<html><header><title>Answer from CGI</title></header><body>Hi, received your data:<br>"

HttpAnswer$+StringField(StringField(Input$, 1, "&"), 1, "=")+": "+StringField(StringField(Input$, 1, "&"), 2, "=")+"<br>"+StringField(StringField(Input$, 2, "&"), 1, "=")+": "+StringField(StringField(Input$, 2, "&"), 2, "=")

HttpAnswer$+"</body></html>"

WriteFile_(hOutput, @HttpAnswer$, Len(HttpAnswer$), @Written, 0)

CloseHandle_(hOutput)
put the compiled console exe in cgibin folder.


and the html for the XAMP (apache) server:

Code: Select all

<H3>MultiImage Request Form cgitest</H3>
<P>
<FORM ACTION="../cgi-bin/CGIMIMAGE.exe" METHOD="POST" ENCTYPE="multipart/form-data">
<INPUT TYPE=FILE NAME="P1_imagefile" SIZE=35 MAXLENGTH=50><BR>
</P>
<P>
<INPUT TYPE="submit" VALUE="Send the pictures">
</P>
</FORM>

any help ?

Posted: Mon Dec 18, 2006 1:11 am
by Pantcho!!
You should try Paul's CGI lib!
I also tried to use PB native to make cgi's and it got weird.

Paul's lib supports all the most important things you can think on cgi programming also including multiple files uploads.

I have created cgi's exe with paul's lib and got great results.

You can judge for yourself here: http://www.cellphoto.net

the whole site is working under cgi's made with purebasic :)

Posted: Mon Dec 18, 2006 3:20 am
by freak
I don't see a problem really.
There is the GetEnvironmentVariable() command to access all the CGI variables,
like CONTENT_LENGTH, QUERY_STRING etc.

To read in the POST data, you can either use Input() to get it in string format,
or ReadConsoleData() to read it into a buffer.
To write back to the server, use Print(), PrintN() or WriteConsoleData()

It is all there since PB4. The linux version has all of it as well (allready wrote some CGI's with the linux version),
and the mac version will get it too of course.

Of course that is just the basic stuff... You still need to do stuff like
encoding/decoding urls yourself. If you want that done too, you'll need a userlib.

Posted: Mon Dec 18, 2006 9:44 am
by Rings
first, thx for your replies.
Pantcho:I dont't wanna use a userlib without source,
most ppl here know why.
And second, i don't wanna use native Api
(if i want, i can use that code i post in front, it works :)

I want use native 'Purebasic' for that.

And next,

Freak: Yes, i know its possible, that is why i ask here
for an example.
Purebasic could be the ideal tool for building
CGI's. Except that there are no example provided
from the offical Pure-team, or anyone else.

Posted: Mon Dec 18, 2006 11:55 am
by CadeX
Pantcho!! wrote:You should try Paul's CGI lib!
I also tried to use PB native to make cgi's and it got weird.

Paul's lib supports all the most important things you can think on cgi programming also including multiple files uploads.

I have created cgi's exe with paul's lib and got great results.

You can judge for yourself here: http://www.cellphoto.net

the whole site is working under cgi's made with purebasic :)
Is there even a PB 4.* version out yet?

Posted: Tue Dec 19, 2006 2:27 pm
by Rings