cgi.lsp

Module index

source download

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
Version: v 2.5 - cleanup put-page for 10.0
Version: v 2.6 - help text corrections
Version: v 2.72 - help text corrections
Version: v 2.8 - check for and use CONTENT_LENGTH when reading POST data
Version: v 2.9 - eliminate deprecated integer -> int
Version: v 2.91 - minor code cleanup in CONTENT_LENGTH handling
Version: v 3.0 - handle multipart POST data. Added by Unya, Oct 2012
Version: v 3.2 - fixed for large POST data
Author: Lutz Mueller, Unya 2002-2012


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 using either one of the following lines:
 (load "/usr/share/newlisp/cgi.lsp")
 ; or as a shorter alternative
 (module "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)
parameter: str-file-name - The HTML file containing <% and %> tags.

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 -
 
 <html>
 <body>
 <a href="http://example.com">example.com</a>
 </body>
 </html>


§

CGI:url-translate

syntax: (CGI:url-translate str-url-format)
parameter: str-url-format - The URL formatted string to translate.

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 (module "cgi.lsp") statement.

Example:
 (module "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