response.lsp
Module: Response
Author: Jeff Ober
Version: 1.2.1
Original location: http://static.artfulcode.net/newlisp/response.lsp
Source: response.lsp
Package definition: response.qwerty
Response module to replace output functions in the standard CGI module (updated for newlisp 10)
This module is independent of the Http module, and duplicates some of the effort there by providing the ability to write headers to the HTTP output (such as the content-type and the ability to redirect). It is meant to be used with an existing CGI serving application, such as Apache.
Output is controlled using Response:write or default functor (`Response:Response`), which formats the output as a CGI response. The result of Response:Response is a formatted string. It is the application's responsibility to print this string or pass it to the appropriate middle-man.
Version history
1.2.1 • replaced deprecated set-assoc with setf in Response:header
1.2 • fixed incompatibilities with newlisp 10
1.1 • fixed bug in headers • fixed bug forcing text/html on all responses
1.0 • initial release- § -
Response:write
syntax: (Response:write str-text ...)
parameter: str-text - text to write to the output buffer
Response:write allows programmatic output of CGI content.
example:(Response:write "<html>\n") (Response:write "<head><title>Test page</title></head>\n") (Response:write "<body><h3>Hello world</h3></body>\n") (Response:write "</html>")- § -
Response:header
syntax: (Response:header str-key str-val)
parameter: str-key - header name
parameter: str-val - header content
Adds a new header to the output.
- § -
Response:header?
syntax: (Response:header? str-key)
parameter: str-key - header name
Predicates that a header has already been added for output.
- § -
Response:headers
syntax: (Response:headers)
Returns an association list of headers that have been set.
- § -
Response:set-cookie
syntax: (Response:set-cookie str-key str-value [str-domain [str-path [int-expires]]])
parameter: str-key - the cookie's name
parameter: str-value - the cookie's value
parameter: str-domain - optional; the cookie's domain
parameter: str-path - optional; the cookie's path
parameter: int-expires - optional; integer timestamp suitable as an input to date
Adds a cookie to the output buffer.
example:(let ((sid (new-session-id))) (Response:set-cookie "session-id" sid nil nil (+ (date-value) (* 60 60)))) => sets cookie "session-id" to the result of the function 'new-session-id' and an expiration date one hour in the future.- § -
Response:delete-cookie
syntax: (Response:delete-cookie str-key [str-domain [str-path]])
parameter: str-key - the cookie's name
parameter: str-domain - optional; the cookie's domain
parameter: str-path - optional; the cookie's path
Deletes a cookie with matching key/domain/path that has been previously set.
- § -
Response:cookie-set?
syntax: (Response:cookie-set? str-key [str-domain [str-path]])
parameter: str-key - the cookie's name
parameter: str-domain - optional; the cookie's domain
parameter: str-path - optional; the cookie's path
Predicates whether a cookie with a matching pattern has been previously set.
- § -
Response:Response
syntax: (Response:Response [str-text])
parameter: str-text - optional; string text to output
The default functor will return a formatted response, suitable for CGI output. If str-text is provided, this will be the output. Otherwise, any content added with Response:write will be used.
Output will include any necessary headers, including cookies, that were set for this session.
This module is not re-entrant, so in a stateful environment, it should be prototyped using new for each new response.
The manner in which the response is rendered may be hacked by setting the variable Response:_response to a suitable lambda expression. See the code for more implementation details.
example:(Response "Hello world") => "Status: 200 OK\nContent-type: text/html; charset=utf-8\nContent-Length: 11\n\nHello world" (Response:write "Hello world") (Response) => "Status: 200 OK\nContent-type: text/html; charset=utf-8\nContent-Length: 11\n\nHello world"- § -
Response:redirect
syntax: (Response:redirect str-path)
parameter: str-path - the url or path to redirect to
Redirects the client to str-path.
example:(Response:redirect "/some/other/path") => "Status: 302 Found\nLocation: /some/other/path\n\n"- § -
Response:not-found
syntax: (Response:not-found str-message)
parameter: str-message - 404 NOT FOUND error message to display
Outputs a 404 NOT FOUND error message.
example:(Response:not-found "<p>These are not the droids you are looking for.</p>") => "Status: 404 Not Found\nContent-type: text/html; charset=utf-8\nContent-Length: 52\n\n<p>These are not the droids you are looking for.</p>"- § -
Response:error
syntax: (Response:error str-message)
parameter: str-message - 500 ERROR message to display
Outputs a 500 ERROR message.
example:(Response:error "<p>An error occurred while processing this request.</p>") => "Status: 500 Internal Error\nContent-type: text/html; charset=utf-8\nContent-Length: 55\n\n<p>An error occurred while processing this request.</p>"- ∂ -
Artful Code
generated with newLISP and newLISPdoc