cgi.lsp

Module index

source

Module: cgi.lsp

Basic CGI processing tools for GET and POST requests

Version: v 2.2 - comments redone for automatic documentation
Version: v 2.3 - a fix in CGI:url-translate when translating utf-8 strings
Version: v 2.4 - check for empty string when aquiring HTTP_COOKIE environment
Author: Lutz Mueller, lutz@nuevatec.com

This module defines basic CGI processing tools for processing CGI GET and POST requests and cookies.

Include this file at the beginning of each file performing CGI processing:
 (load "/usr/share/newlisp/cgi.lsp")
 

Overview

On loading cgi.lsp will retrieve GET, POST and cookie parameters via standard input and the environment variables: QUERY_STRING and HTTP_COOKIE. These environment variables are set by the webserver (tested with Apache 1.3). The webserver is receiving information back from cgi.lsp via std I/O channels.

After having loaded this file all parameters from either GET or POST method are stored as an association list and in CGI:params and individual parameters can be accessed using CGI:get.

All cookies can be accessed in an association list CGI:cookies and are accessed similar to the GET and PUT parameters using CGI:get-cookie. A function CGI:set-cookie is available for setting cookies.

The function CGI:put-page outputs a HTML page to the webserver after processing newLISP source embedded in <% and %> tags.

CGI:params and CGI:cookies contain the empty list () when no parameters or cookies are present

The function CGI:put-page can be used to output web pages containing newLISP source embedded in <%, %> tags. Inside these tags are newLISP statements printing output/HTML to the webpage.

- § -

CGI:put-page

syntax: (CGI:put-page str-file-name)

return: The page output to standard out.

Processes an HTML page by evaluating newLISP source embedded into the HTML text between <% and %> tags. The newLISP source typically contains print and println statements to output strings to standard out.

example:
 <html>
 <body>
 <% (set 'site "example.com") %>
 <a href="http://<% (print site) %>"><% (print site) %></a>
 </body>
 </html>
 
 ; will output:
 
 <pre>
     <html>
     <body>
     <a href="http://example.com">example.com</a>
     </body>
     </html>
 </pre>

- § -

CGI:url-translate

syntax: (CGI:url-translate str-url-format)

return: An ASCII formatted string.

Translates all URL formatted characters to ASCII. Translates '+' into spaces and %nn hexdigits into characters. nn is a 2-nibble hex number.

example:
 (CGI:url-translate "What+time+is+it%3f")  => "What time is it?"

- § -

CGI:set-cookie

syntax: (CGI:set-cookie str-var str-value str-domain str-path)
parameter: str-var - The cookie variable name as a string.
parameter: str-value - The cookie value as a string.
parameter: str-domain - The domain where to set the cookie.
parameter: str-path - The path for the domain.

return: The string sent to standard out by CGI:set-cookie.

This function should be called immedeately before closing the header with (print "Content-type: text/html\r\n\r\n"), which is typically the first statement in a CGI script written in newLISP after the (load "cgi.lsp") statement.

example:
 (load "cgi.lsp")

 (CGI:set-cookie "password" "secret" "asite.com" "/somedir")
 (print "Content-type: text/html\r\n\r\n")
 ...

- § -

CGI:get-cookie

syntax: (CGI:get-cookie str-key)
parameter: str-key - The string for the cookie variable name.

return: The string for the cookie value.

example:
 (CGI:get-cookie "login")   =>  "somebody" 

- § -

CGI:get

syntax: (CGI:get str-key)
parameter: The - name of the GET or POST variable as a string.

return: The value string of the GET or POST variable.

example:
 (CGI:get "city") => "San Francisco"

- ∂ -

generated with newLISP  and newLISPdoc