libcgi "go to url"

Andre Garzia andre at andregarzia.com
Sun Oct 7 15:59:57 EDT 2007


Hello Chris,

sorry for the late reply, I think I understand what you're trying to
accomplish and will write some words on the topic, if I understood you
wrong, then, sorry, english is not my strongest quality.

Let us divide your problem in two topics: "templating" and "answering"

Templating is the act of having a placeholder file or stack that is used by
the cgi to answer to a request, so you put your presentation layer into one
of those templates then with some calls, you glue your data into them. We're
specifically talking about building HTML like files. So you can have a
default HTML template and just glue your data in it.

When you run a Revolution CGI, the CGI has a "default folder" (global
property the defaultFolder), all it's file operations are relative to such
location, you can change it by setting it to a new folder path such as:

    set the defaultfolder to "/mySweetTemplates/"

You can set it to absolute paths (absolute paths are relative too but they
are relative to the root folder of your file system) or relative path. If
you set the default folder to a relative path, it will be set using the
previous known location and then override this location. This in plain
english means that if the default folder is your cgi folder and you have a
"template" folder inside it, then executing the following line:

    set the defaultfolder to "templates/"

will set all the file operations to be relative to such child folder. To
read or write files, the best option is to use URL keywork commands such as
you're already doing. Now to glue data into your template there are many
ways, I'll just explore two in here.

First is to use the replace command. You fill your template html file with
place holders such as "###NAME###", each of those placeholders will be
replaced with the actual data by using multiple replace calls. Using your
previous example, let us replace some values, consider that your HTML file
has something like ###NAME### and ###COLOR### in it, and it is answering the
name and the  favorite color of the user. You can then call something like:

    replace "###NAME###" with tName in tData

    replace "###COLOR###" with tFavoriteColor in tData

So with each replace call you glue one piece of your data back in the tData
template. This is a very straight forward way of doing templates in
Revolution. Another popular way that requires less coding and is more
flexible is to use the merge command. This command will pick a variable and
inspect it looking for pre-defines placeholders, it will then replace these
place holders with the result of their values. Revolution uses double
brackets as place holders for the merge call, so you just fill your template
with double brackets and your variables. Using our previous example, imagine
that your template looks like

    <html>

    <head><title>My Fav Color</title></head>

    <body>

    Hello, [[tName]]

    Your favorite color is [[tFavoriteColor]]

    </body>

    </html>

then if you execute the call:

    put the merge of tData into tAssembledData

The resulting variable called tAssembledData will contain the content of the
template with those double bracket place holders replaced with the actual
variable content. This is very flexible because you can change your template
to display different data without changing your program code.

OBS: the merge call does more than variable glueing. This is just a quick
note...

So now you should have some mental image of how to glue data into your
template and end up with a variable that contains the answer you want to
send back to the browser, let us talk about answering to the browser now.

In your example you try to use RevGoURL. This command is to be used by
desktop application to open the browser directed to some web page, this is
not to be used by CGI applications. It's a purelly desktop command that
opens another program and launches a web page.

Most web servers are running under some kind of unix or follow some
conventions set by the unix familly of operating systems. To explain how the
cgi answers back to the browser let me talk a little about the standard i/o
common to C programs. There are three important standard i/o, they are
STDIN, STDOUT and STDERR. They mean "standard input", "standard output" and
"standard error output".

You can think of them as files, three different files used for different
purposes. STDIN always mean the standard input interface, it may be the
keyboard or a piped file. When you use a CGI, the STDIN is written by the
web server to contain the web request, so when you read from STDIN using a
CGI you should read the raw web request data.



STDOUT always mean the standard output, in some cases is the screen, in
others it may be a file. When you talk about CGI, the STDOUT will be grabed
by the web server and sent back to the browser. So your CGI will write it's
output to STDOUT and this will be piped back to the browser.

STDERR points to the standard error output, like STDOUT it differs for each
context, in the cases of CGI, the web server will grab STDERR and log it to
system log file. So if something wrong happens in your cgi you can have the
web server to log useful information by writting to STDERR.

Okay, now we know that using these three "files" we're able to interface
with the web server and thus talk with the web browsers. When executing  a
revolution CGI, the console becomes STDOUT, so all your put commands that
would go to the message box when run from the IDE will actually go to
STDOUT. So to send data back to the browser, you simply use the put command.
Like this:

    put tAssembledData

This will send tAssembledData to STDOUT. Be aware that the web browser
expects a fully HTTP compliant answer. Some web servers will inspect what
you write to STDOUT and fix missing information but most will not. Below I
give you a very simple output function that your can use to assemble a
minimal response back to the browser.

on cgiOutput pData

    put "200 OK HTTP/1.0" & cr

    put "Content-type: text/html" & cr

    put "Content-lenght:" && the lenght of pData & cr & cr

    put pData

end cgiOutput

using this simple function you can then call:

    cgiOutput tAssembledData

and have the HTML stored in that variable to display in the web browser. If
you want to look further into these topics, I advise you to follow Jacques
CGI tutorial at:

   http://hyperactivesw.com/cgitutorial/index.html

I'll also tell you some buzzwords that you may find userful. Look into
"XSLT" for your templates, it is a standard way to build HTML templates and
most server boxes come with "xsltproc" installed so you can very quickly
assemble and web response by calling xsltproc thru the shell() command. Also
read JM Marshall "HTTP Made Easy" eBook, it will give you a basic grasp of
the HTTP protocol. Very useful.

and then use my own RevHTTP server from the Revolution IDE to learn more
about CGIs. You can learn more about it at
http://www.andregarzia.com/RevOnRockets

Cheers, I hope this help you.

Andre


Hello

On 10/1/07, chris livermore <chrisliv at unimelb.edu.au> wrote:
>
> Hi
> I have libCGI working nicely (thanks for this stack), I'm storing/
> writing variables to text files all OK. Problem is sending a client
> back to a web page.
> So the client clicks 'Submit' button, all parameters are processed;
> This script:  put url ("file:../test/switchatest/
> parameters_summary.html") into tData
> finds the correct page but because the page's links are relative,
> none of the graphics are shown. I'll convert all to absolute links
> but there must be an easier way to go to any url?
> I've tried: get url ("file:../test/switchatest/
> parameters_summary.html") into tData
> and: revGoURL "http://www.kipmultimedia.com/test/switchatest/
> parameters_oral.html"
> but no luck.
> I have a feeling this is going to be embarrassingly simple, thanks
> for a great forum.
>
> macintel 10.4.10, rev 2.8.1 (3)
>
> regards
>
> chris
>
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>



More information about the use-livecode mailing list