How to Process Inputs to a REBOL CGI Web Page

Processing HTML Forms in a REBOL Internet Based Application

Jul 24, 2009 Mark Alexander Bain

Web Pages users normally pass information to applications by means of an HTML Form. The REBOL programmer can take those inputs and use them in their CGI scripts.

If a programmer needs to implement business rules across an organisation then REBOL is a suitable solution. The business rules can be mapped to a custom REBOL dialects and REBPOL itself is available in a number of versions, both free and commercial. For example anyone can download:

  • REBOL/View - this provides a GUI (Graphical User Interface) that enables the programmer to write and test business rules
  • REBOL/Core - this has no GUI and has been designed for use on server

Therefore, if the programmer has access to a web server (such as Apache) then they can produce quickly produce REBOL CGI web pages for all of their users to view. Of course the next stage is for the developer to create REBOL CGI applications that can both display information for the users, and to accept information from them. For that to happen the REBOL must be able to process data sent from HTML forms.

An Example HTML Form

A user will enter information into an HTML form in a web page. The programmer must create such a form, for example:

<form method=get action="\cgi-bin\process_input.r">
Enter your full name:<input name=full_name>
<input type=submit>
</form>

Here the form will call the "process_input.r" script file when the user clicks on the submit button.

Identifying the Request Method with REBOL

There are two methods that the web programmer can use to send information from the form to the CGI application:

  • GET - information is passed as part of the URL. This is visible to the user
  • POST - information is passes via the standard input and is invisible to the user

The CGI application must, therefore, be able to identify which method is being used and then to handle the inputs accordingly. The first step is to add the first three obligatory lines that must appear at the start of every REBOL CGI file. These lines are:

  • the shebang line - this tells the server where to find the REBOL interpretor
  • the REBOL header line
  • the CGI header line

They will look like:

#!C:\REBOL\rebcore -c
REBOL[]
print "content-type: text/html^/"

And, in this example, the request method being used is displayed in the browser:

print system/options/cgi/request-method

Figure 1 (at the bottom of this article) shows the results when using the GET method, and figure 2 shows the results of using the POST method. The next step is to process the inputs according to the sending method being used.

Processing the CGI Inputs with REBOL

The CGI variables will start life as a string, either read in from the string sent as part of the URL (in the case of the GET method) or it will be read in from the standard input (in the case of the POST method). The first step, therefore, is to create the variable that will store this information. In this case it's been given and initial size of 1 KB:

cgi-string: make string! 1024

The CGI input is then read into this string:

switch system/options/cgi/request-method [
"GET" [cgi-string: system/options/cgi/query-string]
"POST" [read-io system/ports/input cgi-string 1024]
]

At the moment the string is not in a usable form;

  • all the variables input will form part of the string
  • the string will contain control characters

However, all of that can be resolved in a single statement:

cgi: construct decode-cgi cgi-string

This new object (named "cgi") will contain a field for each of the CGI variables, for example:

print [ "Hello" cgi/full_name ]

Figure 3 shows this simple process in operation, and that's the important thing for any programmer to remember: it is a very simple procedure, but it enables the REBOL programmer to create highly sophisticated business rules based applications.

The copyright of the article How to Process Inputs to a REBOL CGI Web Page in Computer Programming is owned by Mark Alexander Bain. Permission to republish How to Process Inputs to a REBOL CGI Web Page in print or online must be granted by the author in writing.
How to Process Inputs to a REBOL CGI Web Page, Mark Alexander Bain How to Process Inputs to a REBOL CGI Web Page
Figure 1: Processing a GET Form, Mark Alexander Bain Figure 1: Processing a GET Form
Figure 2: Processing a POST Form, Mark Alexander Bain Figure 2: Processing a POST Form
Figure 3: Processing the CGI Variables, Mark Alexander Bain Figure 3: Processing the CGI Variables
   
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 4+1?

Comments

Aug 25, 2009 8:15 PM
Guest :
alright mark....

im a total newb to rebol or scripting and im in need of tha fuly working example to build on later.....

the problem and i hope an interesting one is this.

i Need to parse this page for a every single file download it has , open the referenced page of the cecond URL and download the file to the lcal or remote website over ftp or http.

its hoped that many people will be putting their video clips on there over time and we NEED a reliable way to catch and parse the ever changing dynamic URLs as referenced from the top dir..

http://cid-bee3c9ac9541c85b.skydrive.live.com/browse.aspx/.Public/Match%2 0Point

leading to many other files you most parse to download the currently active file section....

MS just have to do it their own way instead of foling standards but im hopeing Rebol view will be able to do this simply and show people how easy it can be with a request get for all files and even populedted to a GUI screen or two....

can you help us and write one that works, im totally lost, OR cant rebol actually do this with the MS pages, and i need to use something better that can... ?

thanks for the help and helping people realise how rebol and MS multi files pages can be colected , parsed and downloaded automaticly ratehr than some obscure linux mojo wget heads and tails line that work first time every time but noone really understands ;)
1 Comment: