19 CGI Scripts

▼ |  | Net: Racket Networking Libraries

1 | URLs and HTTP
2 | URI Codec: Encoding and Decoding URIs
3 | Web Socket
4 | FTP: Client Downloading
5 | Send URL: Opening a Web Browser
6 | SMTP: Sending E-Mail
7 | sendmail: Sending E-Mail
8 | Headers: Parsing and Constructing
9 | IMAP: Reading Mail
10 | POP3: Reading Mail
11 | MIME: Decoding Internet Data
12 | Base 64: Encoding and Decoding
13 | Quoted-Printable: Encoding and Decoding
14 | DNS: Domain Name Service Queries
15 | NNTP: Newsgroup Protocol
16 | TCP: Unit and Signature
17 | TCP Redirect: tcp^ via Channels
18 | SSL Unit: tcp^ via SSL
19 | CGI Scripts
20 | Cookie: HTTP Client Storage
| Bibliography
| Index

► | 19 | CGI Scripts

19.1 | CGI Functions
19.2 | CGI Unit
19.3 | CGI Signature

On this page:

19.1`` CGI Functions
get-bindings
get-bindings/ post
get-bindings/ get
extract-bindings
extract-binding/ single
output-http-headers
generate-html-output
string->html
generate-link-text
generate-error-output
get-cgi-method
bindings-as-html
cgi-error
incomplete-%-suffix
invalid-%-suffix
19.2`` CGI Unit
cgi@
19.3`` CGI Signature
cgi^

top← prev up next →

### 19`` CGI Scripts

( require net/cgi)

The net/cgi module provides tools for scripts that follow the Common Gateway Interface [CGI].

The net/cgi library expects to be run in a certain context as defined by the CGI standard. This means, for instance, that certain environment variables will be bound.

Unfortunately, not all CGI environments provide this. For instance, the FastCGI library, despite its name, does not bind the environment variables required of the standard. Users of FastCGI will need to bind REQUEST_METHOD and possibly also QUERY_STRING to successfully employ the CGI library. The FastCGI library ought to provide a way to extract the values bound to these variables; the user can then put these into the CGI program’s environment using the putenv function.

A CGI binding is an association of a form item with its value. Some form items, such as checkboxes, may correspond to multiple bindings. A binding is a tag-string pair, where a tag is a symbol or a string.

#### 19.1`` CGI Functions

( get-bindings)
→ ( listof ( cons/c ( or/c symbol? string?) string?))

( get-bindings/post)
→ ( listof ( cons/c ( or/c symbol? string?) string?))

( get-bindings/get)
→ ( listof ( cons/c ( or/c symbol? string?) string?))

Returns the bindings that corresponding to the options specified by the user. The get-bindings/post and get-bindings/get variants work only when POST and GET forms are used, respectively, while get-bindings determines the kind of form that was used and invokes the appropriate function.

These functions respect current-alist-separator-mode.

( extract-bindings key? bindings) → ( listof string?)
key? : ( or/c symbol? string?)
bindings : ( listof ( cons/c ( or/c symbol? string?) string?))

Given a key and a set of bindings, determines which ones correspond to a given key. There may be zero, one, or many associations for a given key.

( extract-binding/single key? bindings) → string?
key? : ( or/c symbol? string?)
bindings : ( listof ( cons/c ( or/c symbol? string?) string?))

Like extract-bindings, but for a key that has exactly one association.

( output-http-headers) → void?

Outputs all the HTTP headers needed for a normal response. Only call this function if you are not using generate-html-output or generate-error-output.

( generate-html-output |  | title |  |  |  |
|  | body |  |  |  |
| [ | text-color |  |  |  |
|  | bg-color |  |  |  |
|  | link-color |  |  |  |
|  | vlink-color |  |  |  |
|  | alink-color]) |  | → |  | void?

title : string?
body : ( listof string?)
text-color : string? = "#000000"
bg-color : string? = "#ffffff"
link-color : string? = "#cc2200"
vlink-color : string? = "#882200"
alink-color : string? = "#444444"

Outputs an response: a title and a list of strings for the body.

The last five arguments are each strings representing a HTML color; in order, they represent the color of the text, the background, un-visited links, visited links, and a link being selected.

( string->html str) → string?
str : string?

Converts a string into an HTML string by applying the appropriate HTML quoting conventions.

( generate-link-text str html-str) → string?
str : string?
html-str : string?

Takes a string representing a URL, a HTML string for the anchor text, and generates HTML corresponding to an anchor.

( generate-error-output strs) → any
strs : ( listof string?)

The procedure takes a list of HTML strings representing the body, prints them with the subject line "Internal error", and exits via exit.

( get-cgi-method) → ( one-of/c "GET" "POST")

Returns either "GET" or "POST" when invoked inside a CGI script, unpredictable otherwise.

( bindings-as-html listof) → (listof string?)
listof : ( cons/c ( or/c symbol? string?) string?)

Converts a set of bindings into a list of HTML strings, which is useful for debugging.

( struct |  | cgi-error ()
|  | #:extra-constructor-name make-cgi-error)

A supertype for all exceptions thrown by the net/cgi library.

( struct |  | incomplete-%-suffix cgi-error (chars)
|  | #:extra-constructor-name make-incomplete-%-suffix)

chars : ( listof char?)

Raised when a % in a query is followed by an incomplete suffix. The characters of the suffix – excluding the % – are provided by the exception.

( struct |  | invalid-%-suffix cgi-error (char)
|  | #:extra-constructor-name make-invalid-%-suffix)

char : char?

Raised when the character immediately following a % in a query is invalid.

#### 19.2`` CGI Unit

( require net/cgi-unit)

cgi@ : unit?

Imports nothing, exports cgi^.

#### 19.3`` CGI Signature

( require net/cgi-sig)

cgi^ : signature

Includes everything exported by the net/cgi module.

top← prev up next →