-- Leo's gemini proxy

-- Connecting to gemini.marmaladefoo.com:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

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:


If no expression is passed to the script, we ask the user to provide one using the "10 input required" Gemini header.

If an expression is provided, we sanity check it then use Rebol to calculate the result.

The result is shown on a page with some guidance on usage.


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




-- Response ended

-- Page fetched on Fri Apr 19 23:47:06 2024