1. Using Euphoria for CGI programming

To Grape Vine et all (warning: long CGI specific post ahead):

I'm glad not to be the only one interested on using Euphoria as a CGI
programming language. I'll try to provide with my limited english
vocabulary what I have learned (empirically) over the past months.

CGI is a way to build on-the-fly HTML files. There are plenty of HTML
tutorial and authoring tools on the net (search Yahoo or Hotbot), so I
won't bother in trying to teach HTML here. There's a great tool named
"HomeSite" from Allaire (www.allaire.com), it's not only the best HTML
editor on the market, but also includes a complete HTML reference. You may
download a 30 days evaluation version (fully functional).

First we need to understand what happens behind the scenes when we type (or
click) a URL on our browser (I'll skip the DNS stuff). The browser tries to
connect to port 80 of the server. If there is a webserver running on the
server (and listening to port 80), a connection is established. The protcol
used in this connection is HTTP. To retrieve an HTML file (page) from the
server, the browser sends a GET command.

What the webserver does with this command is not of the browsers bussiness.
You may configure your webserver in such a way that when a GET one.html is
recieved, the webserver send's page two.html back. I hope it's clear
enough. The point is that the webserver's "file space" is not like a normal
file system, it can be tweaked, one URL doesn't mean one distinct file.

If the URL to retrieve happens to be a CGI, the webserver executes it and
sends back to the browser the output of that program. CGIs are great for
database front-ends. But what *is* a CGI? A CGI is any file that can be
executed. On *nix world that means programs and shell scripts, on Dos/Win
that means .exe, .com, .bat, and .cmd (NT). For security reasons webserver
usually don't allow to just go and execute winword.exe, they must be on a
special directory (generally cgi-bin).


Another question pops in our minds: How does my CGI gets it's input, and
how to output the result? To solve this is that a CGI standard was created.
In escence the CGI get's it's input from stdin, and sends it's output to
stdout. In *nix that's easy, on Windows platforms that's more complicated.
I'll focus on Windows platforms using the Xitami webserver
(www.sitami.net), 'cause there's no Eu4*nix, and 'cause I'm using WinNT
(Xitami also runs on 95/98, and *nix!).

On HTML there are this things called forms. A form is a collection of
related data that can be submited to a CGI for it's processing. A form is
defined with the <FORM> tag. The parameters of the tag specify the CGI URL
where to send the data, the method to be used to submit the data (GET ot
POST), and the encoding type. You can safely ommint the encoding mime type.

<FORM ACTION="_URL_" METHOD="GET|POST" ENCTYPE="MIME type">
</FORM>

Within the form definition you create fields of data. A field can be a
button, checkbox, radiobutton, text line, text area, etc. A field is
defined with the <INPUT> tag.

Here's a form that sends a username and email to the CGI located at
http://some.server/cgi-bin/mycgi.cgi:

<FORM ACTION="http://some.server/cgi-bin/mycgi.cgi" METHOD="POST">
        Enter username:<BR>
        <INPUT NAME="USERNAME" TYPE="TEXT" SIZE="20"><BR><BR>
        Enter email:<BR>
        <INPUT NAME="EMAIL" TYPE="TEXT" SIZE="10"><BR><BR>
        <INPUT TYPE="SUBMIT">
</FORM>

BTW <BR> is a break of line.

When the user press the "Submit" button on the browser window, the values
of both (username and email) fields are sent to mycgi.cgi. These pair of
values (USERNAME=some_value&EMAIL=some_other_value) can be read by the CGI
from stdin. Xitami makes it a bit more easier by parsing these values for
you and creating one enviormental variable for each field. You can
configure Xitami to prepend to the field name a defined string, by default
it is "FORM_". A CGI under XItami should then read the enviormental
variables FORM_USERNAME and FORM_EMAIL to get the appropiate values.


As I have mentioned before stdin and stdout may be a hassle on Win32, so
Xitami also provides an alternative. It gives you files from where to read
and write, as stdin and stdout alternatives respectivly. The filenames are
available from the enviormental variables CGI_STDIN and CGI_STDOUT. These
files are created by Xitami on the xitami\temp directory. CGI_* filenames
doesn't include a full pathname, so you must "fix" them before opening the
files, usually with "c:\\xitami\\temp\\" & CGI_* (It depends on where you
installed Xitami and how you've configured it). You won't use CGI_STDIN
usually, so don't even bother in reading it.

The output of the CGI should be sent to the above CG_STDOUT file. One
important thing is that the very first line sent must be a mime header,
usually of type text/html, followed by at least one blank line and then the
actual data. Note that the CGI could as well output an image or any other
binary data, as long a your mime header correctly identifies the data sent.

As you see a CGI is just like any other application, the only difference is
where and how to recieve it's input and send it's output.

Now that we know what and how a CGI does, let's see how to do it in Euphoria:

Let's create a small EuCGI (my name for CGIs written with Euphoria), that
processes the above FORM.

[warning: untested code (but should work), directly typed in the mail
client. No warranties are implied ;) ]

--mycgi.exw
sequence username, email, outfile
integer out

--Get output file
outfile = getenv("CGI_STDOUT")
outfile = "c:\\xitami\\temp\\" & outfile

--Get form data
username = getenv("FORM_USERNAME")
email = getenv("FORM_EMAIL")

--Open output file for APPEND
out = open(outfile, "a")

--MIME header
puts(out, "Content-type: text/html\n\n")

--Put the html data
printf(out, "<html><head><title>EuCGI test</test></head><body><center>Your
{username, email})

--Close output file
close(out)

We have just one pending topic. An .exw file ain't directly executable. We
have two choices:
a) Bound it to create an .exe file.
b) Execute exw.exe with the path to the .exw file.

Alternative a) is limited to registred users. I'll show a technique I
"discovered" to perform b). Xitami let's a CGI to be a Perl script. In Perl
the # character marks the begining of a comment (like '--' in Euphoria), so
if you put in the very first line of the script:

#! full_path_to_perl_interpreter

Xitami will spawn the Perl interpreter and pass it as parameter the path of
the script. When the script is interpreted by Perl this first line will be
ignored because it's a comment.

We can't do this directly in our Euphoria source code because # isn't a
comment delimiter in Euphoria. The workaround is to create a dummy text
file that will be used to fool Xitami:

#! full_path_to_exw.exe full_path_to_exw_file

So if you saved mycgi.exw on c:\EuCode, the dummy file should be:
#! c:\euphoria\bin\exw.exe c:\eucode\mycgi.exw

The dummy file can be named whatever you want, but in this example it must
be mycgi.cgi (remember the ACTION=... of the form?), and has to be located
on the cgi-bin directory.

I hope this document clarifies some doughts about CGIs and Euphoria.


Regards,
          Daniel  Berstein
         [ daber at pair.com ]

new topic     » topic index » view message » categorize

2. Re: Using Euphoria for CGI programming

Regarding Daniel Berstein's CGI note, I'd like to comment briefly.

CGI does have a heavy involvement with HTML.   Sometimes this means building
HTML files on the fly and sometimes it means taking information from an HTML
file / form, organising it, and putting it in a database  It can be used to
page someone when a stock reaches a certain critical value or it could be
used to send data to some other device based on some condition being met.

In other words, CGI is often used to permit what we might call "Internet
programming" that overcomes the very static way in which HTML processes
information.   However, the HTML connection is NOT by any means the only way
to use CGI.   CGI can be used to screen users, redirect users, and even
gather information (on users, or what they are doing) to be put into some
format such as a database, and often will not be used on the net as html at
all.

So CGI might be described as a kind of bridge, and certainly it can and
usually is used WITH html and sometimes it is used to build HTML files on
the fly.   But it can also extract and organise data from a wide variety of
sources (HTML files, HTML forms, databases via ODBC, and from physical
devices),

But as anyone who is familiar with Perl will be quick to point out, CGI goes
far beyond mere HTML.  It is a way to program on the Internet.

Additionally, cgi files used to be restricted to special directories, often
called CGI-BIN or CGI-LOCAL (for example) for security reasons, but in
recent years, most servers permit the execution of CGI files in any
directory.

I'm certainly NOT questioning Daniel's understanding -- just adding to his
statement so that it might be more complete.

--Warren

---------------------------------------------------------------------
Protect privacy, boycott Intel.   Why?  Go to
http://www.bigbrotherinside.org for a full explanation.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Using Euphoria for CGI programming

A quick comment:

I think Euphoria has the potential to become a great CGI language.  When
people think of CGI, they probably think of Perl or C/C++, both not very
fun for the average user.

Imagine just *one* free webspace provider installing euphoria for CGI
use, possibly instead of the standard, perl.  Suddenly users on the
server can easily create CGI programs to to any number of tasks usually
done by perl or C programs.  Euphoria is well suited to almost any CGI
task, be it a hit counter or a online database.

Anyways, just a thought
Greg

--
Greg Phillips
i.shoot at rednecks.com
http://euphoria.server101.com
--

Useless fact of the day:

The thumbnail grows the slowest; the middle nail grows the fastest

new topic     » goto parent     » topic index » view message » categorize

4. Re: Using Euphoria for CGI programming

>I'm certainly NOT questioning Daniel's understanding -- just adding to his
>statement so that it might be more complete.

Thanks.

The previous post was an "introduction" to CGI, and only was focused on the
tangible part of the CGI script: input and output.

Regards,
          Daniel  Berstein
         [ daber at pair.com ]

new topic     » goto parent     » topic index » view message » categorize

5. Re: Using Euphoria for CGI programming

Daniel, in your excellent introduction you clearly stated that   "CGI is a
way to build on-the-fly HTML files. ".   Certainly you are correct that
there is a lot of potential in the HTML-related aspects of CGI.

HOwever, since CGI goes MUCH further than just HTML, I felt compelled to
address this omission.   Again, thanks for your input.

---------------------------------------------------------------------
Protect privacy, boycott Intel.   Why?  Go to
http://www.bigbrotherinside.org for a full explanation.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Using Euphoria for CGI programming

Great job, Daniel.  I really understood a lot from your post.

Alan
  =

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu