CGI with input

Next in the tests is to get a CGI script to query the user for input.

In this exercise we will implement an arbitrary script to perform simple, yet aribitrary, arithmetic calculations provided by the user.

We want to build a user interface that lets users provide their own arithmetic expression, and then have this calculated.

The basic logic is as follows:

The calculator

Go to the calculator

Design summary

Here is a summary of the overall design and logical flow. The actual calculation of the expression is left as an exercise for the reader, but is not too difficult in a dynamic language like Rebol.

First we need to get the query string from the server if it is provided and decode it.

get-query-string: does [
    query: copy ""
    if (error? try [query: url-decode get-env "QUERY_STRING"]) [query: copy ""]
    if unset? query [query: copy ""]
    
    :query
]

If no expression is provided we ask the user for input using the Gemini 10 response type

handle-request: funct [query] [

    either ("" =  query) [
        ;--request input
        prin join "10 Please provide an expression to calculate" crlf
        quit/return 0   ;--exit the script
    ] [
        ;--continue and return content to user
        prin join "20 text/gemini; charset=utf-8" crlf    
    ]

]

Next we do some sanity checks on the query expression and calculate the result.

Finally we return the content back to the user, displaying the calculated result.

prin build-page query

Home