How to write "Hello World" CGI with Rebol 3?

Let's start with something simple: a form with a field that receives an echo.

+8
cgi rebol rebol3
source share
1 answer

At the time of this writing (2013-01), Rebol 3 still lacks a few CGI support functions that were related to Rebol 2. However, if you're fine by cracking the missing CGI support, you can still right now.

Before you begin, you need to save the R3 binary on the computer on which you want to run your CGI, and you need to know the full path to the place where you saved it (for simplicity). The following examples assume a Unix-style machine with the R3 binary in /usr/local/bin/rebol3 .

Start with something even simpler than you requested: CGI just sends "Hello, World!" page:

 #!/usr/local/bin/rebol3 -cs REBOL [] prin [ "Content-type: text/html" crlf crlf <!doctype html> <title> "Rebol 3 CGI Sample: Hello" </title> "Hello, World!" ] 

This is identical to what you write in R2.

Forward to something more interesting : reading and parsing HTML form submissions as you requested.

To do this, we need to know two things about CGI: the submitted data is passed as standard input to CGI; other CGI-specific information is transmitted from the web server via environment variables. We can access the input in R3 through the system/ports/input port and read the environment variables using get-env native.

Insert the HTML form into the CGI and make a mode switch inside the CGI: if the data has not been submitted, show the HTML form; if the data has been submitted, process it and show the corresponding response. We can do this by writing a form that submits data via the HTTP POST method, and then checks inside the CGI if it was called using the HTTP GET method (without data) or POST (form data). The method called by the CGI script is available through the REQUEST_METHOD environment variable.

With all that said here is a complete script without further ado:

 #!/usr/local/bin/rebol3 -cs REBOL [] handle-get: function [] [ prin [ "Content-type: text/html" crlf crlf <!doctype html> <title> "Rebol 3 CGI Sample: Form" </title> <form method="POST"> "Your name:" <input type="text" name="field"> <input type="submit"> </form> ] ] handle-post: function [] [ data: to string! read system/ports/input fields: parse data "&=" value: dehex select fields "field" prin [ "Content-type: text/html" crlf crlf <!doctype html> <title> "Rebol 3 CGI Sample: Response" </title> "Hello," (join value "!") ] ] main: does [ switch get-env "REQUEST_METHOD" [ "GET" [handle-get] "POST" [handle-post] ] ] main 

The final part of understanding this script is how to actually parse HTML form data submitted to CGI. Rebol 2 had a decode-cgi helper function for this, which Rebol 3 does not currently need.

However, for basic forms, it is enough to know that the CGI data is sent in an encoding that separates the fields with & , as well as the field name and value = ; everything is ok url coding. Therefore, if we present the form embedded above with the value "Charlie", CGI will get field=Charlie as input. Sending "Foo Bar" sends "field = Foo% 20Bar". So, again: for basic forms, a combination of parse ... "&=" (to split fields and field names and values) and dehex (to decode URL encoding), as shown above, will be sufficient.

+11
source share

All Articles