File: classes.lisp

package info (click to toggle)
cl-imho 1.2.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,604 kB
  • ctags: 1,104
  • sloc: lisp: 6,569; ansic: 2,120; makefile: 222; sh: 143
file content (299 lines) | stat: -rw-r--r-- 9,262 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
;;; -*- Syntax: Ansi-Common-Lisp; Base: 10; Mode: lisp; Package: imho -*-
;;; $Id: classes.lisp,v 1.19 2001/11/27 17:30:53 jesse Exp $
;;;
;;; Copyright (c) 1999, 2000, 2001 onShore Development, Inc.
;;;
;;; See the file 'COPYING' in this directory for terms.

(in-package :imho)

;; ------------------------------------------------------------
;; framework-class: application
;;
;; The 'application' encapsulates information common to all sessions
;; of an application, such as the path to templates for rendering
;; its html elements, and the application's public base URL.

(defclass application ()
  ((base-url
    :reader base-url
    :initarg :base-url
    :documentation
    "URL fragment at which this application can be accessed below
the HTTPD's mount point")
   (session-class
    :accessor application-session-class
    :initform 'http-session
    :initarg :session-class
    :documentation
    "symbol which names the type of the application's session class")
   (sessions
    :accessor application-sessions
    :initform (make-hash-table :test 'equal)
    :documentation
    "hashtable which contains all active sessions")
   (initial-method
    :initform nil
    :initarg :initial-method
    :documentation
    "symbol which names the application's initial method.  This method will be invoked if no target element is specified in the request, and should return an HTML-ELEMENT instance")
   (initial-element
    :initform nil
    :initarg :initial-element
    :documentation
    "symbol which names the application's initial element")
   (doc-root
    :accessor application-doc-root
    :initform nil
    :initarg :doc-root
    :documentation
    "location of HTML docs for this application's html-element
instances")
   (template-root
    :initform nil
    :initarg :template-root
    :documentation
    "location of HTML templates for this application's html-element
instances")
   (script-root
    :initform nil
    :initarg :script-root
    :documentation
    "location of JavaScript functions for inclusion with components.")
   (style-sheet
    :accessor application-style-sheet
    :initform nil
    :initarg :style-sheet
    :documentation
    "Global style sheet for application.")
   (templates
    :initform (make-hash-table :test 'equal))
   (scripts
    :initform (make-hash-table :test 'equal))
   (html-elements
    :accessor html-elements
    :initform (make-hash-table :test 'equal)
    :documentation
    "hashtable which contains all active html-elements")
   (timeout :accessor application-session-timeout
	    :initarg :application-session-timeout
	    :initform (* 60 120)
	    :documentation "Idle Timeout in seconds"))
  (:documentation
   "encapsulates information common to all sessions of an application,
E.g. where to find templates for rendering its html-elements, the
application's public URL"))


;; ------------------------------------------------------------
;; framework class: http-session

(defclass http-session ()
  ((session-id
    :accessor session-id
    :initarg :id)
   (session-html-elements
    :accessor session-html-elements
    :initform (make-hash-table :test 'equal))
   (session-instances
    :accessor session-instances
    :initform (make-hash-table :test 'equal))
   (session-application
    :accessor session-application
    :initarg :application
    :initform nil)
   (active-response
    :initform nil)
   (last-url
    :accessor last-url
    :initarg :last-url
    :initform nil
    :documentation
    "The last URL visited by this session's client.  This is really
here to support a 'go back' link from a help system page. I wonder if
this is the right way to do it.")
   (help-target
    :accessor help-target
    :initarg :help-target
    :initform "help-main")
   (timeout
    :accessor session-timeout
    :initarg :session-timeout
    :initform 300
    :documentation
    "Idle Timeout in seconds")
   (timestamp
    :accessor session-timestamp
    :initarg :session-timestamp
    :initform (get-universal-time)
    :documentation
    "Used for determining if session has timed-out"))
  (:documentation
   "A session encapsulates all required information about a set of
interactions with a client browser.  Subclasses should store
authentication data and other objects that persist across requests."))

(defmethod destroy-session ((self http-session))
  (slot-makunbound self 'session-id)
  (slot-makunbound self 'session-application)
  (slot-makunbound self 'active-response)
  (with-slots (session-html-elements session-instances)
    self
    (maphash (lambda (k v)
               (declare (ignore k))
               (destroy-element v)) session-html-elements)
    (clrhash session-html-elements)
    (maphash (lambda (k v)
               (declare (ignore k))
               (destroy-element v)) session-instances)
    (clrhash session-instances)))

;; hook for application-specific session classes to do initialization

(defgeneric start-session (t)
  (:documentation "Method called when a session is created, suitable for
specialization by application-specifics session classes."))
  
(defmethod start-session ((session http-session))
  t)

;; ------------------------------------------------------------
;; framework-structure: request

(defstruct (request (:print-function print-http-request))
  "A request object"
  (stream)
  (rid)
  (http-method)

  (protocol)
  (headers-in)
  (cookies-in)
  
  (mount-point)
  (application)
  (session)
  (caller)
  (callee)
  (method)
  (args)

  (client-content)

  (response-type "text/html")
  (response-element)
  (response-length)
  (response-body)
  (response-callback (error "no response callback supplied"))
  
  (binary nil)
  (div-elements nil)
  (html-stream (make-string-output-stream))
  (binary-stream (make-byte-array-output-stream))
  (headers-out *default-headers*)
  (body-attrs '((:onload  . "imho_init_instances();")))
  (doc-title "Untitled Page")
  (css-entries nil)
  (scripted-instances nil))

(defun destroy-request (request)
  (setf (request-stream request) nil
        (request-rid request) nil
        (request-http-method request) nil
        (request-protocol request) nil
        (request-headers-in request) nil
        (request-cookies-in request) nil
        (request-mount-point request) nil
        (request-application request) nil
        (request-session request) nil
        (request-caller request) nil
        (request-callee request) nil
        (request-method request) nil
        (request-args request) nil
        (request-client-content request) nil
        (request-response-type request) nil
        (request-response-length request) nil
        (request-response-body request) nil
        (request-binary request) nil
        (request-div-elements request) nil
        (request-html-stream request) nil
        (request-binary-stream request) nil
        (request-headers-out request) nil
        (request-body-attrs request) nil
        (request-doc-title request) nil
        (request-css-entries request) nil
        (request-scripted-instances request) nil))

(defun print-http-request (request stream depth)
  (declare (ignore depth))
  (print-unreadable-object
   (request stream :type t)
   (ignore-errors
     (format stream "~a/~a"
             (if-bind (app (request-application request))
                 (base-url app)
                 "***")
             (or (request-method request)
                 "***")))))

;; ------------------------------------------------------------
;; framework class: html-element
;;
;; This is the root of the html-element inheritance graph.
;;
;; Some classes to derive their rendering behavior from HTML templates
;; residing in the filesystem, and others from overriding
;; 'render-html'.

(defclass html-element ()
  ((element-external-name
    :reader element-external-name
    :initarg :element-external-name
    :initform (symbol-name (gensym "C"))
    :documentation
    "The externalized name of this html-element, for use in URLs or
interhtml-element references in HTML or client-side code. Guaranteed
unique.")
   (element-internal-name
    :accessor element-internal-name
    :initarg :element-internal-name
    :documentation
    "The name used by this html-element's parent to refer to it.")
   (value
    :initarg :value
    :initform nil
    :documentation
    "application 'value' of this html-element, returned by IMHO public
object protocol")
   (session
    :initarg :session
    :initform nil)
   (parent
    :accessor element-parent
    :initarg :parent
    :initform nil)
   (children
    :initform (make-hash-table)
    :documentation
    "A hashtable of children that are dynamically rendered by this
html-element; keys are the internal names of these children."))
  (:documentation
   "Base display html-element for applications")
  )

(defmethod destroy-element ((self html-element))
  (with-slots (children)
    self
    (maphash (lambda (k v)
               (declare (ignore k))
               (destroy-element v)) children)
    (clrhash children))
  (slot-makunbound self 'value)
  (slot-makunbound self 'parent)
  (slot-makunbound self 'session))

(defmethod print-object ((self html-element) stream)
  (print-unreadable-object
   (self stream :type t)
   (format stream "~s"
           (element-value self))))